@silkweaver/build 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/build.d.ts CHANGED
@@ -15,6 +15,16 @@ import type { project_file as project_data } from '@silkweaver/project';
15
15
  export type { project_data };
16
16
  /** Reads and parses a project's project.json. */
17
17
  export declare function read_project(project_folder: string): Promise<project_data>;
18
+ /** Public: the engine version this toolchain ships — what "Update engine" would pin a project to. */
19
+ export declare function toolchain_engine_version(): string;
20
+ /**
21
+ * Vendors the toolchain's current engine into a project as one self-contained bundle
22
+ * (`.engine/engine.mjs`, matter-js inlined) and records its version (`.engine/version.json` +
23
+ * project.json `engineVersion`). Called at project creation; safe to re-run to upgrade the pin.
24
+ * @param project_folder - Absolute path to the project folder
25
+ * @returns The vendored engine version.
26
+ */
27
+ export declare function vendor_engine(project_folder: string): Promise<string>;
18
28
  /**
19
29
  * Builds the game to a single bundled game.js at out_path, for the in-IDE preview.
20
30
  * @param out_path - Where to write game.js (the caller decides — e.g. exports/game.js)
package/dist/build.js CHANGED
@@ -47,6 +47,8 @@ var __importStar = (this && this.__importStar) || (function () {
47
47
  })();
48
48
  Object.defineProperty(exports, "__esModule", { value: true });
49
49
  exports.read_project = read_project;
50
+ exports.toolchain_engine_version = toolchain_engine_version;
51
+ exports.vendor_engine = vendor_engine;
50
52
  exports.build_preview = build_preview;
51
53
  exports.export_html5 = export_html5;
52
54
  exports.export_executable = export_executable;
@@ -65,18 +67,73 @@ async function read_project(project_folder) {
65
67
  * entry can import the whole API (keeping it in sync with the IDE's autocomplete).
66
68
  * esbuild tree-shakes whatever the game doesn't actually use.
67
69
  */
68
- let _engine_names = null;
70
+ const _engine_names = new Map(); // engine path → its export names (per vendored engine)
69
71
  async function engine_export_names(engine_path) {
70
- if (_engine_names)
71
- return _engine_names;
72
+ const cached = _engine_names.get(engine_path);
73
+ if (cached)
74
+ return cached;
72
75
  const mod = await import((0, node_url_1.pathToFileURL)(engine_path).href);
73
- _engine_names = Object.keys(mod).filter(n => n !== 'default' && /^[A-Za-z_$][\w$]*$/.test(n));
74
- return _engine_names;
76
+ const names = Object.keys(mod).filter(n => n !== 'default' && /^[A-Za-z_$][\w$]*$/.test(n));
77
+ _engine_names.set(engine_path, names);
78
+ return names;
75
79
  }
76
- /** Absolute path to the engine package entry the esbuild alias target + export-name source. */
80
+ /** Absolute path to the toolchain's own engine entry (the vendoring source + pre-vendoring fallback). */
77
81
  function engine_entry() {
78
82
  return require.resolve('@silkweaver/engine');
79
83
  }
