Skip to content

Commit e89c5a4

Browse files
authored
Merge pull request #38 from typetorch/feat/production-pack
Production pack: strip, __DEV__ define, story shake (darklua)
2 parents f05babe + bbb70d0 commit e89c5a4

7 files changed

Lines changed: 417 additions & 9 deletions

File tree

‎README.md‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ show whether signing is ready, with placeholders instead of real signatures. Any
211211
| Command | Does |
212212
|---|---|
213213
| `typetorch init [--dir <folder>] [--phase <name>] [--answers <file>] [--agent] [--teardown]` | the guided setup, from an empty folder to a live game (see "Setup"); `--agent` prints `AGENT_PROMPT.md` again; `--teardown` removes the backend step 6 installed (asks first; its data only when you say so) |
214-
| `typetorch build [--branch <b>] [--channel prod\|dev] [--clean]` | writes `src/shared/build.ts`, runs the compiler (goblox when installed, else rbxtsc; `bun run build` if the repo has a build script), and rojo-builds `.typetorch/payload.rbxm` with the identity stamped on the root; checks it holds only Folders and ModuleScripts; writes `.typetorch/payload.json`. `--clean`: `git clean -fdX` out/ and include/ first, then restore the build cache |
214+
| `typetorch build [--branch <b>] [--channel prod\|dev] [--clean] [--no-minify]` | writes `src/shared/build.ts`, runs the compiler (goblox when installed, else rbxtsc; `bun run build` if the repo has a build script), and rojo-builds `.typetorch/payload.rbxm` with the identity stamped on the root; checks it holds only Folders and ModuleScripts; writes `.typetorch/payload.json`. `--clean`: `git clean -fdX` out/ and include/ first, then restore the build cache |
215215
| `typetorch upload [--no-build]` | clean build, upload as a new Model asset, wait for moderation; no deploy (then `promote` it) |
216216
| `typetorch deploy [--branch] [--channel] [--no-build] [--dry-run] [--message <text>] [--force] [--propose] [--proposed-by <who>] [--test] [--skip-test <reason>] [--wait [s]] [--no-wait] [--rollout <1-99>]` | clean build, upload, wait until Approved, log "uploaded"; the cloud test (always for prod-channel branches); then approve here (a person at a terminal) or write a proposal; on approval: deploy message, durable head, log "published"; then (prod: by default) wait for the servers' reports. Per-stage timings |
217217
| `typetorch deploy --widen <1-100> [--branch]` | re-send the branch's live deploy (same seq) to more servers; dev-channel branches only (see "Rollouts") |
@@ -368,6 +368,16 @@ goblox is selected and the tsconfig lists one. Set `"compiler": "rbxtsc"` to kee
368368
is gone are removed first; dev and prod never share one. Switching to goblox, or to another goblox version, is a full
369369
compile (the compiler is in the cache key; rbxtsc's keys are what they were before goblox). Needs `"incremental": true` with the tsbuildinfo inside
370370
outDir. `TYPETORCH_BUILD_CACHE=0` turns it off.
371+
- **Production pack (`src/pack.ts`):** `build` and `deploy` pack a transformed copy of the project's `$path` folders
372+
(`.typetorch/pack/`), so the artifact id, the signature and the `typetorch test` gate all cover the final bytes:
373+
comments and whitespace stripped by darklua's parser (lines stay where they are, so error traces and analytics still
374+
match), `__DEV__` set to false with its dead branches removed (skipped with a warning when game code sets it), and
375+
`*.story` modules no other module names left out. Packages are never shaken (their folder names aren't the names code
376+
requires them by). Needs darklua (`rokit add seaofvoices/darklua`; `TYPETORCH_DARKLUA` picks a binary): without it
377+
the build warns and packs unminified. Each module's output is cached by content hash in
378+
`.typetorch/build-cache/pack/`, and a darklua failure fails the build. `--no-minify`, or `"minify": false` in
379+
typetorch.json, turns it off. `typetorch serve` never packs. On a 1160-module game: about 2-3 s cold, under 0.5 s
380+
unchanged, the rbxm 26-27% smaller.
371381
- **Payload check:** the built `.rbxm` must be one `Model` holding only `Folder`s and `ModuleScript`s; anything else
372382
(Scripts, values from `.txt` files, models from `.rbxm` files...) fails the build with its path.
373383
- **Debug macros per channel:** dev builds keep the `$print`/`$warn`/`$dbg` macros with their `[src/file.ts:line]`

