Skip to content

Commit f21b699

Browse files
committed
fix(cli): transform yoga-sync.mjs to remove top-level await for CJS
The newer yoga-sync builds from socket-btm use top-level await which isn't compatible with esbuild's CJS output format. This adds a transformation step during asset processing to convert the top-level await pattern back to a promise-based export.
1 parent 5b39cc7 commit f21b699

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

packages/cli/scripts/download-assets.mjs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,30 @@ async function extractArchive(tarGzPath, extractConfig, assetName) {
208208
await fs.writeFile(versionPath, tag, 'utf-8')
209209
}
210210

211+
/**
212+
* Transform yoga-sync.mjs to remove top-level await for CJS compatibility.
213+
*
214+
* The newer yoga-sync builds use top-level await which isn't compatible with
215+
* esbuild's CJS output format. We transform it back to exporting a promise.
216+
*/
217+
function transformYogaSync(content) {
218+
// Pattern: const Yoga = wrapAssembly(await yogaPromise);
219+
// Transform to: const Yoga = yogaPromise.then(m => wrapAssembly(m));
220+
// And change: export default Yoga;
221+
// To: export default Yoga;
222+
// (keep the export, but now Yoga is a promise)
223+
const hasTopLevelAwait = content.includes('wrapAssembly(await yogaPromise)')
224+
if (!hasTopLevelAwait) {
225+
return content
226+
}
227+
228+
// Replace the top-level await pattern.
229+
return content.replace(
230+
/const Yoga = wrapAssembly\(await yogaPromise\);/,
231+
'const Yoga = yogaPromise.then(m => wrapAssembly(m));',
232+
)
233+
}
234+
211235
/**
212236
* Process and transform asset (e.g., add header to JS file).
213237
*/
@@ -238,7 +262,12 @@ async function processAsset(assetPath, processConfig, assetName) {
238262
}
239263

240264
// Read the downloaded asset.
241-
const content = await fs.readFile(assetPath, 'utf-8')
265+
let content = await fs.readFile(assetPath, 'utf-8')
266+
267+
// Transform yoga-sync to remove top-level await for CJS compatibility.
268+
if (assetName === 'yoga') {
269+
content = transformYogaSync(content)
270+
}
242271

243272
// Compute source hash for cache validation.
244273
const sourceHash = await computeFileHash(assetPath)

0 commit comments

Comments
 (0)