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.

feat(build): WeChat-targeted physics + spine module builds

esenginea77e1a015e04
diff --git a/build-tools/build.config.js b/build-tools/build.config.js
index 7248411de..60413a3cb 100644
--- a/build-tools/build.config.js
+++ b/build-tools/build.config.js
@@ -96,6 +96,33 @@ export default {
'sdk/spine41.wasm': 'wasm/web/spine41.wasm',
},
},
+ // WeChat-targeted side modules: the SAME standalone modules built under
+ // ES_BUILD_WXGAME, which forces ENVIRONMENT=web + the wxgame-pre.js shim so
+ // they evaluate inside the MiniGame runtime. Web-aligned filenames in a
+ // separate wechat dir (synced to desktop/public/wasm-wechat) → exportWeChat
+ // require()s `./wasm/physics.js` / `./wasm/spine42.js` exactly as the host expects.
+ 'physics-wechat': {
+ buildDir: 'build-physics-wechat',
+ cmakeFlags: ['-DES_BUILD_WXGAME=ON', '-DES_BUILD_TESTS=OFF', '-DES_ENABLE_BOX2D=ON'],
+ targets: ['physics_module'],
+ outputs: {
+ 'sdk/physics.js': 'wasm/wechat/physics.js',
+ 'sdk/physics.wasm': 'wasm/wechat/physics.wasm',
+ },
+ },
+ 'spine-wechat': {
+ buildDir: 'build-spine-wechat',
+ cmakeFlags: ['-DES_BUILD_WXGAME=ON', '-DES_BUILD_TESTS=OFF'],
+ targets: ['spine_module', 'spine_module_41', 'spine_module_38'],
+ outputs: {
+ 'sdk/spine42.js': 'wasm/wechat/spine42.js',
+ 'sdk/spine42.wasm': 'wasm/wechat/spine42.wasm',
+ 'sdk/spine41.js': 'wasm/wechat/spine41.js',
+ 'sdk/spine41.wasm': 'wasm/wechat/spine41.wasm',
+ 'sdk/spine38.js': 'wasm/wechat/spine38.js',
+ 'sdk/spine38.wasm': 'wasm/wechat/spine38.wasm',
+ },
+ },
},
sdk: {
@@ -127,7 +154,10 @@ export default {
sync: {
wasm: {
'build/wasm/web': 'desktop/public/wasm',
- 'build/wasm/wechat': 'desktop/public/wasm',
+ // WeChat artifacts go to their OWN dir (main.ts resolves wechatWasm from
+ // wasm-wechat first) — web-aligned filenames (physics.js, spine42.js, …)
+ // would otherwise overwrite the web modules in desktop/public/wasm.
+ 'build/wasm/wechat': 'desktop/public/wasm-wechat',
'build/wasm/playable': 'desktop/public/wasm',
},
sdk: {
diff --git a/build-tools/cli.js b/build-tools/cli.js
index 887f7b74b..62d7c4682 100644
--- a/build-tools/cli.js
+++ b/build-tools/cli.js
@@ -27,7 +27,7 @@ program
program
.command('build')
.description('Build ESEngine')
- .option('-t, --target <target>', 'Build target (web, wechat, playable, spine, spine38, spine41, physics, basis, sdk, all)', 'web')
+ .option('-t, --target <target>', 'Build target (web, wechat, playable, spine, spine38, spine41, physics, physics-wechat, spine-wechat, basis, sdk, all)', 'web')
.option('-d, --debug', 'Debug build', false)
.option('-r, --release', 'Release build (default)', true)
.option('-c, --clean', 'Clean before build', false)
@@ -51,7 +51,7 @@ program
const isDebug = options.debug && !options.release;
const targets = options.target === 'all'
- ? ['web', 'wechat', 'playable', 'spine', 'spine38', 'spine41', 'physics', 'basis']
+ ? ['web', 'wechat', 'playable', 'spine', 'spine38', 'spine41', 'physics', 'physics-wechat', 'spine-wechat', 'basis']
: [options.target];
if (options.clean) {
diff --git a/cmake/Emscripten.cmake b/cmake/Emscripten.cmake
index e6751f4aa..8dd9d18c8 100644
--- a/cmake/Emscripten.cmake
+++ b/cmake/Emscripten.cmake
@@ -148,6 +148,17 @@ set(ES_EMSCRIPTEN_WXGAME_SDK_FLAGS
--closure=0
)
+# Extra link flags that make a standalone module (physics/spine) load inside the
+# WeChat MiniGame runtime: force ENVIRONMENT=web (no node/worker branches WeChat
+# can't take) and inject the same window/document shim the engine wxgame build uses
+# (ENVIRONMENT=web's module-level code touches them). Appended to the module flag
+# sets only when ES_BUILD_WXGAME — the web variant keeps the multi-env default.
+set(ES_EMSCRIPTEN_WXGAME_MODULE_EXTRA
+ -sENVIRONMENT=web
+ "--extern-pre-js=${CMAKE_SOURCE_DIR}/src/esengine/platform/web/wxgame-pre.js"
+ --closure=0
+)
+
# Helper function to apply Emscripten settings to a target (monolithic build)
function(es_apply_emscripten_settings TARGET_NAME)
if(ES_BUILD_WEB OR ES_BUILD_WXGAME)
@@ -307,7 +318,11 @@ function(es_apply_spine_module_settings TARGET_NAME)
if(ES_BUILD_WEB OR ES_BUILD_WXGAME)
target_compile_options(${TARGET_NAME} PRIVATE ${ES_EMSCRIPTEN_COMPILE_FLAGS} -flto -fno-exceptions -fno-rtti)
- string(REPLACE ";" " " LINK_FLAGS_STR "${ES_EMSCRIPTEN_SPINE_MODULE_FLAGS}")
+ set(_SPINE_LINK_FLAGS ${ES_EMSCRIPTEN_SPINE_MODULE_FLAGS})
+ if(ES_BUILD_WXGAME)
+ list(APPEND _SPINE_LINK_FLAGS ${ES_EMSCRIPTEN_WXGAME_MODULE_EXTRA})
+ endif()
+ string(REPLACE ";" " " LINK_FLAGS_STR "${_SPINE_LINK_FLAGS}")
set_target_properties(${TARGET_NAME} PROPERTIES
SUFFIX ".js"
LINK_FLAGS "${LINK_FLAGS_STR}"
@@ -393,7 +408,11 @@ function(es_apply_physics_module_settings TARGET_NAME)
endif()
target_compile_options(${TARGET_NAME} PRIVATE ${_PHYSICS_COMPILE_FLAGS})
- string(REPLACE ";" " " LINK_FLAGS_STR "${ES_EMSCRIPTEN_PHYSICS_MODULE_FLAGS}")
+ set(_PHYSICS_LINK_FLAGS ${ES_EMSCRIPTEN_PHYSICS_MODULE_FLAGS})
+ if(ES_BUILD_WXGAME)
+ list(APPEND _PHYSICS_LINK_FLAGS ${ES_EMSCRIPTEN_WXGAME_MODULE_EXTRA})
+ endif()
+ string(REPLACE ";" " " LINK_FLAGS_STR "${_PHYSICS_LINK_FLAGS}")
set_target_properties(${TARGET_NAME} PROPERTIES
SUFFIX ".js"
LINK_FLAGS "${LINK_FLAGS_STR}"
diff --git a/desktop/.gitignore b/desktop/.gitignore
index 5e387e30c..884df0c77 100644
--- a/desktop/.gitignore
+++ b/desktop/.gitignore
@@ -8,3 +8,4 @@ release
# Synced from the engine/SDK build (build-tools/tasks/sync.js) — generated, not source.
public/sdk/
public/wasm/
+public/wasm-wechat/
diff --git a/desktop/electron/exportWeChat.ts b/desktop/electron/exportWeChat.ts
index db88e2c07..63644ffab 100644
--- a/desktop/electron/exportWeChat.ts
+++ b/desktop/electron/exportWeChat.ts
@@ -88,14 +88,14 @@ function projectConfigJson(title: string, appid: string): string {
/** The MiniGame entry WeChat runs: require the engine + the scene's optional
* side modules (physics/spine), hand them to the bundle's boot(). WeChat has no
* fetch/import — modules are pulled in by `require` here (outside the bundle). */
-function gameEntryJs(sideModules: ReadonlyArray<{ id: string; file: string }>): string {
+function gameEntryJs(sideModules: ReadonlyArray<{ id: string; file: string }>, engineGlueFile: string): string {
const requires = sideModules
.map((m) => ` ${JSON.stringify(m.id)}: asFactory(require('./wasm/${m.file}.js')),`)
.join('\n');
return `'use strict';
// Generated by Estella exportGame (WeChat MiniGame entry).
const asFactory = (m) => (typeof m === 'function' ? m : (m && m.default) || m);
-const engineFactory = asFactory(require('./wasm/esengine.js'));
+const engineFactory = asFactory(require('./wasm/${engineGlueFile}'));
const sideModuleFactories = {
${requires}
};
@@ -242,8 +242,11 @@ export async function exportWeChat(opts: {
errors.push(...(e.errors?.map((x) => x.text) ?? [String(e.message ?? err)]));
}
- // 5. Entry + config.
- await writeFile(path.join(absOut, 'game.js'), gameEntryJs(sideModules));
+ // 5. Entry + config. Require the engine glue by its ACTUAL name in the wasm dir
+ // (the -t wechat build emits esengine.wxgame.js; a web-aligned build, esengine.js).
+ const engineGlueFile = ['esengine.js', 'esengine.wxgame.js']
+ .find((f) => existsSync(path.join(opts.wasmDir, f))) ?? 'esengine.js';
+ await writeFile(path.join(absOut, 'game.js'), gameEntryJs(sideModules, engineGlueFile));
await writeFile(path.join(absOut, 'game.json'), gameJson(opts.orientation ?? 'portrait'));
await writeFile(path.join(absOut, 'project.config.json'), projectConfigJson(title, opts.appid ?? ''));