@defold-typescript/transpiler 0.51.0 → 0.53.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/src/transpile.ts CHANGED
@@ -63,23 +63,27 @@ export interface TranspileProjectResult {
63
63
  // TSTL synthesizes this bundle (no user source) whenever a lualib feature is
64
64
  // registered — either by a user source using one (`Object.keys`, spread,
65
65
  // `__TS__TypeOf`, ...) or by the timers import lowering, whose generated
66
- // runtime requires the bundle. The emitted `require("lualib_bundle")` only
67
- // resolves in Defold if the CLI writes it to the output root. Absent when
66
+ // runtime requires the bundle. The emitted `require("lualib_bundle")` is an
67
+ // internal name the CLI respells to where it writes the bundle. Absent when
68
68
  // neither trigger fires.
69
69
  readonly lualib?: string;
70
70
  // Hand-authored runtime Lua for the `@defold-typescript/types/timers`
71
71
  // polyfills (`setTimeout`/`wait`/...). Unlike `defineScript`, the import is
72
- // not erased — it lowers to `require("defold_typescript_timers")`, so the CLI
73
- // must write this to the output root. Present only when a user file imports
74
- // the module (pay-for-use).
72
+ // not erased — it lowers to `require("defold_typescript_timers")`, an
73
+ // internal name the CLI respells to where it writes this. Present only when
74
+ // a user file imports the module (pay-for-use).
75
75
  readonly timersRuntime?: string;
76
76
  // The compiled `hsm` modules the program requires, keyed by module name
77
77
  // (`index`, `defold`, ...). An hsm import lowers to
78
- // `require("defold_typescript_hsm.<module>")`, so the CLI must write each one
79
- // under the output root. Present only when some chunk requires one
80
- // (pay-for-use); never carries a source map, since the `.ts` it would point
81
- // at is not in the user's project.
78
+ // `require("defold_typescript_hsm.<module>")`, an internal name the CLI
79
+ // respells to where it writes each one. Present only when some chunk requires one
80
+ // (pay-for-use).
82
81
  readonly hsmModules?: Readonly<Record<string, string>>;
82
+ // The source map of each module in `hsmModules`, under the same keys. Its
83
+ // `sources` names `<module>.ts` with an empty `sourceRoot`: only the CLI knows
84
+ // where the module is written and where the hsm source sits, so it sets the
85
+ // root that joins them.
86
+ readonly hsmSourceMaps?: Readonly<Record<string, string>>;
83
87
  }
84
88
 
85
89
  function flattenDiagnosticMessage(
@@ -313,6 +317,7 @@ export function collectOutputs(
313
317
  }
314
318
 
315
319
  const hsm = hsmClosure([...Object.values(lua), ...Object.values(companions)], compileHsmModules);
320
+ const hsmSourceMaps = hsm === undefined ? undefined : selectHsmSourceMaps(Object.keys(hsm));
316
321
 
317
322
  return {
318
323
  lua,
@@ -322,9 +327,22 @@ export function collectOutputs(
322
327
  ...(lualib !== undefined ? { lualib } : {}),
323
328
  ...(timersImported ? { timersRuntime: TIMERS_RUNTIME } : {}),
324
329
  ...(hsm !== undefined ? { hsmModules: hsm } : {}),
330
+ ...(hsmSourceMaps !== undefined ? { hsmSourceMaps } : {}),
325
331
  };
326
332
  }
327
333
 
334
+ function selectHsmSourceMaps(names: readonly string[]): Record<string, string> {
335
+ const maps = compileHsmSourceMaps();
336
+ const selected: Record<string, string> = {};
337
+ for (const name of names) {
338
+ const map = maps[name];
339
+ if (map !== undefined) {
340
+ selected[name] = map;
341
+ }
342
+ }
343
+ return selected;
344
+ }
345
+
328
346
  function transpileVirtual(
329
347
  files: Readonly<Record<string, string>>,
330
348
  companionEmit: ReturnType<typeof createCompanionEmitPlugin>,
@@ -382,7 +400,12 @@ export function transpileProject(input: TranspileProjectInput): TranspileProject
382
400
  );
383
401
  }
384
402
 
385
- let compiledHsm: Readonly<Record<string, string>> | undefined;
403
+ interface CompiledHsm {
404
+ readonly lua: Readonly<Record<string, string>>;
405
+ readonly sourceMaps: Readonly<Record<string, string>>;
406
+ }
407
+
408
+ let compiledHsm: CompiledHsm | undefined;
386
409
 
387
410
  function requiresLualib(lua: string): boolean {
388
411
  return scanEmittedRequires(lua).some(({ path: requirePath }) => requirePath === "lualib_bundle");
@@ -398,6 +421,15 @@ function requiresLualib(lua: string): boolean {
398
421
  * lualib bundle.
399
422
  */
400
423
  export function compileHsmModules(): Readonly<Record<string, string>> {
424
+ return compileHsm().lua;
425
+ }
426
+
427
+ /** The source map of every module `compileHsmModules` returns, under the same keys. */
428
+ export function compileHsmSourceMaps(): Readonly<Record<string, string>> {
429
+ return compileHsm().sourceMaps;
430
+ }
431
+
432
+ function compileHsm(): CompiledHsm {
401
433
  if (compiledHsm !== undefined) {
402
434
  return compiledHsm;
403
435
  }
@@ -417,6 +449,7 @@ export function compileHsmModules(): Readonly<Record<string, string>> {
417
449
  (d) => `${d.file?.fileName ?? "(project)"}: ${flattenDiagnosticMessage(d.messageText)}`,
418
450
  );
419
451
  const lua: Record<string, string> = {};
452
+ const sourceMaps: Record<string, string> = {};
420
453
  for (const file of result.transpiledFiles) {
421
454
  if (isLualibBundle(file)) {
422
455
  continue;
@@ -430,14 +463,17 @@ export function compileHsmModules(): Readonly<Record<string, string>> {
430
463
  problems.push(`the compiled ${name} module requires lualib_bundle`);
431
464
  }
432
465
  lua[name] = file.lua;
466
+ if (typeof file.luaSourceMap === "string") {
467
+ sourceMaps[name] = file.luaSourceMap;
468
+ }
433
469
  }
434
470
  if (problems.length > 0) {
435
471
  throw new Error(
436
472
  `@defold-typescript/transpiler: the hsm library does not compile:\n ${problems.join("\n ")}`,
437
473
  );
438
474
  }
439
- compiledHsm = lua;
440
- return lua;
475
+ compiledHsm = { lua, sourceMaps };
476
+ return compiledHsm;
441
477
  }
442
478
 
443
479
  export function transpile(source: string): TranspileResult {