@tilua/compiler 8.0.0 → 9.0.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/index.d.cts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as T from '@tilua/parser';
2
2
  import { OutputMode, TiluaConfig, ConfigProblem, LoweringPlugin, TypeAnalysis, ScopeAnalysis } from '@tilua/parser';
3
- export { LoweringPlugin, LuaSyntax, MethodCall, MethodLowering } from '@tilua/parser';
3
+ export { EntryExports, EntryExportsLowering, FilesAnswer, FilesLayout, LoweringPlugin, LuaSyntax, MethodCall, MethodLowering, PluginContext } from '@tilua/parser';
4
4
  import * as L from 'luau-parser';
5
5
 
6
6
  /** What `tilua.config.json` holds. */
@@ -15,13 +15,32 @@ interface TiluaConfigJson {
15
15
  sourceMap?: string | null;
16
16
  /** How many files the output is. `"single"` (the default) is one file;
17
17
  * `"chunks"` also writes a file for what only an `import()` reaches,
18
- * loaded when it is first asked for; `"modules"` writes one per module. */
18
+ * loaded when it is first asked for; `"files"` has no entry, and
19
+ * compiles every file under `rootDir` to one of its own under `outDir`. */
19
20
  output?: OutputMode;
21
+ /** With `"files"`, the folder whose files are compiled. Default: the
22
+ * config's folder. */
23
+ rootDir?: string;
24
+ /** With `"files"`, the folder the output goes in, laid out as `rootDir`
25
+ * is. Default: `rootDir`, each `.luau` beside its `.tilua`. */
26
+ outDir?: string;
20
27
  /** Leave out Lua's standard library — `print`, `string`, `pcall` — which
21
28
  * is otherwise there for every file, as JavaScript's is in TypeScript. */
22
29
  noLib?: boolean;
23
30
  /** Leave out of the bundle what the entry never reaches (`treeShake`). */
24
31
  treeShaking?: boolean;
32
+ /** Write the map from the bundle's lines back to the project's, which
33
+ * error reports and `console` read. Default: true. */
34
+ lineMap?: boolean;
35
+ /** Indent the output, a block level at a time: `"\t"` or a number of
36
+ * spaces. Default: no indentation. */
37
+ indent?: "\t" | number;
38
+ /** Write the output on one line, with as few `;` as it can take, and
39
+ * without the line map. Default: false. */
40
+ minify?: boolean;
41
+ /** The options a type library in `types` declares, such as
42
+ * `@tilua-types/luau`'s `typedExports`. */
43
+ [option: string]: unknown;
25
44
  }
26
45
  /** A config file's path, or the config itself. Relative paths — to a config
27
46
  * file, and inside an inline config — resolve from `cwd`. */