‎src/build.ts‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
* -> rojo build stamped with `<commit7>[-dirty]` -> hash -> rojo build stamped with the final id
77
* -> payload contents check (only Folders and ModuleScripts) -> .typetorch/payload.rbxm + payload.json.
88
*/
9+
import { execFileSync } from "node:child_process";
910
import { createHash } from "node:crypto";
11+
import { packProject, type PackResult } from "./pack.ts";
1012
import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
1113
import { dirname, extname, join, relative, resolve } from "node:path";
1214
import type { Project } from "./config.ts";
@@ -49,7 +51,7 @@ export const PAYLOAD_FILE = `${OUT_DIR}/payload.rbxm`;
4951
export const PAYLOAD_META = `${OUT_DIR}/payload.json`;
5052
export { PACKAGES_MANIFEST };
5153
/** Files TypeTorch writes inside the repo; they never make a build "dirty". */
52-
export const GENERATED_PATHS = [BUILD_FILE, GEN_PROJECT, PROD_TSCONFIG, `${OUT_DIR}/`];
54+
export const GENERATED_PATHS = [BUILD_FILE, GEN_PROJECT, PROD_TSCONFIG, `${OUT_DIR}/`, ".typetorch/pack/", ".typetorch/pack-work/"];
5355
/** File types that can end up in a payload (compiled by rbxtsc, or synced by Rojo). */
5456
const PAYLOAD_FILE_TYPES = /\.(ts|tsx|lua|luau|json|toml|ya?ml|txt|csv|rbxm|rbxmx|model\.json|meta\.json)$/i;
5557

@@ -243,6 +245,19 @@ export function rojoBinary(): string {
243245
return settings().get("TYPETORCH_ROJO")?.value || "rojo";
244246
}
245247

248+
export function darkluaBinary(): string {
249+
return process.env.TYPETORCH_DARKLUA || "darklua";
250+
}
251+
252+
/** The installed darklua's version line, or undefined when it can't run. */
253+
export function darkluaVersion(): string | undefined {
254+
try {
255+
return execFileSync(darkluaBinary(), ["--version"], { encoding: "utf8", timeout: 10_000, stdio: ["ignore", "pipe", "ignore"] }).trim();
256+
} catch {
257+
return undefined;
258+
}
259+
}
260+
246261
export function luneBinary(): string {
247262
return settings().get("TYPETORCH_LUNE")?.value || "lune";
248263
}
@@ -385,6 +400,8 @@ export interface BuildResult {
385400
}
386401

