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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(pipeline): the esengine specifiers a game can write are the ones the SDK publishes
esengine6879775aa806
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 8c4bd0e38..96369110e 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -23,6 +23,12 @@ published separately; it ships inside the editor.### Fixed+- **A page no longer carries a module mapping that resolves to a file it does not have.** The+ exported page's import map listed `esengine/factory` → `sdk/webAppFactory.js`; the SDK has+ never published that subpath and `dist/` has never emitted that file, so a game importing it+ would have failed at load. The map and the bundler's aliases are derived from one list now,+ and a gate holds that list against what the package actually exports.+- **A build that ships Spine or DragonBones installs one of each again, not two.** Makingregistration a call rather than a module's side effect gave it two callers — the`esengine/spine` subpath and an entry's own install — and the plugin list appended both,diff --git a/pipeline/src/bundle/esengineResolve.ts b/pipeline/src/bundle/esengineResolve.tsindex 5a37aab4a..e2efab34b 100644--- a/pipeline/src/bundle/esengineResolve.ts+++ b/pipeline/src/bundle/esengineResolve.ts@@ -16,6 +16,21 @@ import path from 'node:path';/** esengine left external (web / desktop import-map builds). */export const ESENGINE_EXTERNAL = ['esengine', 'esengine/*'];+/**+ * The subpaths a game may import, and what each resolves to under the staged+ * SDK. ONE list: both strategies read it, and so does the import map — a second+ * hand-written copy is how a specifier the SDK never exported survived in both.+ * check-import-map.mjs holds it against the package.+ */+export const ESENGINE_SUBPATHS: Readonly<Record<string, string>> = {+ 'esengine/spine': 'spine/index.js',+ 'esengine/dragonbones': 'dragonbones/index.js',+ 'esengine/physics': 'physics/index.js',+ 'esengine/physics3d': 'physics3d/index.js',+ 'esengine/douyin': 'douyin/index.js',+ 'esengine/wasm': 'wasm.js',+};+/*** esbuild `alias` resolving `esengine` and its subpath exports to files under* `sdkDir`, for INLINED builds (the project root has no esengine to resolve from).@@ -23,14 +38,9 @@ export const ESENGINE_EXTERNAL = ['esengine', 'esengine/*'];* 'index.wechat.js' (the WeChat SDK).*/export function esengineAlias(sdkDir: string, mainEntry = 'index.js'): Record<string, string> {- return {- esengine: path.join(sdkDir, mainEntry),- 'esengine/spine': path.join(sdkDir, 'spine', 'index.js'),- 'esengine/dragonbones': path.join(sdkDir, 'dragonbones', 'index.js'),- 'esengine/physics': path.join(sdkDir, 'physics', 'index.js'),- 'esengine/physics3d': path.join(sdkDir, 'physics3d', 'index.js'),- 'esengine/douyin': path.join(sdkDir, 'douyin', 'index.js'),- 'esengine/wasm': path.join(sdkDir, 'wasm.js'),- 'esengine/factory': path.join(sdkDir, 'webAppFactory.js'),- };+ const out: Record<string, string> = { esengine: path.join(sdkDir, mainEntry) };+ for (const [specifier, rel] of Object.entries(ESENGINE_SUBPATHS)) {+ out[specifier] = path.join(sdkDir, ...rel.split('/'));+ }+ return out;}diff --git a/pipeline/src/bundle/importMap.ts b/pipeline/src/bundle/importMap.tsindex 3ab1fd656..2bda4eab6 100644--- a/pipeline/src/bundle/importMap.ts+++ b/pipeline/src/bundle/importMap.ts@@ -6,20 +6,22 @@* layout it was previewed on.** Subpath exports are listed file by file: an import map does not append- * `/index.js` for a directory. Mirrors the SDK's `exports`.+ * `/index.js` for a directory. The list is esengineResolve's, so the two+ * strategies cannot name different subpaths.*/import { createHash } from 'node:crypto';+import { ESENGINE_SUBPATHS } from './esengineResolve';++/** Where the exporter stages the SDK, relative to the page. */+const STAGED = './sdk/';export const IMPORT_MAP = {imports: {- esengine: './sdk/index.js',- 'esengine/spine': './sdk/spine/index.js',- 'esengine/dragonbones': './sdk/dragonbones/index.js',- 'esengine/physics': './sdk/physics/index.js',- 'esengine/physics3d': './sdk/physics3d/index.js',- 'esengine/wasm': './sdk/wasm.js',- 'esengine/factory': './sdk/webAppFactory.js',- },+ esengine: `${STAGED}index.js`,+ ...Object.fromEntries(+ Object.entries(ESENGINE_SUBPATHS).map(([specifier, rel]) => [specifier, STAGED + rel]),+ ),+ } as Record<string, string>,};export const IMPORT_MAP_JSON = JSON.stringify(IMPORT_MAP);diff --git a/tools/check-import-map.mjs b/tools/check-import-map.mjsnew file mode 100644index 000000000..081fb682d--- /dev/null+++ b/tools/check-import-map.mjs@@ -0,0 +1,77 @@+#!/usr/bin/env node+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file Every `esengine` specifier a game may write resolves to a file the SDK+ * actually publishes.+ *+ * Two files said "mirrors sdk/package.json exports" and neither was held to it:+ * `esengine/factory` sat in both, pointing at `webAppFactory.js`, which the SDK+ * has never exported and `dist/` has never emitted at that path. A page whose+ * script imported it would get a bare-specifier error at load; nothing did, so+ * nothing said anything for a year.+ *+ * Both now derive from one list, and this holds that list against the package.+ *+ * node tools/check-import-map.mjs+ */+import { readFileSync, existsSync } from 'node:fs';+import path from 'node:path';+import { fileURLToPath } from 'node:url';++const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');+const RESOLVE = 'pipeline/src/bundle/esengineResolve.ts';++const src = readFileSync(path.join(ROOT, RESOLVE), 'utf8');+const block = /export const ESENGINE_SUBPATHS[^{]*\{([\s\S]*?)\n\};/.exec(src);+if (!block) {+ console.error(`check-import-map: no ESENGINE_SUBPATHS in ${RESOLVE} — the list moved.`);+ process.exit(1);+}++/** `'esengine/spine': 'spine/index.js',` — the specifier and what it resolves to. */+const listed = [...block[1].matchAll(/'(esengine\/[^']+)'\s*:\s*'([^']+)'/g)]+ .map(([, specifier, file]) => ({ specifier, file }));++if (listed.length === 0) {+ console.error(`check-import-map: ESENGINE_SUBPATHS is empty — a game could import no subpath.`);+ process.exit(1);+}++const pkg = JSON.parse(readFileSync(path.join(ROOT, 'sdk', 'package.json'), 'utf8'));+const problems = [];++for (const { specifier, file } of listed) {+ const subpath = `.${specifier.slice('esengine'.length)}`;+ const entry = pkg.exports?.[subpath];+ if (!entry) {+ problems.push(`${specifier} is resolved, but sdk/package.json exports no "${subpath}" — `+ + 'a game writing it gets a bare-specifier error, and the exporter stages nothing for it');+ continue;+ }+ // The exporter stages dist/ as sdk/, so the export's own target is the answer.+ const target = (typeof entry === 'string' ? entry : entry.import ?? '').replace(/^\.\/dist\//, '');+ if (target !== file) {+ problems.push(`${specifier} resolves to "${file}" but "${subpath}" exports "${target}" — `+ + 'the staged file and the published one are different files');+ }+}++/**+ * The bare specifier is not in the list (every strategy names its own main+ * entry), so it is checked here: a page's import map points at `sdk/index.js`.+ */+const main = typeof pkg.exports?.['.'] === 'string' ? pkg.exports['.'] : pkg.exports?.['.']?.import;+if (main !== './dist/index.js') {+ problems.push(`sdk/package.json exports "." as "${main}", and the import map stages `+ + '"./sdk/index.js" — the page would fetch a file the package does not publish there');+}++if (problems.length > 0) {+ for (const p of problems) console.error(` ${p}`);+ console.error(`check-import-map: ${problems.length} finding(s).`);+ process.exit(1);+}++console.log(`check-import-map: ${listed.length + 1} esengine specifier(s), each published by `+ + 'sdk/package.json and staged from the file it names.');diff --git a/tools/gates.mjs b/tools/gates.mjsindex 4e09efb51..f5d6e01d7 100644--- a/tools/gates.mjs+++ b/tools/gates.mjs@@ -256,6 +256,9 @@ export const GATES = [// variables are the editor, the plugins and the docs, and only the first is// check-theme's subject.{ id: 'css-vars', run: 'node tools/check-css-vars.mjs', needs: 'editor' },+ // The specifiers a game writes against the SDK, held to what the package+ // publishes: a map entry that resolves to nothing fails at load, not at build.+ { id: 'import-map', run: 'node tools/check-import-map.mjs' },{ id: 'comment-style', run: 'node tools/check-comment-style.mjs' },{ id: 'silent-writes', run: 'node tools/mutation-census.mjs --gate' },{diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex 157f339e3..d54de1306 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -28,6 +28,9 @@ export const NOTES_FLOOR = 'v0.65.0';* the reader recognises anyway.*/export const NOTED = {+ 'fix(pipeline): the esengine specifiers a game can write are the ones the SDK publishes':+ { note: 'A page no longer carries a module mapping that resolves to a file it does not have.' },+'fix(sdk): a subsystem installs one plugin, however many callers registered it':{ note: 'A build that ships Spine or DragonBones installs one of each again, not two.' },