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(sdk): a host entry keeps the platform when the web one loads too

esengine7b649da01686
diff --git a/sdk/src/runtime/webEntry.ts b/sdk/src/runtime/webEntry.ts
index ca5d848c4..c2c820886 100644
--- a/sdk/src/runtime/webEntry.ts
+++ b/sdk/src/runtime/webEntry.ts
@@ -8,13 +8,18 @@
* this in a module the entries share and it lands in a chunk nobody whitelisted,
* where tree-shaking drops it. The editor booted to "Platform not initialized".
*/
-import { setPlatform, webAdapter } from '../platform';
+import { setPlatform, webAdapter, isPlatformInitialized } from '../platform';
import { ensureBuiltinComponentsRegistered, markEngineComponentBaseline } from '../ecs/component';
import { ensureBuiltinAiRegistrations } from '../ai/builtins';
-/** Idempotent: an entry that re-exports another still calls it exactly once. */
+/**
+ * Idempotent, and deferential about the platform: a host-specific entry
+ * (`esengine/node`) claims one outright, and one process can hold both — the
+ * build pipeline reads a project through modules a browser also runs. Whichever
+ * order they load in, the host that declared itself keeps the platform.
+ */
export function installWebEntry(): void {
- setPlatform(webAdapter);
+ if (!isPlatformInitialized()) setPlatform(webAdapter);
// Every engine component (COMPONENT_META) up front, so a scene can never
// silently drop one that exists in the engine but lacks a typed const.
ensureBuiltinComponentsRegistered();
diff --git a/tools/check-entry-boots.mjs b/tools/check-entry-boots.mjs
index e4406b36f..2ba06fb78 100644
--- a/tools/check-entry-boots.mjs
+++ b/tools/check-entry-boots.mjs
@@ -77,5 +77,26 @@ for (const entry of ENTRIES) {
results.push(`${entry.specifier} → ${got}`);
}
+// One process can hold two entries: a node tool reaches `esengine` through
+// modules a browser also runs. They share one core, so the host that declared
+// itself must keep the platform whichever order they arrive in.
+for (const [first, second] of [['esengine', 'esengine/node'], ['esengine/node', 'esengine']]) {
+ const src = path.join(work, `order-${first.replace(/\W+/g, '-')}.mjs`);
+ const out = `${src}.bundle.mjs`;
+ writeFileSync(src, `import ${JSON.stringify(first)};\n`
+ + `import { getPlatformType } from ${JSON.stringify(second)};\n`
+ + `console.log(getPlatformType());\n`);
+ await build({ entryPoints: [src], outfile: out, bundle: true, format: 'esm', platform: 'node', logLevel: 'silent' });
+ const run = spawnSync(process.execPath, [out], { encoding: 'utf8' });
+ const got = (run.stdout ?? '').trim();
+ if (got !== 'node') {
+ fail(`loading ${first} then ${second} left the platform as ${got || '(nothing)'}, not node.`
+ + '\n\nA host entry claims the platform outright; the web entry installs one only when'
+ + ' nobody has (runtime/webEntry.ts). A node process that ends up on the web adapter'
+ + ' reads files over fetch and throws on localStorage.');
+ }
+ results.push(`${first}+${second} → ${got}`);
+}
+
rmSync(work, { recursive: true, force: true, maxRetries: 10, retryDelay: 50 });
-console.log(`check-entry-boots: ${results.length} entry(ies) bundled and run — ${results.join(', ')}.`);
+console.log(`check-entry-boots: ${results.length} case(s) bundled and run — ${results.join(', ')}.`);