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(export): a web package no longer carries another host's SDK

esengine1443f635c033
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 96369110e..8e6c5f61d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -23,6 +23,12 @@ published separately; it ships inside the editor.
### Fixed
+- **A web build stops shipping the WeChat SDK entries next to the one its page loads.**
+ `sdk/dist` holds every target's build side by side and the staging step filtered them by an
+ enumerated list of filenames, so an entry added after that list — `index.wechat.lean` — went
+ out in every web package, along with the WeChat open-data script. The kept set is derived
+ from the page's own import map now.
+
- **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
diff --git a/pipeline/src/export/exportGame.ts b/pipeline/src/export/exportGame.ts
index 4a680c8ec..708e39bcd 100644
--- a/pipeline/src/export/exportGame.ts
+++ b/pipeline/src/export/exportGame.ts
@@ -33,7 +33,7 @@ import type { PackagedGameConfig } from 'esengine';
import {
DEFAULT_RUNTIME_CONFIG, packagedRuntimeFields, type RuntimeProjectConfig,
} from '../project/runtimeConfig';
-import { IMPORT_MAP_JSON, IMPORT_MAP_CSP_HASH } from '../bundle/importMap';
+import { IMPORT_MAP, IMPORT_MAP_JSON, IMPORT_MAP_CSP_HASH } from '../bundle/importMap';
import { exportMiniGame } from './exportMiniGame';
import { wechatExportProfile, douyinExportProfile } from './miniGameExportProfile';
import type { MiniGameExportProfile } from './miniGameExportProfile';
@@ -141,13 +141,17 @@ export async function discoverProjectScenes(root: string, entryScene: string, sc
}
/**
- * Entry bundles in sdk/dist that a BROWSER page can never load: the other
- * platforms' builds. `dist` holds every target's output side by side, and the
- * export used to copy the tree wholesale — so a web package shipped the Node,
- * WeChat, mini-game and native SDKs, each with a multi-megabyte source map, next
- * to the one the import map actually names.
+ * Top-level bundles in sdk/dist a BROWSER page can load, derived from the map
+ * that names them — `dist` holds every target's output side by side.
+ *
+ * Derived, not enumerated: a listed set went stale the first time an entry was
+ * added, and a web package shipped a WeChat SDK.
*/
-const OTHER_PLATFORM_ENTRIES = /^index\.(node|wechat|wechat\.cjs|minigame|native|native\.bundled|bundled)\.js(\.map)?$/;
+const BROWSER_TOP_LEVEL = new Set(
+ Object.values(IMPORT_MAP.imports)
+ .map((t) => t.replace(/^\.\/sdk\//, ''))
+ .filter((t) => !t.includes('/')),
+);
/**
* Whether a file under sdk/dist belongs in a browser package built with
@@ -163,12 +167,17 @@ const OTHER_PLATFORM_ENTRIES = /^index\.(node|wechat|wechat\.cjs|minigame|native
* would be exact but silent when it is wrong, and a bundle this drops that
* something did want is a 404 the moment the page loads, not a subtle bug.
*/
-export function shipsToBrowser(sourceMaps: boolean): (src: string) => boolean {
+export function shipsToBrowser(sourceMaps: boolean, sdkDist?: string): (src: string) => boolean {
return (src: string) => {
const base = path.basename(src);
if (base.endsWith('.d.ts')) return false;
if (!sourceMaps && base.endsWith('.map')) return false;
- return !OTHER_PLATFORM_ENTRIES.test(base);
+ // Only the top level: a subpath's `index.js` lives in its own directory, and
+ // `shared/` is chunks rather than entries.
+ if (sdkDist && /\.js(\.map)?$/.test(base) && path.dirname(src) === path.resolve(sdkDist)) {
+ return BROWSER_TOP_LEVEL.has(base.replace(/\.map$/, ''));
+ }
+ return true;
};
}
@@ -856,7 +865,7 @@ async function produceExport(opts: ExportGameOptions): Promise<ExportGameResult>
progress({ phase: 'Copying SDK + runtime' });
if (existsSync(opts.sdkDistDir)) {
await cp(opts.sdkDistDir, path.join(payloadDir, 'sdk'), {
- recursive: true, filter: shipsToBrowser(sourceMaps),
+ recursive: true, filter: shipsToBrowser(sourceMaps, opts.sdkDistDir),
});
if (!sourceMaps) await dropSourceMapRefs(path.join(payloadDir, 'sdk'));
} else errors.push(`sdk dist not found: ${opts.sdkDistDir}`);
diff --git a/pipeline/tests/ships-to-browser.test.ts b/pipeline/tests/ships-to-browser.test.ts
new file mode 100644
index 000000000..61f57b7d2
--- /dev/null
+++ b/pipeline/tests/ships-to-browser.test.ts
@@ -0,0 +1,53 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file A web package carries the entry the page names and no other host's.
+ *
+ * `sdk/dist` holds every target's build side by side, so the staging step is the
+ * only thing standing between a browser package and the Node, WeChat, mini-game
+ * and native SDKs. The set it keeps is derived from the import map, because an
+ * enumerated one went stale the first time an entry was added.
+ */
+import { describe, it, expect } from 'vitest';
+import path from 'node:path';
+import { shipsToBrowser } from '../src/export/exportGame';
+import { IMPORT_MAP } from '../src/bundle/importMap';
+
+const DIST = path.resolve('/tmp/does-not-need-to-exist/dist');
+const at = (rel: string) => path.join(DIST, rel);
+
+describe('what a web export stages from sdk/dist', () => {
+ const keeps = shipsToBrowser(false, DIST);
+
+ it('keeps exactly the top-level files the import map names', () => {
+ const named = Object.values(IMPORT_MAP.imports)
+ .map((t) => t.replace('./sdk/', ''))
+ .filter((t) => !t.includes('/'));
+ for (const f of named) expect(keeps(at(f)), f).toBe(true);
+ });
+
+ it.each([
+ 'index.node.js', 'index.wechat.js', 'index.wechat.cjs.js', 'index.wechat.lean.js',
+ 'index.minigame.js', 'index.native.js', 'index.native.bundled.js', 'index.bundled.js',
+ 'open-data.js',
+ ])('drops %s — a browser page can never load it', (f) => {
+ expect(keeps(at(f))).toBe(false);
+ });
+
+ it('keeps a subpath entry, which lives in its own directory', () => {
+ for (const f of ['spine/index.js', 'physics/index.js', 'douyin/index.js']) {
+ expect(keeps(at(f)), f).toBe(true);
+ }
+ });
+
+ it('keeps the shared chunks, which are not entries and are named by nothing', () => {
+ expect(keeps(at('shared/webAppFactory.js'))).toBe(true);
+ });
+
+ it('drops declarations and, without source maps, the maps', () => {
+ expect(keeps(at('index.d.ts'))).toBe(false);
+ expect(keeps(at('index.js.map'))).toBe(false);
+ expect(shipsToBrowser(true, DIST)(at('index.js.map'))).toBe(true);
+ expect(shipsToBrowser(true, DIST)(at('index.wechat.js.map'))).toBe(false);
+ });
+});
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index d54de1306..f3177f44d 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(export): a web package no longer carries another host's SDK":
+ { note: "A web build stops shipping the WeChat SDK entries next to the one its page loads." },
+
'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.' },