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.

build(sdk): the build cleans dist, and says which chunks each entry needs

esengine386263efd88e
diff --git a/sdk/package.json b/sdk/package.json
index 3fc8f61b5..7f9edc8a8 100644
--- a/sdk/package.json
+++ b/sdk/package.json
@@ -69,7 +69,7 @@
"dist"
],
"scripts": {
- "build": "rolldown -c && rollup -c",
+ "build": "pnpm clean && rolldown -c && rollup -c",
"watch": "rolldown -c -w",
"clean": "rimraf dist",
"api:check": "node ../tools/api-surface.mjs --check",
diff --git a/sdk/rolldown.config.js b/sdk/rolldown.config.js
index ff8fb91e9..ab21c646f 100644
--- a/sdk/rolldown.config.js
+++ b/sdk/rolldown.config.js
@@ -40,6 +40,35 @@ function preserveViteIgnore() {
};
}
+// One code-split graph feeds eleven entries, so `dist/shared/` is their union and
+// a packager copying the directory hands a web player the mini-game runtime.
+// Which chunk belongs to which entry is the bundler's to say, so it says it here.
+function emitChunkManifest() {
+ return {
+ name: 'emit-chunk-manifest',
+ generateBundle(_options, bundle) {
+ const chunks = new Map(
+ Object.values(bundle).filter((c) => c.type === 'chunk').map((c) => [c.fileName, c]),
+ );
+ const reach = (entry) => {
+ const seen = new Set();
+ const queue = [entry];
+ while (queue.length > 0) {
+ const name = queue.pop();
+ if (seen.has(name) || !chunks.has(name)) continue;
+ seen.add(name);
+ queue.push(...chunks.get(name).imports, ...chunks.get(name).dynamicImports);
+ }
+ seen.delete(entry);
+ return [...seen].sort();
+ };
+ const manifest = {};
+ for (const [fileName, chunk] of chunks) if (chunk.isEntry) manifest[fileName] = reach(fileName);
+ this.emitFile({ type: 'asset', fileName: 'chunks.json', source: `${JSON.stringify(manifest, null, 2)}\n` });
+ },
+ };
+}
+
const minify = { compress: true, mangle: true, codegen: { removeWhitespace: true, legalComments: 'inline' } };
export default defineConfig([
@@ -69,7 +98,7 @@ export default defineConfig([
chunkFileNames: 'shared/[name].js',
minify,
},
- plugins: [preserveViteIgnore()],
+ plugins: [preserveViteIgnore(), emitChunkManifest()],
treeshake,
},
{
diff --git a/tools/check-sdk-chunk-manifest.mjs b/tools/check-sdk-chunk-manifest.mjs
new file mode 100644
index 000000000..7c1a22be6
--- /dev/null
+++ b/tools/check-sdk-chunk-manifest.mjs
@@ -0,0 +1,116 @@
+#!/usr/bin/env node
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file The chunk manifest says the same thing the emitted bytes do.
+ *
+ * Eleven SDK entries share one code-split graph, so `dist/shared/` is their
+ * union — a web package that copies the directory carries the WeChat and native
+ * runtimes too. `dist/chunks.json` is the bundler's record of which chunks each
+ * entry reaches, and the exporter ships exactly that.
+ *
+ * Which makes a manifest that under-reports a chunk into a package missing a
+ * module, and the export layer once refused to walk the import graph for this
+ * very reason: exact, but silent when wrong. So it is not trusted on its own —
+ * this reads the emitted `import` statements back and the two answers have to
+ * agree. Two derivations of one fact, one from the bundler's metadata and one
+ * from the bytes it wrote.
+ *
+ * node tools/check-sdk-chunk-manifest.mjs
+ */
+import { readFileSync, existsSync, readdirSync, statSync } from 'node:fs';
+import path from 'node:path';
+import { fileURLToPath } from 'node:url';
+
+const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
+const DIST = path.join(ROOT, 'sdk/dist');
+const MANIFEST = path.join(DIST, 'chunks.json');
+
+if (!existsSync(MANIFEST)) {
+ console.error(`check-sdk-chunk-manifest: ${path.relative(ROOT, MANIFEST)} missing — `
+ + 'build the SDK (pnpm --filter ./sdk build).');
+ process.exit(1);
+}
+const manifest = JSON.parse(readFileSync(MANIFEST, 'utf8'));
+
+/**
+ * Every static specifier in an emitted chunk. Minified output writes `}from"x"`
+ * with no space, and a literal dynamic `import("x")` reaches a chunk just as a
+ * static one does.
+ */
+function specifiers(file) {
+ const src = readFileSync(file, 'utf8');
+ const found = new Set();
+ for (const re of [
+ /\b(?:import|export)\b[^;\n]*?\bfrom\s*['"]([^'"]+)['"]/g,
+ /\bimport\s*['"]([^'"]+)['"]/g,
+ /\bimport\s*\(\s*['"]([^'"]+)['"]\s*\)/g,
+ ]) {
+ let m;
+ while ((m = re.exec(src)) !== null) found.add(m[1]);
+ }
+ return [...found];
+}
+
+const rel = (abs) => path.relative(DIST, abs).split(path.sep).join('/');
+
+/** What `entry` reaches, read off the bytes rather than off the manifest. */
+function reached(entry) {
+ const seen = new Set();
+ const queue = [path.join(DIST, entry)];
+ const missing = [];
+ while (queue.length > 0) {
+ const file = queue.pop();
+ const name = rel(file);
+ if (seen.has(name)) continue;
+ if (!existsSync(file)) { missing.push(name); continue; }
+ seen.add(name);
+ for (const spec of specifiers(file)) {
+ if (spec.startsWith('.')) queue.push(path.resolve(path.dirname(file), spec));
+ }
+ }
+ seen.delete(entry);
+ return { seen, missing };
+}
+
+const problems = [];
+
+/** An entry the manifest forgot is an entry the exporter ships with no chunks. */
+const entries = readdirSync(DIST, { recursive: true })
+ .map((f) => String(f).split(path.sep).join('/'))
+ .filter((f) => /\.js$/.test(f) && !f.startsWith('shared/') && statSync(path.join(DIST, f)).isFile());
+
+for (const entry of Object.keys(manifest)) {
+ if (!existsSync(path.join(DIST, entry))) {
+ problems.push(`chunks.json names ${entry}, which dist does not have`);
+ continue;
+ }
+ const { seen, missing } = reached(entry);
+ for (const m of missing) problems.push(`${entry} imports ${m}, which dist does not have`);
+ const claimed = new Set(manifest[entry]);
+ for (const f of seen) {
+ if (!claimed.has(f)) {
+ problems.push(`${entry} reaches ${f} and chunks.json does not list it — `
+ + 'an export trusting the manifest ships a package missing that module');
+ }
+ }
+ for (const f of claimed) {
+ if (!seen.has(f)) problems.push(`chunks.json says ${entry} needs ${f}, and nothing in it imports that`);
+ }
+}
+
+// Entries that produce no chunk of their own still have to be listed, or the
+// exporter cannot tell "needs nothing" from "never built".
+const unlisted = entries.filter((f) => !(f in manifest) && !/\.bundled\.js$/.test(f)
+ && !/^index\.(node|wechat\.cjs)\.js$/.test(f) && f !== 'open-data.js');
+for (const f of unlisted) problems.push(`${f} is an entry in dist and chunks.json does not mention it`);
+
+if (problems.length > 0) {
+ for (const p of problems) console.error(` ${p}`);
+ console.error(`check-sdk-chunk-manifest: ${problems.length} disagreement(s) between the manifest and the bytes.`);
+ process.exit(1);
+}
+
+const chunks = new Set(Object.values(manifest).flat());
+console.log(`check-sdk-chunk-manifest: ${Object.keys(manifest).length} entr(ies) over ${chunks.size} shared chunk(s), `
+ + 'manifest and emitted imports agree.');
diff --git a/tools/gates.mjs b/tools/gates.mjs
index 1ad393430..b79497108 100644
--- a/tools/gates.mjs
+++ b/tools/gates.mjs
@@ -259,6 +259,9 @@ export const GATES = [
// 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' },
+ // The exporter ships a web package by the chunk list the SDK build writes, so
+ // a list that disagrees with the emitted imports is a package missing a module.
+ { id: 'sdk-chunk-manifest', run: 'node tools/check-sdk-chunk-manifest.mjs' },
// A packaging setting the manifest parser does not read is a setting that
// does nothing, in the editor and on a build server alike.
{ id: 'packaging-settings', run: 'node tools/check-packaging-settings.mjs' },