387402
export interface BuildOptions {
403+
/** The production stages (pack.ts); false: --no-minify. */
404+
minify?: boolean;
388405
branch?: string;
389406
channel?: Channel;
390407
/** `git clean -fdX` out/ and include/ first (deploy and upload always do; `build --clean`). */
@@ -579,10 +596,25 @@ export async function buildPayload(project: Project, options: BuildOptions = {})
579596
Object.assign(stamped, healthAttributes(health));
580597
return stamped;
581598
};
599+
// The production stages (pack.ts): rojo packs a transformed copy, so the id and the signature cover the final bytes.
600+
let packed: PackResult | undefined;
601+
if (options.minify !== false && (project.config as { minify?: boolean }).minify !== false) {
602+
const version = darkluaVersion();
603+
if (!version) {
604+
warn(`darklua not found: this payload isn't minified. Add it with \`rokit add seaofvoices/darklua\`, or set "minify": false in typetorch.json to silence this`);
605+
} else {
606+
await watch.stage("pack", async () => {
607+
packed = await packProject({ root, project: projectJson, paths: projectPaths(projectJson), darklua: darkluaBinary(), darkluaVersion: version, run });
608+
});
609+
for (const reason of packed!.skipped) warn(`pack: ${reason}`);
610+
debug(`pack: ${packed!.modules} modules (${packed!.processed} processed), ${packed!.sourceBytes} -> ${packed!.packedBytes} bytes, ${packed!.dropped.length} dropped`);
611+
}
612+
}
613+
const rojoProject = packed?.project ?? projectJson;
582614
const rojoBuild = async (id: string): Promise<Uint8Array> => {
583615
debug(`rojo build stamped ArtifactId=${id}`);
584616
const children = { [ASSETS_PAYLOAD_FOLDER]: { [ASSETS_PAYLOAD_ATTRIBUTE]: assetsJson } };
585-
writeFileSync(genPath, JSON.stringify(stampProject(projectJson, attributes(id), children), null, "\t"));
617+
writeFileSync(genPath, JSON.stringify(stampProject(rojoProject, attributes(id), children), null, "\t"));
586618
try {
587619
await run([rojoBinary(), "build", GEN_PROJECT, "-o", PAYLOAD_FILE], root);
588620
} finally {

‎src/commands/build.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { keepPayload } from "../payloads.ts";
1111
import { fixCensoredName, uploadPayload } from "../upload.ts";
1212
import { channelFlag, openCloud, project, projectStateDir } from "./common.ts";
1313

14-
export const buildFlags = { branch: "string", channel: "string", clean: "boolean" } as const;
14+
export const buildFlags = { branch: "string", channel: "string", clean: "boolean", "no-minify": "boolean" } as const;
1515

1616
export function describeBuild(meta: PayloadMeta): string {
1717
const state = meta.dirty ? "dirty" : "clean";
@@ -24,6 +24,7 @@ export async function buildCommand(args: ParsedArgs) {
2424
branch: flagString(args, "branch"),
2525
channel: channelFlag(args),
2626
clean: flagBool(args, "clean"),
27+
minify: !flagBool(args, "no-minify"),
2728
});
2829
if (isJson()) return emitJson({ ...meta, timings });
2930
info(`built ${describeBuild(meta)}`);
@@ -38,6 +39,7 @@ export async function buildCommand(args: ParsedArgs) {
3839
}
3940

4041
export const uploadFlags = {
42+
"no-minify": "boolean",
4143
branch: "string",
4244
channel: "string",
4345
"no-build": "boolean",
@@ -54,7 +56,7 @@ export async function uploadCommand(args: ParsedArgs) {
5456
if (flagBool(args, "no-build")) {
5557
({ meta, bytes } = readBuiltPayload(proj.root));
5658
} else {
57-
const built = await watch.stage("build", () => buildPayload(proj, { branch: flagString(args, "branch"), channel: channelFlag(args), clean: true }));
59+
const built = await watch.stage("build", () => buildPayload(proj, { branch: flagString(args, "branch"), channel: channelFlag(args), clean: true, minify: !flagBool(args, "no-minify") }));
5860
meta = built.meta;
5961
bytes = payloadBytes(proj.root);
6062
info(` build ${formatSeconds(watch.timings.build)} ${describeBuild(meta)}`);

‎src/commands/deploy.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import type { OpenCloud } from "../opencloud.ts";
4949
import type { LiveHead } from "../deployments.ts";
5050

5151
export const deployFlags = {
52+
"no-minify": "boolean",
5253
branch: "string",
5354
channel: "string",
5455
"no-build": "boolean",
@@ -210,7 +211,7 @@ export async function deployCommand(args: ParsedArgs) {
210211
info(`using ${describeBuild(meta)}`);
211212
} else {
212213
const notes = async (branch: string) => ({ message: note, previous: withLocal(proj).heads.get(branch) });
213-
const built = await watch.stage("build", () => buildPayload(proj, { branch: branchFlag, channel: channelOverride, clean: true, notes }));
214+
const built = await watch.stage("build", () => buildPayload(proj, { branch: branchFlag, channel: channelOverride, clean: true, notes, minify: !flagBool(args, "no-minify") }));
214215
meta = built.meta;
215216
by = built.target.git.userName;
216217
bytes = payloadBytes(proj.root);

‎src/index.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const COMMANDS: Record<string, Command> = {
7373
flags: buildFlags,
7474
run: buildCommand,
7575
summary: "compile and pack the payload",
76-
usage: `typetorch build [--branch <b>] [--channel prod|dev] [--clean]
76+
usage: `typetorch build [--branch <b>] [--channel prod|dev] [--clean] [--no-minify]
7777
7878
Writes src/shared/build.ts, runs the compiler (goblox when installed, else rbxtsc; typetorch.json "compiler" or
7979
TYPETORCH_COMPILER picks one) (prod channel: $print/$warn compiled away, no source paths), then
@@ -89,7 +89,7 @@ const COMMANDS: Record<string, Command> = {
8989
flags: uploadFlags,
9090
run: uploadCommand,
9191
summary: "build and upload a payload, no deploy",
92-
usage: `typetorch upload [--branch <b>] [--channel prod|dev] [--no-build] [--moderation-timeout <s>]
92+
usage: `typetorch upload [--branch <b>] [--channel prod|dev] [--no-minify] [--no-build] [--moderation-timeout <s>]
9393
9494
Uploads the payload as a NEW private Model asset named tt-<branch>-<commit>[-dirty] and waits for moderation.
9595
Does not touch live servers.`,
@@ -98,7 +98,7 @@ const COMMANDS: Record<string, Command> = {
9898
flags: deployFlags,
9999
run: deployCommand,
100100
summary: "build, upload, approve and send to live servers",
101-
usage: `typetorch deploy [--branch <b>] [--channel prod|dev] [--no-build] [--dry-run] [--message <text>] [--force] [--reupload <build>]
101+
usage: `typetorch deploy [--branch <b>] [--channel prod|dev] [--no-build] [--no-minify] [--dry-run] [--message <text>] [--force] [--reupload <build>]
102102
[--no-registry] [--moderation-timeout <s>] [--propose] [--proposed-by <who>]
103103
[--key-file <path>] [--fallback-key-file <path>]
104104

0 commit comments

Comments
 (0)