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(desktop): ship the SDK .d.ts so a packaged editor can stage project types

yhhb34d4aecd713
diff --git a/desktop/electron-builder.yml b/desktop/electron-builder.yml
index 6c73c1aae..4d5327a18 100644
--- a/desktop/electron-builder.yml
+++ b/desktop/electron-builder.yml
@@ -40,6 +40,18 @@ asarUnpack:
extraResources:
- from: ../build-tools/basis
to: build-tools/basis
+ # The SDK's TypeScript declarations, shipped as a real resource so a project's
+ # `.esengine/sdk` types mirror can be staged on open (see electron/main.ts →
+ # syncSdkTypes). electron-builder strips *.d.ts from node_modules by default
+ # (app-builder-lib excludedExts), so node_modules/esengine/dist carries the
+ # SDK's .js — used by the play runtime and esbuild aliasing — but NONE of its
+ # .d.ts; without this the IDE cannot resolve `esengine` in a shipped build
+ # (the real root cause behind issue #49's staging error). SDK dist is built
+ # before packaging (release-desktop.yml "Build SDK").
+ - from: ../sdk/dist
+ to: sdk-types
+ filter:
+ - "**/*.d.ts"
# New-project templates: the bundled blank-slate starter(s) plus the sample
# projects, staged as real directories (createFromTemplate copies a template
# with fs.cp, which cannot source from inside the asar). The launcher reads
diff --git a/desktop/electron/main.ts b/desktop/electron/main.ts
index 8ce04df5f..e05313906 100644
--- a/desktop/electron/main.ts
+++ b/desktop/electron/main.ts
@@ -110,13 +110,21 @@ const unpacked = (p: string): string => p.replace(/app\.asar(?=[\\/])/, 'app.asa
const HOSTS_DIR = unpacked(path.join(__dirname, 'hosts'));
/** The esengine SDK dist: staged into realms/exports, aliased into inlined bundles. */
const SDK_DIST = unpacked(path.join(process.env.APP_ROOT, 'node_modules', 'esengine', 'dist'));
-/** Where the types mirror may READ the SDK dist from, in preference order. The
- * mirror is plain Node fs, which reads app.asar fine — so the in-archive path
- * is a valid fallback when the unpacked copy is missing (v0.22.0 shipped
- * without asarUnpack for the sdk; the mirror silently skipped and projects
- * lost their `esengine` types — issue #49). Native consumers (esbuild) must
- * keep using SDK_DIST only. */
-const SDK_TYPES_CANDIDATES = [SDK_DIST, path.join(process.env.APP_ROOT, 'node_modules', 'esengine', 'dist')];
+/** Where the types mirror may READ the SDK dist's `.d.ts` from, in preference
+ * order. Packaged first: electron-builder strips *.d.ts from node_modules
+ * (excludedExts), so the SDK_DIST copy ships the SDK's .js but none of its
+ * declarations — the declarations ride along as an extraResource under
+ * resources/sdk-types instead (see electron-builder.yml). Dev falls through to
+ * the real node_modules dist (which has both). The mirror is plain Node fs, so
+ * the in-asar path stays as a last-ditch fallback. Native consumers (esbuild)
+ * must keep using SDK_DIST only — types never travel that path. Shipping
+ * without the declarations is how projects lost their `esengine` types with
+ * only a staging error to show for it (issue #49). */
+const SDK_TYPES_CANDIDATES = [
+ ...(app.isPackaged ? [path.join(process.resourcesPath, 'sdk-types')] : []),
+ SDK_DIST,
+ path.join(process.env.APP_ROOT, 'node_modules', 'esengine', 'dist'),
+];
/** The web engine runtime (glue + wasm + side modules) staged into play realms and
* exports by recursive directory copy — which cannot source from inside app.asar. */
const WEB_WASM_DIR = unpacked(
diff --git a/desktop/tests/sdk-types-packaging.test.ts b/desktop/tests/sdk-types-packaging.test.ts
new file mode 100644
index 000000000..efd36f1f6
--- /dev/null
+++ b/desktop/tests/sdk-types-packaging.test.ts
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file Guards the packaging contract that ships the SDK's TypeScript
+ * declarations. electron-builder strips *.d.ts from node_modules by
+ * default (app-builder-lib excludedExts), so the node_modules/esengine
+ * copy carries the SDK's .js but NONE of its .d.ts. v0.23.0 shipped
+ * exactly that: a fresh project could not resolve `esengine` in the IDE
+ * and every open logged "SDK dist not found". The declarations must ride
+ * along as an explicit extraResource, and main.ts must read them.
+ */
+import { describe, it, expect } from 'vitest';
+import { existsSync, readFileSync } from 'node:fs';
+import path from 'node:path';
+
+const DESKTOP_ROOT = path.resolve(__dirname, '..');
+const read = (rel: string): string => readFileSync(path.join(DESKTOP_ROOT, rel), 'utf8');
+
+describe('SDK types packaging contract', () => {
+ it('electron-builder stages the SDK .d.ts as a real resource (not via node_modules)', () => {
+ const yml = read('electron-builder.yml');
+ // The declarations come from the built SDK dist and land under
+ // resources/sdk-types, filtered to declarations only. Assert each leg so a
+ // partial edit (e.g. dropping the filter) still trips this guard.
+ expect(yml).toContain('../sdk/dist');
+ expect(yml).toContain('sdk-types');
+ expect(yml).toContain('**/*.d.ts');
+ });
+
+ it('main.ts reads the shipped sdk-types resource for the types mirror', () => {
+ const main = read('electron/main.ts');
+ // The packaged candidate must be the resources/sdk-types dir — the
+ // node_modules dist (SDK_DIST) has no declarations in a shipped build.
+ expect(main).toMatch(/SDK_TYPES_CANDIDATES\s*=/);
+ expect(main).toMatch(/process\.resourcesPath,\s*['"]sdk-types['"]/);
+ });
+
+ it('the built SDK dist actually carries declarations to ship', () => {
+ // sdk/dist is a build artifact (gitignored); CI builds it before these
+ // tests. When present, it must hold the declaration tree the mirror needs —
+ // a JS-only dist is the failure mode this whole guard exists to catch.
+ const dist = path.resolve(DESKTOP_ROOT, '..', 'sdk', 'dist');
+ if (!existsSync(dist)) return;
+ expect(existsSync(path.join(dist, 'index.d.ts'))).toBe(true);
+ });
+});