84
+ /** The version of the toolchain's own engine (what a freshly-vendored project pins). */
85
+ function engine_version() {
86
+ try {
87
+ return JSON.parse(fs.readFileSync(path.join(path.dirname(engine_entry()), '..', 'package.json'), 'utf8')).version;
88
+ }
89
+ catch {
90
+ return '0.0.0';
91
+ }
92
+ }
93
+ /** Public: the engine version this toolchain ships — what "Update engine" would pin a project to. */
94
+ function toolchain_engine_version() {
95
+ return engine_version();
96
+ }
97
+ /**
98
+ * Resolves the engine a project builds against: its vendored copy (`.engine/engine.mjs`) when
99
+ * present, else the toolchain's own engine (projects created before per-project vendoring). This is
100
+ * what lets a project keep building against the exact engine it was made with — IDE updates don't
101
+ * touch it.
102
+ */
103
+ function resolve_engine(project_folder) {
104
+ const vendored = path.join(project_folder, '.engine', 'engine.mjs');
105
+ return fs.existsSync(vendored) ? vendored : engine_entry();
106
+ }
107
+ /**
108
+ * Vendors the toolchain's current engine into a project as one self-contained bundle
109
+ * (`.engine/engine.mjs`, matter-js inlined) and records its version (`.engine/version.json` +
110
+ * project.json `engineVersion`). Called at project creation; safe to re-run to upgrade the pin.
111
+ * @param project_folder - Absolute path to the project folder
112
+ * @returns The vendored engine version.
113
+ */
114
+ async function vendor_engine(project_folder) {
115
+ const version = engine_version();
116
+ const dir = path.join(project_folder, '.engine');
117
+ await fs.promises.mkdir(dir, { recursive: true });
118
+ const esbuild_api = require('esbuild');
119
+ await esbuild_api.build({
120
+ entryPoints: [engine_entry()],
121
+ bundle: true,
122
+ format: 'esm',
123
+ outfile: path.join(dir, 'engine.mjs'),
124
+ keepNames: true, // the engine reads constructor.name at runtime
125
+ });
126
+ await fs.promises.writeFile(path.join(dir, 'version.json'), JSON.stringify({ version, vendoredAt: new Date().toISOString() }, null, 2) + '\n', 'utf8');
127
+ // Pin the version in project.json so the IDE can display it / gate features on it.
128
+ try {
129
+ const proj_path = path.join(project_folder, 'project.json');
130
+ const proj = JSON.parse(await fs.promises.readFile(proj_path, 'utf8'));
131
+ proj.engineVersion = version;
132
+ await fs.promises.writeFile(proj_path, JSON.stringify(proj, null, 2) + '\n', 'utf8');
133
+ }
134
+ catch { /* no project.json yet — caller sets engineVersion */ }
135
+ return version;
136
+ }
80
137
  /**
81
138
  * Strips leading `import …` lines from a code snippet so it can be inlined inside a
82
139
  * function body (timeline moments). Engine references auto-resolve via the inject shim,
@@ -101,7 +158,7 @@ function hex_to_bgr(hex) {
101
158
  * Generates the bootstrapper (_entry.ts) source for a project.
102
159
  * @param asset_mode - 'preview' (file:// into project) or 'export' (relative assets/)
103
160
  */
