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(export): the source-maps option covers the whole package
yhh76c4289c2b2a
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 80c5d2a0b..0c3151733 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -50,6 +50,17 @@ published separately; it ships inside the editor.### Fixed+- **A web package carried the SDK's source maps whatever the build asked for.**+ "Source maps" is a build option, off by default, and it reached only the two+ bundles the export writes itself — the SDK tree copied in beside them brought+ its maps along regardless. They were most of the package: `hello-world` shipped+ 4.96 MB of them in 8.0 MB. The option now covers both halves, which is what it+ reads as, and a build without maps also drops the `sourceMappingURL` line from+ the scripts it copies — a package that names a map it does not carry 404s the+ moment anyone opens devtools on the shipped game.++ With the module scan below, `hello-world` goes from 8.0 MB to 3.0 MB.+- **A web package carried every optional module the engine builds.** The exportcopied the engine's wasm directory wholesale, so a 2D game shipped the 3Dsolver, all five Spine runtimes, the DragonBones player, the video decoder anddiff --git a/desktop/tests/export-game.test.ts b/desktop/tests/export-game.test.tsindex 64f974df9..6c527a6ac 100644--- a/desktop/tests/export-game.test.ts+++ b/desktop/tests/export-game.test.ts@@ -111,11 +111,10 @@ describe('exportGame', () => {expect(has('game.js')).toBe(true);expect(has('scripts.mjs')).toBe(true);expect(has('sdk/index.js')).toBe(true);- // The import map's target ships with its chunks and its map; the OTHER- // platforms' builds and the type declarations do not. Copying sdk/dist- // wholesale put the Node, WeChat, mini-game and native SDKs — each with a- // multi-megabyte source map — into a package for a browser.- expect(has('sdk/index.js.map')).toBe(true);+ // The import map's target ships with its chunks; the other platforms' builds+ // and the type declarations do not. Maps follow the export's own sourcemap+ // answer, which is off by default.+ expect(has('sdk/index.js.map')).toBe(false);expect(has('sdk/shared/resource.js')).toBe(true);expect(has('sdk/index.d.ts')).toBe(false);for (const dead of [diff --git a/desktop/tests/export-source-maps.test.ts b/desktop/tests/export-source-maps.test.tsnew file mode 100644index 000000000..eddaf5721--- /dev/null+++ b/desktop/tests/export-source-maps.test.ts@@ -0,0 +1,78 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file Whether a web package carries source maps. One answer covers the+ * bundles the export writes and the SDK tree it copies beside them.+ */+import { describe, it, expect } from 'vitest';+import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, existsSync, rmSync } from 'node:fs';+import { tmpdir } from 'node:os';+import path from 'node:path';+import { fileURLToPath } from 'node:url';+import { exportGame } from '../../pipeline/src/export/exportGame';++const HERE = path.dirname(fileURLToPath(import.meta.url));+const GAME_HOST = path.join(HERE, '..', '..', 'pipeline', 'src', 'runtime', 'gameHost.ts');++const SCN = 'ffffffff-ffff-ffff-ffff-ffffffffffff';+const meta = (uuid: string, type: string) => JSON.stringify({ uuid, version: '2.0', type, importer: {} });++function setup(): { root: string; out: string } {+ const root = mkdtempSync(path.join(tmpdir(), 'estella-srcmap-'));+ mkdirSync(path.join(root, 'scenes'), { recursive: true });+ writeFileSync(path.join(root, 'scenes', 'main.esscene'),+ JSON.stringify({ version: '1.0', name: 'Main', entities: [{ id: 0, components: [] }] }));+ writeFileSync(path.join(root, 'scenes', 'main.esscene.meta'), meta(SCN, 'scene'));++ mkdirSync(path.join(root, '_sdk', 'shared'), { recursive: true });+ // Exactly how the SDK build writes them: the script names its map on the last+ // line, and the map sits beside it.+ writeFileSync(path.join(root, '_sdk', 'index.js'), 'export const x = 1;\n//# sourceMappingURL=index.js.map\n');+ writeFileSync(path.join(root, '_sdk', 'index.js.map'), '{"version":3}');+ writeFileSync(path.join(root, '_sdk', 'shared', 'resource.js'), 'export const r = 1;\n//# sourceMappingURL=resource.js.map');+ writeFileSync(path.join(root, '_sdk', 'shared', 'resource.js.map'), '{"version":3}');++ mkdirSync(path.join(root, '_wasm'), { recursive: true });+ writeFileSync(path.join(root, '_wasm', 'esengine.js'), 'export default () => {};');+ writeFileSync(path.join(root, '_wasm', 'esengine.wasm'), 'ENGINE');+ return { root, out: path.join(root, 'dist') };+}++const run = (f: { root: string; out: string }, sourcemap?: boolean) => exportGame({+ root: f.root, entryScene: 'scenes/main.esscene', gameHostEntry: GAME_HOST,+ sdkDistDir: path.join(f.root, '_sdk'), wasmDir: path.join(f.root, '_wasm'),+ outDir: f.out, ...(sourcemap === undefined ? {} : { sourcemap }),+});++describe('source maps in a web package', () => {+ it('ships none by default, and leaves no script asking for one', async () => {+ const f = setup();+ try {+ const res = await run(f);+ expect(res.errors).toEqual([]);+ expect(existsSync(path.join(f.out, 'sdk', 'index.js.map'))).toBe(false);+ expect(existsSync(path.join(f.out, 'sdk', 'shared', 'resource.js.map'))).toBe(false);+ // A dangling reference is a 404 the moment anyone opens devtools on the+ // shipped game, so the copy drops the line as well as the file.+ for (const rel of ['sdk/index.js', 'sdk/shared/resource.js']) {+ expect([rel, readFileSync(path.join(f.out, rel), 'utf8')]).toEqual([rel, expect.not.stringContaining('sourceMappingURL')]);+ }+ // Stripping the comment is all it does to the script.+ expect(readFileSync(path.join(f.out, 'sdk', 'index.js'), 'utf8')).toContain('export const x = 1;');+ expect(existsSync(path.join(f.out, 'game.js.map'))).toBe(false);+ } finally { rmSync(f.root, { recursive: true, force: true }); }+ }, 60_000);++ it('ships them when the build asks for them', async () => {+ const f = setup();+ try {+ const res = await run(f, true);+ expect(res.errors).toEqual([]);+ expect(existsSync(path.join(f.out, 'sdk', 'index.js.map'))).toBe(true);+ expect(existsSync(path.join(f.out, 'sdk', 'shared', 'resource.js.map'))).toBe(true);+ expect(readFileSync(path.join(f.out, 'sdk', 'index.js'), 'utf8')).toContain('sourceMappingURL');+ // The export's own bundles answer the same question the same way.+ expect(existsSync(path.join(f.out, 'game.js.map'))).toBe(true);+ } finally { rmSync(f.root, { recursive: true, force: true }); }+ }, 60_000);+});diff --git a/pipeline/src/export/exportGame.ts b/pipeline/src/export/exportGame.tsindex af7ad1b9c..4995c4613 100644--- a/pipeline/src/export/exportGame.ts+++ b/pipeline/src/export/exportGame.ts@@ -137,7 +137,9 @@ export async function discoverProjectScenes(root: string, entryScene: string, scconst OTHER_PLATFORM_ENTRIES = /^index\.(node|wechat|wechat\.cjs|minigame|native|native\.bundled|bundled)\.js(\.map)?$/;/**- * Whether a file under sdk/dist belongs in a browser package.+ * Whether a file under sdk/dist belongs in a browser package built with+ * `sourceMaps` on or off — the same answer the export's own bundles get, since+ * the maps a package carries are one decision and not one per producer.** Directories always pass — the per-file test decides what lands inside them,* and refusing a directory would drop the shared chunks the entry imports.@@ -148,10 +150,29 @@ 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(src: string): boolean {- const base = path.basename(src);- if (base.endsWith('.d.ts')) return false;- return !OTHER_PLATFORM_ENTRIES.test(base);+export function shipsToBrowser(sourceMaps: boolean): (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);+ };+}++/**+ * Strip the `sourceMappingURL` line from every script under `dir`. A package+ * built without maps still carries scripts that name one, and the browser asks+ * for it the moment anyone opens devtools on the shipped game.+ */+async function dropSourceMapRefs(dir: string): Promise<void> {+ for (const entry of await readdir(dir, { withFileTypes: true })) {+ const full = path.join(dir, entry.name);+ if (entry.isDirectory()) { await dropSourceMapRefs(full); continue; }+ if (!/\.(js|mjs|cjs)$/.test(entry.name)) continue;+ const text = await readFile(full, 'utf8');+ const stripped = text.replace(/\r?\n?[ \t]*\/\/# sourceMappingURL=.*/g, '');+ if (stripped !== text) await writeFile(full, stripped);+ }}/**@@ -567,6 +588,9 @@ async function produceExport(opts: ExportGameOptions): Promise<ExportGameResult>/** Desktop, Steam channel: the per-build checklist written beside the scripts. */let steamChecklist: string | undefined;await mkdir(payloadDir, { recursive: true });+ // One answer for the whole package: the bundles esbuild writes here and the+ // SDK tree copied in beside them.+ const sourceMaps = opts.sourcemap ?? false;const common: BuildOptions = {bundle: true,format: 'esm',@@ -574,7 +598,7 @@ async function produceExport(opts: ExportGameOptions): Promise<ExportGameResult>target: 'es2020',external: ESENGINE_EXTERNAL,minify: opts.minify ?? false,- sourcemap: opts.sourcemap ?? false,+ sourcemap: sourceMaps,write: true,logLevel: 'silent',};@@ -669,7 +693,10 @@ async function produceExport(opts: ExportGameOptions): Promise<ExportGameResult>if (!nativeContent) {progress({ phase: 'Copying SDK + runtime' });if (existsSync(opts.sdkDistDir)) {- await cp(opts.sdkDistDir, path.join(payloadDir, 'sdk'), { recursive: true, filter: shipsToBrowser });+ await cp(opts.sdkDistDir, path.join(payloadDir, 'sdk'), {+ recursive: true, filter: shipsToBrowser(sourceMaps),+ });+ if (!sourceMaps) await dropSourceMapRefs(path.join(payloadDir, 'sdk'));} else errors.push(`sdk dist not found: ${opts.sdkDistDir}`);if (existsSync(opts.wasmDir)) {const ids = await scanSideModuleIds({