@tilua/compiler 8.1.0 → 9.0.1

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/README.md CHANGED
@@ -13,10 +13,17 @@ npx tilua src/main.tilua --out build/main.luau
13
13
  ```
14
14
 
15
15
  ```
16
- tilua <entry> [--out <file>] [--output single|chunks|modules] [--config <tilua.config.json>]
17
- [--noCheck] [--noLineMap]
16
+ tilua [<entry>] [--out <file>] [--output single|chunks|files] [--config <tilua.config.json>]
17
+ [--noCheck] [--noLineMap] [--minify] [--watch]
18
+ tilua init
18
19
  ```
19
20
 
21
+ `--watch` builds again whenever a file it reads changes — a `.tilua`, the
22
+ config, a Rojo project, and with an `outDir` of its own anything under
23
+ `rootDir` — and keeps going. `tilua init` sets up the Rojo project in the
24
+ folder: `src` compiled to `out`, and the project pointed at `out` — see
25
+ `@tilua-types/roblox`.
26
+
20
27
  Every module the entry imports is bundled in. Problems are reported as
21
28
  `file:line:column message`. Type errors are checked against the config's
22
29
  `types`: they are reported, but the bundle is still written (`--noCheck` skips
@@ -123,6 +130,11 @@ Luau without asking:
123
130
  | `@native` and other attributes | dropped | kept |
124
131
  | another file of the bundle | `loadfile` | `require` by path |
125
132
 
133
+ Without such a library the output is printed as Lua 5.1, and the printer
134
+ refuses what 5.1 has no syntax for: a `+=` let through, or a library's runtime
135
+ written in Luau, stops the build with `The output cannot be written as Lua
136
+ 5.1: …` instead of failing on a Lua that cannot read it.
137
+
126
138
  An object's `freeze` does nothing on a Lua without `table.freeze`, and
127
139
  `isFrozen` answers `false`. `npm test` with `LUA51` naming a Lua 5.1
128
140
  interpreter builds a program both ways and checks they print the same.
@@ -264,6 +276,18 @@ arguments. `globalCall` and `globalValue` answer for a global, such as
264
276
  `print(x)` or a bare `print`. A runtime can read `__LINES__` to reach the
265
277
  bundle's line map, and wraps a new array it answers with in `__ARRAY__(...)`. See the `@tilua/parser` README for the details.
266
278
 
279
+ `entryExports` answers for what a bundle whose entry exports returns: it is
280
+ handed that expression (`value`) and the entry's exports as an importer sees
281
+ them, and may answer another expression and statements to put before it.
282
+ `@tilua-types/luau` casts the exports table to its Luau type there, so a
283
+ script that `require`s the bundle knows what it got.
284
+
285
+ A library can add options to `tilua.config.json` by declaring them in its
286
+ package.json — `"tilua": { "options": { "typedExports": { "type": "boolean",
287
+ "default": true } } }` — and every hook is handed its library's own as
288
+ `options`. A key no loaded library declares is an unknown option. See the
289
+ `@tilua/parser` README.
290
+
267
291
  A library for a Lua with more syntax than 5.1's lists it, and the output uses
268
292
  it (see [Lua and Luau](#lua-and-luau)):
269
293
 
@@ -306,6 +330,15 @@ everything that reads it: the entry runs without `xpcall`, a callback handed
306
330
  to Roblox is passed through unwrapped, and an error names the bundle's own
307
331
  lines, as plain Luau would.
308
332
 
333
+ ## Layout
334
+
335
+ `"indent": "\t"` (or a number of spaces) indents the output a block level at
336
+ a time; without it nothing is indented. `"minify": true` (or `--minify`, or
337
+ `bundle({ minify: true })`) writes the whole output on one line, keeping a `;`
338
+ only where the next statement starts with `(`. One line cannot be mapped back
339
+ to the project's, so a minified bundle goes without the line map, as with
340
+ `"lineMap": false`.
341
+
309
342
  ## Modules
310
343
 
311
344
  Roblox's `require` takes an Instance, so the bundle has its own. Each module
@@ -401,21 +434,16 @@ says how many files the project becomes:
401
434
  |---|---|
402
435
  | `"single"` (default) | one, holding every module |
403
436
  | `"chunks"` | the entry's, holding what it reaches through `import` statements; one for each `import()` that leads somewhere else, holding what only it reaches; and one for what several of those share |
404
- | `"modules"` | one per module, in the project's own tree |
437
+ | `"files"` | no entry: one per file under `rootDir`, in the same place under `outDir`, and the runtime — see [A file per module](#a-file-per-module) |
405
438
 
406
439
  ```
407
440
  tilua src/main.tilua --out build/main.luau --output chunks
408
441
  # build/main.luau, build/main_src_shop.luau, ...
409
-
410
- tilua src/main.tilua --out build --output modules
411
- # build/src/main.luau, build/src/shop.luau, ...
412
442
  ```
413
443
 
414
- With `"modules"`, `--out` is the folder the tree goes in — by default the
415
- project's, so each `.luau` lands beside its `.tilua`. From the API,
416
- `result.files` holds every file with the path it has to be written at, the
417
- entry's first; `outFile` names the entry's file, which the others are named
418
- after.
444
+ From the API, `result.files` holds every file with the path it has to be
445
+ written at, the entry's first; `outFile` names the entry's file, which the
446
+ others are named after.
419
447
 
420
448
  The module table is the same one, filled by more than one file. The entry's
421
449
  file has the runtime; every other file is a function that adds its modules:
@@ -441,15 +469,89 @@ How a file is loaded depends on the Lua (see [Lua and Luau](#lua-and-luau)):
441
469
  Either way the files have to stay where they were written, relative to the
442
470
  entry's.
443
471
 
472
+ ## A file per module
473
+
474
+ `"output": "files"` has no entry. Every `.tilua` under `rootDir` (a
475
+ definitions file aside) becomes a `.luau` in the same place under `outDir`,
476
+ as `tsc` writes `src` to `dist`, and each of them is a file a script can
477
+ `require`:
478
+
479
+ ```jsonc
480
+ { "types": ["roblox"], "output": "files", "rootDir": "src", "outDir": "dist" }
481
+ ```
482
+
483
+ ```
484
+ tilua # the config above this folder says the rest
485
+ # dist/main.luau, dist/shop/buy.luau, ..., dist/_tilua.luau
486
+ ```
487
+
488
+ `rootDir` is the config's folder unless set, and `outDir` is `rootDir`, so
489
+ each `.luau` lands beside its `.tilua`. Every file it writes ends with a line
490
+ saying tilua wrote it: the CLI never replaces a `.luau` without that line —
491
+ one written by hand — and removes one with it whose `.tilua` is gone.
492
+
493
+ With an `outDir` of its own, the CLI makes it a mirror of `rootDir`: every
494
+ `.tilua` compiled, every other file copied as it is, every folder there, and
495
+ nothing else. `outDir` is the build's alone, so it may not hold `rootDir` or
496
+ the project, nor sit under `rootDir`. `--out` names the folder in place of
497
+ `outDir`. From the API, `bundle({ output: "files", root })` without an
498
+ `entry`; `result.files` holds every file, its path from `outDir`.
499
+
500
+ The runtime — the module table, `require`, the line maps, and the helpers
501
+ the modules share, the language's and the libraries' runtimes, each set up
502
+ once — is a file of its own, `_tilua.luau`. Every other file requires it,
503
+ takes the helpers it names from it, adds its module to the table, and returns
504
+ the module's exports:
505
+
506
+ ```lua
507
+ local G = require("../_tilua")
508
+ G.modules["src/shop/buy"] = { load = function(exports)
509
+ local tax
510
+ tax = G.require("src/shop/tax", function()
511
+ return require("./tax")
512
+ end)
513
+ ...
514
+ end }
515
+ return G.run("src/shop/buy")
516
+ ```
517
+
518
+ An `import` is still the runtime's `require`, so the modules behave as they
519
+ do in one file: a cycle gets the exports as they stand (Luau's own `require`
520
+ refuses one), and exports are live. The function after the name requires the
521
+ module's file by its path from this one, written out for the tools that
522
+ follow a `require`, and only if nothing has loaded it yet; an `import()` is
523
+ the same, where it runs. `run` is `require` under `xpcall` for the first file
524
+ asked for, so an error reaches the project's files once, however deep.
525
+
526
+ A file named `init.tilua` is the folder it is in, as Luau reads `init.luau`:
527
+ `import "./net"` finds `net/init.tilua`, the folder's files require it as
528
+ `./net`, and it requires them as `@self/remote`.
529
+
530
+ Where the files end up is the host's to say: a library's `files` hook can
531
+ place each file somewhere else, name the top of that tree (`@game`), move
532
+ the runtime, run something first in a file, and mark the files that are
533
+ scripts the host runs rather than modules — nothing may import one.
534
+ `@tilua-types/roblox` answers from a Rojo project.
535
+
536
+ A file is required by its path, which Luau reads and Lua 5.1 does not: the
537
+ output needs a library that says its Lua can (`requireByPath` — `"luau"`, or
538
+ `"roblox"`, which loads it). A file imported from outside `rootDir` has no
539
+ place under `outDir`, and is an error. Without an entry there is nothing to
540
+ reach things from, so `treeShaking` leaves no module out; it still leaves out
541
+ the runtime functions nothing calls.
542
+
543
+ `@tilua-types/luau` casts what each file returns to its module's exports, so
544
+ a script that requires any of them knows what it got.
545
+
444
546
  <!-- compatibility:start -->
445
547
  ## Compatibility
446
548
 
447
- `@tilua/compiler@8.1.0` works with these tilua packages. *Released with* is the
549
+ `@tilua/compiler@9.0.1` works with these tilua packages. *Released with* is the
448
550
  version it was built and tested against.
449
551
 
450
552
  | Package | Range | Released with | |
451
553
  | --- | --- | --- | --- |
452
- | `@tilua/parser` | `^8.1.0` | 8.1.0 | dependency |
554
+ | `@tilua/parser` | `^9.0.1` | 9.0.1 | dependency |
453
555
  <!-- compatibility:end -->
454
556
 
455
557
  ## Development