@@ -35,8 +54,9 @@ interface ResolvedConfig {
35
54
  declare function resolveConfig(input: ConfigInput | undefined, entry: string, cwd?: string): ResolvedConfig;
36
55
 
37
56
  interface BundleOptions {
38
- /** The file the bundle runs. */
39
- readonly entry: string;
57
+ /** The file the bundle runs. Needed unless the output is `"files"`,
58
+ * which has no entry: every file under the config's `rootDir` is one. */
59
+ readonly entry?: string;
40
60
  /** A `tilua.config.json` path or contents. Default: the nearest config above the entry. */
41
61
  readonly config?: ConfigInput;
42
62
  /** The project folder. Modules are named by their path from it, and an
@@ -46,16 +66,36 @@ interface BundleOptions {
46
66
  /** Check types against the config's `types` too. Default: true. */
47
67
  readonly typeCheck?: boolean;
48
68
  /** How many files to make. Overrides the config's `output`; `"single"`
49
- * without either. */
69
+ * without either. With `"files"`, every file under the config's
70
+ * `rootDir` is compiled to a file of its own, its path from `rootDir`. */
50
71
  readonly output?: OutputMode;
51
72
  /** Leave out what the entry never reaches. Overrides the config's
52
73
  * `treeShaking`; off without either. */
53
74
  readonly treeShaking?: boolean;
75
+ /** Write the map from the bundle's lines back to the project's. Overrides
76
+ * the config's `lineMap`; on without either. Without it an error names
77
+ * the bundle's lines, and the entry does not run under `xpcall`. */
78
+ readonly lineMap?: boolean;
79
+ /** Indent the output: `"\t"` or a number of spaces. Overrides the
80
+ * config's `indent`; no indentation without either. */
81
+ readonly indent?: "\t" | number;
82
+ /** Write the output on one line, with as few `;` as it can take — and so
83
+ * without the line map, which one line cannot have. Overrides the
84
+ * config's `minify`; off without either. */
85
+ readonly minify?: boolean;
86
+ /** With `"files"`, the folder the output is written to — where a host's
87
+ * layout (a Rojo project) finds it. Overrides the config's `outDir`;
88
+ * `rootDir` without either. */
89
+ readonly outDir?: string;
54
90
  /** What the entry's file is called: `main.luau`. The other files of
55
91
  * `"chunks"` are named after it (`main_shop.luau`), and every file takes
56
92
  * its extension. Default: the entry's own name, with `.luau`. */
57
93
  readonly outFile?: string;
58
94
  }
95
+ /** How the last line of every file `"files"` writes starts: what says a
96
+ * file is tilua's to replace or remove, and not one written by hand. On the
97
+ * last line, so no line the line map counts moves. */
98
+ declare const WRITTEN_BY_TILUA = "-- Written by tilua";
59
99
  /** One file of the output. */
60
100
  interface BundleFile {
61
101
  /** Where it goes, from the folder the output is written to: `main.luau`,
@@ -81,9 +121,9 @@ interface BundleDiagnostic {
81
121
  readonly severity?: "warning";
82
122
  }
83
123
  interface BundleResult {
84
- /** The bundle — the entry's file, when there are several — or `undefined`
85
- * when a module could not be compiled. Type errors are reported but do
86
- * not stop it. */
124
+ /** The bundle — the entry's file, when there are several, and the first
125
+ * module's with `"files"` — or `undefined` when a module could not be
126
+ * compiled. Type errors are reported but do not stop it. */
87
127
  readonly code?: string;
88
128
  /** Every file of the output, the entry's first. Empty when there is no
89
129
  * bundle. */
@@ -138,6 +178,9 @@ interface LoadedLowering {
138
178
  * analyzer was given: a method read from a metatable they declare is
139
179
  * this lowering's to answer. Absent for the language's own. */
140
180
  readonly library?: number;
181
+ /** The library's own options, as the config sets them. None for the
182
+ * language's own. */
183
+ readonly options?: Readonly<Record<string, boolean | number | string | undefined>>;
141
184
  }
142
185
  interface LoweringProblem {
143
186
  readonly file: string;
@@ -150,6 +193,7 @@ declare function loadLowerings(modules: readonly {
150
193
  file: string;
151
194
  from: string;
152
195
  library?: number;
196
+ options?: LoadedLowering["options"];
153
197
  }[]): Promise<{
154
198
  lowerings: LoadedLowering[];
155
199
  problems: LoweringProblem[];
@@ -202,6 +246,12 @@ interface ModuleContext {
202
246
  /** What `import("./x")` calls, with the module's name: the bundle's
203
247
  * wrapper around its `require`. Default: `require` itself. */
204
248
  readonly import?: L.Expression;
249
+ /** Where a module is a file of its own: a function, passed after its
250
+ * name, that loads the file it is in — `function() return
251
+ * require("./shape") end`, with the path written out, for the tools that
252
+ * follow a `require`. `require` calls it only if the module is not
253
+ * already there. */
254
+ loader?(name: string): L.Expression | undefined;
205
255
  /** Luau reaching the bundle's line map — what a library's runtime reads,
206
256
  * as `__LINES__`, to name the file a frame was written in. */
207
257
  readonly lines?: string;
@@ -336,4 +386,4 @@ interface ModuleInfo {
336
386
  }
337
387
  declare function lower(program: T.Program, scopes: ScopeAnalysis, options?: LowerOptions): LowerResult;
338
388
 
339
- export { type BundleDiagnostic, type BundleFile, type BundleOptions, type BundleResult, type CompileResult, type ConfigInput, type Diagnostic, type LoadedLowering, type LowerDiagnostic, type LowerOptions, type LowerResult, type LoweringProblem, type ModuleContext, type ResolvedConfig, type TiluaConfigJson, bundle, compile, loadLowerings, lower, resolveConfig };
389
+ export { type BundleDiagnostic, type BundleFile, type BundleOptions, type BundleResult, type CompileResult, type ConfigInput, type Diagnostic, type LoadedLowering, type LowerDiagnostic, type LowerOptions, type LowerResult, type LoweringProblem, type ModuleContext, type ResolvedConfig, type TiluaConfigJson, WRITTEN_BY_TILUA, bundle, compile, loadLowerings, lower, resolveConfig };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as T from '@tilua/parser';
2
2
  import { OutputMode, TiluaConfig, ConfigProblem, LoweringPlugin, TypeAnalysis, ScopeAnalysis } from '@tilua/parser';
3
- export { LoweringPlugin, LuaSyntax, MethodCall, MethodLowering } from '@tilua/parser';
3
+ export { EntryExports, EntryExportsLowering, FilesAnswer, FilesLayout, LoweringPlugin, LuaSyntax, MethodCall, MethodLowering, PluginContext } from '@tilua/parser';
4
4
  import * as L from 'luau-parser';
5
5
 
6
6
  /** What `tilua.config.json` holds. */
@@ -15,13 +15,32 @@ interface TiluaConfigJson {
15
15
  sourceMap?: string | null;
16
16
  /** How many files the output is. `"single"` (the default) is one file;
17
17
  * `"chunks"` also writes a file for what only an `import()` reaches,
18
- * loaded when it is first asked for; `"modules"` writes one per module. */
18
+ * loaded when it is first asked for; `"files"` has no entry, and
19
+ * compiles every file under `rootDir` to one of its own under `outDir`. */
19
20
  output?: OutputMode;
21
+ /** With `"files"`, the folder whose files are compiled. Default: the
22
+ * config's folder. */
23
+ rootDir?: string;
24
+ /** With `"files"`, the folder the output goes in, laid out as `rootDir`
25
+ * is. Default: `rootDir`, each `.luau` beside its `.tilua`. */
26
+ outDir?: string;
20
27
  /** Leave out Lua's standard library — `print`, `string`, `pcall` — which
21
28
  * is otherwise there for every file, as JavaScript's is in TypeScript. */
22
29
  noLib?: boolean;
23
30
  /** Leave out of the bundle what the entry never reaches (`treeShake`). */
24
31
  treeShaking?: boolean;
32
+ /** Write the map from the bundle's lines back to the project's, which
33
+ * error reports and `console` read. Default: true. */
34
+ lineMap?: boolean;
35
+ /** Indent the output, a block level at a time: `"\t"` or a number of
36
+ * spaces. Default: no indentation. */
37
+ indent?: "\t" | number;
38
+ /** Write the output on one line, with as few `;` as it can take, and
39
+ * without the line map. Default: false. */
40
+ minify?: boolean;
41
+ /** The options a type library in `types` declares, such as
42
+ * `@tilua-types/luau`'s `typedExports`. */
43
+ [option: string]: unknown;
25
44
  }
26
45
  /** A config file's path, or the config itself. Relative paths — to a config
27
46
  * file, and inside an inline config — resolve from `cwd`. */
@@ -35,8 +54,9 @@ interface ResolvedConfig {
35
54
  declare function resolveConfig(input: ConfigInput | undefined, entry: string, cwd?: string): ResolvedConfig;
36
55
 
37
56
  interface BundleOptions {
38
- /** The file the bundle runs. */
39
- readonly entry: string;
57
+ /** The file the bundle runs. Needed unless the output is `"files"`,
58
+ * which has no entry: every file under the config's `rootDir` is one. */
59
+ readonly entry?: string;
40
60
  /** A `tilua.config.json` path or contents. Default: the nearest config above the entry. */
41
61
  readonly config?: ConfigInput;
42
62
  /** The project folder. Modules are named by their path from it, and an
@@ -46,16 +66,36 @@ interface BundleOptions {
46
66
  /** Check types against the config's `types` too. Default: true. */
47
67
  readonly typeCheck?: boolean;
48
68
  /** How many files to make. Overrides the config's `output`; `"single"`
49
- * without either. */
69
+ * without either. With `"files"`, every file under the config's
70
+ * `rootDir` is compiled to a file of its own, its path from `rootDir`. */
50
71
  readonly output?: OutputMode;
51
72
  /** Leave out what the entry never reaches. Overrides the config's
52
73
  * `treeShaking`; off without either. */
53
74
  readonly treeShaking?: boolean;
75
+ /** Write the map from the bundle's lines back to the project's. Overrides
76
+ * the config's `lineMap`; on without either. Without it an error names
77
+ * the bundle's lines, and the entry does not run under `xpcall`. */
78
+ readonly lineMap?: boolean;
79
+ /** Indent the output: `"\t"` or a number of spaces. Overrides the
80
+ * config's `indent`; no indentation without either. */
81
+ readonly indent?: "\t" | number;
82
+ /** Write the output on one line, with as few `;` as it can take — and so
83
+ * without the line map, which one line cannot have. Overrides the
84
+ * config's `minify`; off without either. */
85
+ readonly minify?: boolean;
86
+ /** With `"files"`, the folder the output is written to — where a host's
87
+ * layout (a Rojo project) finds it. Overrides the config's `outDir`;
88
+ * `rootDir` without either. */
89
+ readonly outDir?: string;
54
90
  /** What the entry's file is called: `main.luau`. The other files of
55
91
  * `"chunks"` are named after it (`main_shop.luau`), and every file takes
56
92
  * its extension. Default: the entry's own name, with `.luau`. */
57
93
  readonly outFile?: string;
58
94
  }
95
+ /** How the last line of every file `"files"` writes starts: what says a
96
+ * file is tilua's to replace or remove, and not one written by hand. On the
97
+ * last line, so no line the line map counts moves. */
98
+ declare const WRITTEN_BY_TILUA = "-- Written by tilua";
59
99
  /** One file of the output. */
60
100
  interface BundleFile {
61
101
  /** Where it goes, from the folder the output is written to: `main.luau`,
@@ -81,9 +121,9 @@ interface BundleDiagnostic {
81
121
  readonly severity?: "warning";
82
122
  }
83
123
  interface BundleResult {
84
- /** The bundle — the entry's file, when there are several — or `undefined`
85
- * when a module could not be compiled. Type errors are reported but do
86
- * not stop it. */
124
+ /** The bundle — the entry's file, when there are several, and the first
125
+ * module's with `"files"` — or `undefined` when a module could not be
126
+ * compiled. Type errors are reported but do not stop it. */
87
127
  readonly code?: string;
88
128
  /** Every file of the output, the entry's first. Empty when there is no
89
129
  * bundle. */
@@ -138,6 +178,9 @@ interface LoadedLowering {
138
178
  * analyzer was given: a method read from a metatable they declare is
139
179
  * this lowering's to answer. Absent for the language's own. */
140
180
  readonly library?: number;
181
+ /** The library's own options, as the config sets them. None for the
182
+ * language's own. */
183
+ readonly options?: Readonly<Record<string, boolean | number | string | undefined>>;
141
184
  }
142
185
  interface LoweringProblem {
143
186
  readonly file: string;
@@ -150,6 +193,7 @@ declare function loadLowerings(modules: readonly {
150
193
  file: string;
151
194
  from: string;
152
195
  library?: number;
196
+ options?: LoadedLowering["options"];
153
197
  }[]): Promise<{
154
198
  lowerings: LoadedLowering[];
155
199
  problems: LoweringProblem[];
@@ -202,6 +246,12 @@ interface ModuleContext {
202
246
  /** What `import("./x")` calls, with the module's name: the bundle's
203
247
  * wrapper around its `require`. Default: `require` itself. */
204
248
  readonly import?: L.Expression;
249
+ /** Where a module is a file of its own: a function, passed after its
250
+ * name, that loads the file it is in — `function() return
251
+ * require("./shape") end`, with the path written out, for the tools that
252
+ * follow a `require`. `require` calls it only if the module is not
253
+ * already there. */
254
+ loader?(name: string): L.Expression | undefined;
205
255
  /** Luau reaching the bundle's line map — what a library's runtime reads,
206
256
  * as `__LINES__`, to name the file a frame was written in. */
207
257
  readonly lines?: string;
@@ -336,4 +386,4 @@ interface ModuleInfo {
336
386
  }
337
387
  declare function lower(program: T.Program, scopes: ScopeAnalysis, options?: LowerOptions): LowerResult;
338
388
 
339
- export { type BundleDiagnostic, type BundleFile, type BundleOptions, type BundleResult, type CompileResult, type ConfigInput, type Diagnostic, type LoadedLowering, type LowerDiagnostic, type LowerOptions, type LowerResult, type LoweringProblem, type ModuleContext, type ResolvedConfig, type TiluaConfigJson, bundle, compile, loadLowerings, lower, resolveConfig };
389
+ export { type BundleDiagnostic, type BundleFile, type BundleOptions, type BundleResult, type CompileResult, type ConfigInput, type Diagnostic, type LoadedLowering, type LowerDiagnostic, type LowerOptions, type LowerResult, type LoweringProblem, type ModuleContext, type ResolvedConfig, type TiluaConfigJson, WRITTEN_BY_TILUA, bundle, compile, loadLowerings, lower, resolveConfig };
package/dist/index.js CHANGED
@@ -1,16 +1,19 @@
1
1
  import {
2
+ WRITTEN_BY_TILUA,
2
3
  bundle,
3
4
  loadLowerings,
4
5
  lower,
6
+ printOptions,
7
+ printOutput,
5
8
  program,
6
- resolveConfig
7
- } from "./chunk-TOP7NC3Z.js";
9
+ resolveConfig,
10
+ syntaxOf
11
+ } from "./chunk-VUIF62OE.js";
8
12
 
9
13
  // src/compile.ts
10
14
  import { readFileSync } from "fs";
11
15
  import { resolve } from "path";
12
16
  import { parse, analyzeScopes, analyzeTypes, resolveTypeLibraries, ParseError, LexError } from "@tilua/parser";
13
- import { print } from "luau-parser";
14
17
  async function compile(source, config) {
15
18
  let program2;
16
19
  try {
@@ -30,7 +33,7 @@ async function compile(source, config) {
30
33
  line: d.node.line.start,
31
34
  column: d.node.column.start
32
35
  }));
33
- const libraries = resolved ? resolveTypeLibraries(resolved) : { files: [], lowerings: [], problems: [] };
36
+ const libraries = resolved ? resolveTypeLibraries(resolved) : { files: [], lowerings: [], options: [], problems: [] };
34
37
  const { lowerings, problems: loweringProblems } = await loadLowerings(libraries.lowerings);
35
38
  for (const p of [...problems, ...libraries.problems]) {
36
39
  diagnostics.push({ message: p.message, line: p.line ?? 1, column: p.column ?? 1 });
@@ -45,9 +48,15 @@ async function compile(source, config) {
45
48
  });
46
49
  diagnostics.push(...lowered.diagnostics);
47
50
  if (diagnostics.length) return { diagnostics };
48
- return { code: print(program(lowered.statements)) + "\n", diagnostics };
51
+ const printed = printOutput(program(lowered.statements), printOptions(syntaxOf(lowerings), {
52
+ indent: resolved?.indent,
53
+ minify: resolved?.minify
54
+ }));
55
+ if ("problem" in printed) return { diagnostics: [{ message: printed.problem, line: 1, column: 1 }] };
56
+ return { code: printed.code + "\n", diagnostics };
49
57
  }
50
58
  export {
59
+ WRITTEN_BY_TILUA,
51
60
  bundle,
52
61
  compile,
53
62
  loadLowerings,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tilua/compiler",
3
- "version": "8.0.0",
3
+ "version": "9.0.0",
4
4
  "description": "Compiles tilua to Luau: lowers the @tilua/parser AST to a luau-parser AST and prints it",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
@@ -37,8 +37,8 @@
37
37
  "license": "ISC",
38
38
  "type": "module",
39
39
  "dependencies": {
40
- "luau-parser": "^1.0.3",
41
- "@tilua/parser": "^8.0.0"
40
+ "@tilua/parser": "^9.0.0",
41
+ "luau-parser": "^1.1.0"
42
42
  },
43
43
  "devDependencies": {
44
44
  "@types/node": "^22.20.2",