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
feat(editor): a build can be failed for being too big
yhhbaf49c1300f8
diff --git a/desktop/scripts/export-project.mjs b/desktop/scripts/export-project.mjsindex 33fd901e4..f97cdd9e1 100644--- a/desktop/scripts/export-project.mjs+++ b/desktop/scripts/export-project.mjs@@ -18,9 +18,14 @@// --scripts <path> scripts entry, project-relative (default src/main.ts if present)// --template <dir> android/ios: the runtime template to wrap, else the installed one// --json <file> also write the result here, for a caller that reads it back+// --enforce-budget fail (exit 1) when the package is over a size limit//-// Prints the export result as JSON — errors and warnings included, so a caller can-// tell a clean package from one that silently dropped half a scene.+// Prints the export result as JSON — errors, warnings, and what the package weighs+// against the limits in force — so a caller can tell a clean package from one that+// silently dropped half a scene, or from one that will be refused for being too big.+//+// The size report rides the result rather than being a second file: one artifact a+// CI job reads, `result.size`, with the same verdicts the build dialog draws.import path from 'node:path';import { existsSync, readFileSync, writeFileSync, mkdtempSync, rmSync } from 'node:fs';import { fileURLToPath } from 'node:url';@@ -35,15 +40,27 @@ function parseArgs(argv) {const [projectDir, ...rest] = argv;if (!projectDir) {console.error('usage: node desktop/scripts/export-project.mjs <projectDir> '- + '[--platform android] [--out dir] [--output package|project] [--template dir]');+ + '[--platform android] [--out dir] [--output package|project] [--template dir] '+ + '[--enforce-budget]');process.exit(2);}const opts = { projectDir: path.resolve(projectDir), platform: 'android' };- for (let i = 0; i < rest.length; i += 2) {+ // Options take a value; these do not. Without the distinction a trailing+ // `--enforce-budget` swallows nothing and ends the loop, so a CI job would+ // pass the flag and silently get no gate.+ const FLAGS = new Set(['enforce-budget']);+ for (let i = 0; i < rest.length;) {const key = rest[i]?.replace(/^--/, '');+ if (!key) break;+ if (FLAGS.has(key)) {+ opts[key] = true;+ i += 1;+ continue;+ }const value = rest[i + 1];- if (!key || value === undefined) break;+ if (value === undefined) break;opts[key] = value;+ i += 2;}return opts;}@@ -147,7 +164,23 @@ const templateDir = nativePlatform? path.resolve(opts.template): firstExisting([installedTemplateDir(engineVersion, platform)]) ?? null): null;-const { resolveOrientation } = await loadProjectFormat();+// The engine runtime for this target — the headless half of the editor's+// `platformRuntimeDirs`. WeChat's is a different build (WXWebAssembly glue, `-t+// wechat`), and handing it the web one produced either a "runtime not found"+// failure or a package that could not boot on a device.+const wasmDir = platform === 'wechat'+ ? firstExisting([+ path.join(DESKTOP, 'public', 'wasm-wechat'),+ path.join(REPO, 'build', 'wasm', 'wechat'),+ ]) ?? path.join(DESKTOP, 'public', 'wasm')+ : path.join(DESKTOP, 'public', 'wasm');++const { resolveOrientation, parseManifest } = await loadProjectFormat();+// PARSED, not read by hand: the parser normalizes legacy platform ids and drops+// values that could not be judged against, and a budget read straight off the JSON+// here would be the orientation bug again with a different field.+const manifest = parseManifest(project);+const sizeBudgetBytes = manifest.packaging?.sizeBudget?.[platform];const { exportGame, cleanup } = await loadExportGame();let code = 1;try {@@ -157,7 +190,7 @@ try {scriptsEntry,gameHostEntry: path.join(DESKTOP, 'src', 'gameHost.ts'),sdkDistDir: path.join(REPO, 'sdk', 'dist'),- wasmDir: path.join(DESKTOP, 'public', 'wasm'),+ wasmDir,outDir,platform,title: opts.title ?? project.name ?? path.basename(opts.projectDir),@@ -165,6 +198,7 @@ try {androidTemplate: platform === 'android' ? templateDir : null,iosSources: platform === 'ios' && templateDir ? iosTemplateSources(templateDir) : null,androidOutput: opts.output === 'project' ? 'project' : undefined,+ sizeBudgetBytes,});const report = { ...result, outDir };console.log(JSON.stringify(report, null, 2));@@ -172,6 +206,18 @@ try {// result mechanically cannot just redirect it.if (opts.json) writeFileSync(path.resolve(opts.json), `${JSON.stringify(report, null, 2)}\n`);code = result.ok ? 0 : 1;++ // The gate is opt-in. A package over its limit is still a package — it built,+ // it runs, and whether that is a release-blocking fact belongs to the caller,+ // not to the exporter. Said in prose on stderr as well as in the exit code,+ // because a CI log that only goes red tells nobody which limit or by how much.+ const over = (result.size?.verdicts ?? []).filter((v) => v.status === 'over');+ for (const v of over) {+ const mb = (n) => `${(n / 1024 / 1024).toFixed(2)}MB`;+ console.error(`size budget: ${v.budget.scope} is ${mb(v.measuredBytes)}, over the `+ + `${mb(v.budget.maxBytes)} limit (${v.budget.note}).`);+ }+ if (over.length > 0 && opts['enforce-budget']) code = 1;} finally {// Before the exit, not after: process.exit() in the try block would skip this// and leave the bundle dir behind.diff --git a/desktop/tests/export-wechat.test.ts b/desktop/tests/export-wechat.test.tsindex 343ac848a..43c1d6fea 100644--- a/desktop/tests/export-wechat.test.ts+++ b/desktop/tests/export-wechat.test.ts@@ -160,6 +160,34 @@ describe('exportGame (wechat)', () => {expect(initial?.measuredBytes).toBe(res.size?.initialBytes);}, 60_000);+ // The project's own ceiling, all the way through: packaging.sizeBudget →+ // exportGame → the verdict a CI gate and the build dialog both read. The+ // headless exporter reads that field through `parseManifest`, so this covers+ // the half of the path that is not the parser's.+ it("judges against the project's own ceiling when it declares one", async () => {+ const outWx = path.join(root, 'dist-wechat-budget');+ const res = await exportGame({+ root,+ entryScene: 'scenes/main.esscene',+ gameHostEntry: 'unused-for-wechat',+ scriptsEntry: 'src/main.ts',+ sdkDistDir: path.join(root, '_sdk'),+ wasmDir: path.join(root, '_wxwasm'),+ outDir: outWx,+ platform: 'wechat',+ sizeBudgetBytes: 1024, // this fixture is a few KB — deliberately over+ });+ expect(res.ok).toBe(true);+ const initial = res.size?.verdicts.find((v) => v.budget.scope === 'initial');+ expect(initial?.status).toBe('over');+ expect(initial?.budget.maxBytes).toBe(1024);+ // Attributed to the project, not quoted as a WeChat rule that never said this.+ expect(initial?.budget.note).toBe('project budget');+ // Replacing the main-package limit is not a waiver of the other one.+ expect(res.size?.verdicts.find((v) => v.budget.scope === 'total')?.budget.maxBytes)+ .toBe(20 * 1024 * 1024);+ }, 60_000);+it('fails fast when the -t wechat engine runtime is missing', async () => {const outMissing = path.join(root, 'dist-wechat-missing');const res = await exportGame({