testBundlesHoldOnlyWhatTheAppReaches

tcmts.cli.test.e2e.prod.testBundlesHoldOnlyWhatTheAppReaches · #62ngwekl · term

No dead code to eliminate: a production build holds only the definitions the app reaches, never the rest of a namespace it uses (what isn't reached was never in it to shake out)

import { setUp } from "#codebase/tcmts/cli/test/e2e/prod";
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";

export const testBundlesHoldOnlyWhatTheAppReaches = () => {
  const { dir, tcm, scratch } = setUp();
  scratch(`// tcm: namespace app
export const shown = () => "REACHED_MARKER";
export const neverUsed = () => "UNREACHED_MARKER";
export const alsoUnused = { text: "UNREACHED_MARKER_TOO" };
export function main() { document.querySelector("#app")!.textContent = shown(); }
`);
  tcm("add");
  assert.match(tcm("build", "app.main"), /built app\.main/);
  const assets = path.join(dir, ".tcm", "apps", "assets");
  const code = fs
    .readdirSync(assets)
    .filter((f) => f.endsWith(".js"))
    .map((f) => fs.readFileSync(path.join(assets, f), "utf8"))
    .join("\n");
  assert.match(code, /REACHED_MARKER/, "what the app uses is there");
  assert.doesNotMatch(
    code,
    /UNREACHED_MARKER/,
    "what it doesn't, beside it in its namespace, isn't",
  );
}

uses