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(editor): packaged builds still could not read a project's declaration entry
esengine6d2a066c3f15
diff --git a/desktop/electron/extractSchemas.ts b/desktop/electron/extractSchemas.tsindex 6e808a5ea..538504bdb 100644--- a/desktop/electron/extractSchemas.ts+++ b/desktop/electron/extractSchemas.ts@@ -46,6 +46,9 @@ const OUTPUT = 'schemas.json';* esbuild — a native subprocess — cannot see into at all. That is why callers in* the app pass `sdkDir` (main.ts SDK_DIST, retargeted to the app.asar.unpacked* twin); the walk-up stays for tests and any consumer running from source.+ *+ * It is the plugin's anchor and nothing else's — never the generated entry's+ * `resolveDir`, which must be a directory esbuild can actually see (see below).*/const ANCHOR_DIR = path.dirname(fileURLToPath(import.meta.url));@@ -191,19 +194,29 @@ export async function extractProjectSchemas(// any project code), then defer the declaration's side effects to a dynamic// import inside __extract() and hand back what it declared.//+ // It imports the declaration RELATIVE to the project root, and is bundled with+ // `resolveDir` there — like every other generated entry here (exportPlayable /+ // exportMiniGame). esbuild resolves an import against the importer's directory+ // and REFUSES it — an absolute one included — when that directory does not+ // exist; the entry used to sit at ANCHOR_DIR, which packaged is inside+ // app.asar, so no shipped build could resolve the declaration entry at all.+ // A relative specifier also keeps the wrong resolveDir from ever passing+ // silently again: it resolves from the project or not at all.+ //// Actions come from the SAME module for the same reason components do: a// `registerAction` IS a declaration, and this is the one project module the// editor may evaluate. The registry is diffed around the import so only the// project's own names are reported — the engine's builtins register when their// plugins build, which never happens here, but a diff keeps that an invariant// rather than a coincidence.+ const declSpecifier = `./${path.relative(root, declPath).split(path.sep).join('/')}`;const entry =`import { AppContext, setDefaultContext, getUserComponents, aiRegistry } from 'esengine';\n` +`setDefaultContext(new AppContext());\n` +`export async function __extract() {\n` +` const before = new Set(aiRegistry.actionNames());\n` +` const beforeConds = new Set(aiRegistry.conditionNames());\n` +- ` await import(${JSON.stringify(declPath)});\n` ++ ` await import(${JSON.stringify(declSpecifier)});\n` +` const actions = aiRegistry.actionNames().filter((n) => !before.has(n)).map((name) => ({\n` +` name,\n` +` params: aiRegistry.getActionParams(name),\n` +@@ -219,7 +232,7 @@ export async function extractProjectSchemas(try {const { build } = await loadEsbuild();const result = await build({- stdin: { contents: entry, resolveDir: ANCHOR_DIR, loader: 'ts', sourcefile: 'extract-entry.ts' },+ stdin: { contents: entry, resolveDir: root, loader: 'ts', sourcefile: 'extract-entry.ts' },bundle: true,format: 'esm',platform: 'node',diff --git a/desktop/tests/extract-schemas.test.ts b/desktop/tests/extract-schemas.test.tsindex 7929b9909..ffc03bba6 100644--- a/desktop/tests/extract-schemas.test.ts+++ b/desktop/tests/extract-schemas.test.ts@@ -175,6 +175,34 @@ describe('extractProjectSchemas (P2)', () => {});});+ // esbuild resolves an import against the IMPORTER's directory, and refuses it+ // — an absolute one included — when that directory does not exist. The+ // generated entry used to be bundled at this package's own location, which+ // packaged is inside app.asar: invisible to the esbuild subprocess, so no+ // shipped build could resolve the declaration entry, and Add Component was+ // missing the project's components with the extractor reporting only+ // `Could not resolve <project>/src/components.ts`.+ describe('the directory it resolves the declaration from', () => {+ // The teeth: the specifier is relative to the PROJECT, so an entry rooted+ // anywhere else resolves nothing — here as much as in a shipped build.+ it('resolves an entry nested under the project root, from the project root', async () => {+ const proj = mkdtempSync(path.join(tmpdir(), 'estella-schema-nested-'));+ try {+ mkdirSync(path.join(proj, 'a', 'b'), { recursive: true });+ writeFileSync(+ path.join(proj, 'a', 'b', 'decls.ts'),+ `import { defineComponent } from 'esengine';\n` ++ `export const Deep = defineComponent('Deep', { n: 1 });\n`,+ );+ const res = await extractProjectSchemas(proj, { entry: 'a/b/decls.ts' });+ expect(res.errors).toEqual([]);+ expect(res.schemas.map((s) => s.name)).toEqual(['Deep']);+ } finally {+ rmSync(proj, { recursive: true, force: true });+ }+ });+ });+// The palettes can only offer a game's own action names if they reach the// editor as data — the main realm never runs project code.describe('the project\'s registered actions', () => {