Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(wechat): down-level emscripten glue .js to es2017 on export

yhh9ab1b18d2ebf
diff --git a/desktop/electron/exportWeChat.ts b/desktop/electron/exportWeChat.ts
index cfc2b8509..bbda37b4f 100644
--- a/desktop/electron/exportWeChat.ts
+++ b/desktop/electron/exportWeChat.ts
@@ -412,10 +412,22 @@ export async function exportWeChat(opts: {
engineGlueFile.replace(/\.js$/, '.wasm'),
...sideModules.flatMap((m) => [`${m.file}.js`, `${m.file}.wasm`]),
];
+ const { transform } = await loadEsbuild();
for (const f of runtimeFiles) {
const src = path.join(opts.wasmDir, f);
- if (existsSync(src)) await cp(src, path.join(wasmOut, f));
- else errors.push(`wechat runtime file missing: ${f} (in ${opts.wasmDir}) — rebuild with \`node build-tools/cli.js build -t wechat\``);
+ if (!existsSync(src)) {
+ errors.push(`wechat runtime file missing: ${f} (in ${opts.wasmDir}) — rebuild with \`node build-tools/cli.js build -t wechat\``);
+ continue;
+ }
+ const dest = path.join(wasmOut, f);
+ if (f.endsWith('.js')) {
+ // Emscripten glue can carry es2020 syntax (`?.`, `??`) that real-device
+ // WeChat rejects — down-level it like the game bundle.
+ const out = await transform(await readFile(src, 'utf8'), { target: 'es2017', loader: 'js' });
+ await writeFile(dest, out.code);
+ } else {
+ await cp(src, dest);
+ }
}
return { ok: errors.length === 0, platform: 'wechat', outDir: absOut, included: cook.included.length, warnings, errors };