104
- async function generate_entry_code(project_folder, proj, asset_mode) {
161
+ async function generate_entry_code(project_folder, proj, asset_mode, engine_path) {
105
162
  // Parent-before-child so generated imports/registrations init objects in dependency order.
106
163
  const object_names = await _objects_parent_first(project_folder, Object.keys(proj.resources.objects ?? {}));
107
164
  const room_names = Object.keys(proj.resources.rooms ?? {});
@@ -110,8 +167,7 @@ async function generate_entry_code(project_folder, proj, asset_mode) {
110
167
  const font_names = Object.keys(proj.resources.fonts ?? {});
111
168
  const path_names = Object.keys(proj.resources.paths ?? {});
112
169
  const timeline_names = Object.keys(proj.resources.timelines ?? {});
113
- // The engine is resolved from the @silkweaver/engine package (no path coupling).
114
- const engine_path = engine_entry();
170
+ // engine_path is whatever the project resolves to its vendored copy, or the toolchain's.
115
171
  // Each object is a single class file: objects/<name>.ts (a gm_object subclass).
116
172
  // It is imported as-is; metadata/variables/events all live in the class.
117
173
  const object_imports = [];
@@ -492,7 +548,8 @@ ${room_physics[start_room] ? ` physics_world_create(${room_physics[start_room
492
548
  * ESM game.js at out_path via esbuild, then removes the temporary file.
493
549
  */
494
550
  async function bundle_game(project_folder, proj, asset_mode, out_path, minify) {
495
- const entry_code = await generate_entry_code(project_folder, proj, asset_mode);
551
+ const engine_path = resolve_engine(project_folder);
552
+ const entry_code = await generate_entry_code(project_folder, proj, asset_mode, engine_path);
496
553
  const entry_path = path.join(project_folder, '_entry.ts');
497
554
  const globals_path = path.join(project_folder, '_engine_globals.ts');
498
555
  await fs.promises.writeFile(entry_path, entry_code, 'utf8');
@@ -500,7 +557,7 @@ async function bundle_game(project_folder, proj, asset_mode, out_path, minify) {
500
557
  // `inject`, it makes any *bare* engine reference in an object/script file (e.g. `gm_object`,
501
558
  // `draw_sprite`) resolve to an auto-injected import — tree-shaken — so users never write or
502
559
  // manage `import … from '@silkweaver/engine'` themselves.
503
- const engine_names = await engine_export_names(engine_entry());
560
+ const engine_names = await engine_export_names(engine_path);
504
561
  // Also re-export the project's OBJECT classes by name, so they can be referenced bare —
505
562
  // GMS-style, e.g. `place_meeting(x, y, obj_wall)` or `static parent = par_solid` — with no
506
563
  // import (matching what the IDE editor already declares). The defining file won't self-import;
@@ -533,7 +590,7 @@ async function bundle_game(project_folder, proj, asset_mode, out_path, minify) {
533
590
  // `this.constructor.name` (resource.name, room type checks, object_get_name),
534
591
  // so identifier mangling would otherwise break exported (minified) games.
535
592
  keepNames: true,
536
- alias: { '@silkweaver/engine': engine_entry() },
593
+ alias: { '@silkweaver/engine': engine_path },
537
594
  inject: [globals_path],
538
595
  });
539
596
  }
package/dist/templates.js CHANGED
@@ -45,6 +45,7 @@ exports.list_templates = list_templates;
45
45
  exports.create_from_template = create_from_template;
46
46
  const fs = __importStar(require("node:fs"));
47
47
  const path = __importStar(require("node:path"));
48
+ const build_js_1 = require("./build.js");
48
49
  /** Ordered registry of the bundled starter templates (folders live under ../templates/<id>). */
49
50
  const TEMPLATE_REGISTRY = [
50
51
  { id: 'empty', label: 'Empty', description: 'A blank project with a single empty room.' },
@@ -56,7 +57,7 @@ function templates_dir() {
56
57
  return path.join(__dirname, '..', 'templates');
57
58
  }
58
59
  /** Names never copied into a new project (build artifacts / VCS / deps), as a defensive guard. */
59
- const COPY_DENYLIST = new Set(['_entry.ts', '_engine_globals.ts', 'node_modules', '.git', 'exports', 'game.js']);
60
+ const COPY_DENYLIST = new Set(['_entry.ts', '_engine_globals.ts', 'node_modules', '.git', 'exports', 'game.js', '.engine']);
60
61
  /** Returns the available starter templates — only those whose folder is actually installed. */
61
62
  function list_templates() {
62
63
  const out = [];
@@ -103,4 +104,11 @@ async function create_from_template(template_id, dest_folder, name) {
103
104
  }
104
105
  catch { /* keep the template's bundled name */ }
105
106
  }
107
+ // Vendor the current engine into the new project so it's pinned to this version (a later IDE
108
+ // update won't change it). Best-effort: if it fails, the project still builds against the
109
+ // toolchain engine via the resolve_engine fallback.
110
+ try {
111
+ await (0, build_js_1.vendor_engine)(dest_folder);
112
+ }
113
+ catch { /* falls back to the toolchain engine */ }
106
114
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@silkweaver/build",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Silkweaver toolchain — compiles a project folder into a runnable game (HTML5 / desktop executable). Usable from a CLI or the IDE.",
5
5
  "type": "commonjs",
6
6
  "license": "GPL-3.0",