Skip to content

Commit e6444d7

Browse files
authored
Merge pull request #39 from typetorch/fix/pack-keeps-compiler-header
Pack keeps the compiler's header comment
2 parents 52f679b + 50b127c commit e6444d7

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

‎src/pack.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* syncs the project itself and never comes here.
55
*
66
* Stages, on by default for deploys; `"minify": false` in typetorch.json or --no-minify turns them off:
7-
* 1. strip: comments and whitespace, with darklua's parser. Lines stay where they are (generator retain_lines), so
7+
* 1. strip: comments (but the compiler's header line) and whitespace, with darklua's parser. Lines stay where they are (generator retain_lines), so
88
* error traces and analytics fingerprints still point at the compiled source.
99
* 2. define: `__DEV__` becomes false and its dead branches go, like Vite's define. React reads it from _G or
1010
* ReactGlobals, defaulting to false; a game that sets it switches this stage off.
@@ -23,6 +23,8 @@ export const PACK_DIR = ".typetorch/pack";
2323
const CACHE_SUBDIR = "pack";
2424
const WORK_DIR = ".typetorch/pack-work";
2525
const SCRIPT = /\.(luau|lua)$/;
26+
/** darklua's `except` pattern (Rust regex) for the comment rbxtsc and goblox put on line 1. */
27+
export const COMPILER_HEADER = "^--\\s*Compiled with ";
2628
/** Below this many changed modules another process doesn't pay for itself. */
2729
const FILES_PER_PROCESS = 200;
2830
const MAX_PROCESSES = 4;
@@ -56,7 +58,8 @@ function sha(text: string | Uint8Array): string {
5658
/** darklua's settings for these stages. */
5759
export function darkluaConfig(stages: PackStages): object {
5860
const rules: unknown[] = [];
59-
if (stages.strip) rules.push("remove_comments", "remove_spaces");
61+
// The compiler's header line (`-- Compiled with roblox-ts v3.0.0`) stays: it says what produced the module.
62+
if (stages.strip) rules.push({ rule: "remove_comments", except: [COMPILER_HEADER] }, "remove_spaces");
6063
if (stages.define) {
6164
rules.push({ rule: "inject_global_value", identifier: "__DEV__", value: false }, "compute_expression", "remove_unused_if_branch", "remove_unused_while", "remove_empty_do");
6265
}

‎test/pack.test.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { tmpdir } from "node:os";
44
import { dirname, join } from "node:path";
55
import { buildFlags, uploadFlags } from "../src/commands/build.ts";
66
import { deployFlags } from "../src/commands/deploy.ts";
7-
import { darkluaConfig, defineDev, packProject, remapProject, setsDev, shake } from "../src/pack.ts";
7+
import { COMPILER_HEADER, darkluaConfig, defineDev, packProject, remapProject, setsDev, shake } from "../src/pack.ts";
88

99
describe("defineDev", () => {
1010
it("empties a whole-line binding, keeping the line", () => {
@@ -109,6 +109,13 @@ describe("packProject", () => {
109109
expect(result.skipped.length).toBe(1);
110110
});
111111

112+
it("keeps the compiler's header comment", () => {
113+
const rule = (darkluaConfig({ strip: true, define: false, shake: false }) as any).rules[0];
114+
expect(rule).toEqual({ rule: "remove_comments", except: [COMPILER_HEADER] });
115+
expect(new RegExp(COMPILER_HEADER).test("-- Compiled with roblox-ts v3.0.0")).toBe(true);
116+
expect(new RegExp(COMPILER_HEADER).test("-- a normal comment")).toBe(false);
117+
});
118+
112119
it("keeps retain_lines whatever the stages", () => {
113120
expect((darkluaConfig({ strip: true, define: true, shake: true }) as any).generator).toBe("retain_lines");
114121
expect(remapProject({ tree: { a: { $path: "./x" } } }, "d").tree.a.$path).toBe("d/x");

0 commit comments

Comments
 (0)