getBuildConfig defaulted to the whole import.meta.env, so Vite inlined every loaded VITE_* value, read or not. Values are now read by key, a lint rule bans whole-object references in src, and vite.config refuses VITE_* names missing from ENV_REGISTRY. A build test pins the values that must stay in the bundle. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
87 lines
3.4 KiB
TypeScript
87 lines
3.4 KiB
TypeScript
import { spawnSync } from "node:child_process";
|
|
import { mkdtemp, readdir, readFile, rm } from "node:fs/promises";
|
|
import { tmpdir } from "node:os";
|
|
import path from "node:path";
|
|
|
|
import { afterAll, describe, expect, it } from "vitest";
|
|
|
|
import { filesBelow } from "../../scripts/lib/removal-fixture.ts";
|
|
|
|
/**
|
|
* 13-06. Which VITE_* values reach the bundle. The declared values the app
|
|
* reads must still be there (build id, commit, runtime config URL, base path);
|
|
* a declared value nothing reads and an undeclared one must not be, and the
|
|
* design-system chunk must not change with the build id.
|
|
*/
|
|
const roots: string[] = [];
|
|
|
|
afterAll(async () => {
|
|
await Promise.all(roots.map((root) => rm(root, { recursive: true, force: true })));
|
|
});
|
|
|
|
function inheritedEnvironment(): NodeJS.ProcessEnv {
|
|
return Object.fromEntries(
|
|
Object.entries(process.env).filter(([name]) => !name.startsWith("VITE_")),
|
|
);
|
|
}
|
|
|
|
async function build(variables: Readonly<Record<string, string>>) {
|
|
const outDir = await mkdtemp(path.join(tmpdir(), "build-environment-"));
|
|
roots.push(outDir);
|
|
const run = spawnSync(
|
|
path.resolve("node_modules/.bin/vite"),
|
|
["build", "--outDir", outDir, "--emptyOutDir", "--logLevel", "error"],
|
|
{ encoding: "utf8", env: { ...inheritedEnvironment(), ...variables } },
|
|
);
|
|
const contents = run.status === 0
|
|
? await Promise.all(
|
|
(await filesBelow(outDir))
|
|
.filter((file) => /\.(?:js|html)$/u.test(file))
|
|
.map(async (file) => ({ file, text: await readFile(file, "utf8") })),
|
|
)
|
|
: [];
|
|
return { run, outDir, contents };
|
|
}
|
|
|
|
const DECLARED = Object.freeze({
|
|
VITE_COMMIT_SHA: "commit-probe-13",
|
|
VITE_ROUTER_BASE_PATH: "/base-probe/",
|
|
VITE_RUNTIME_CONFIG_URL: "/base-probe/runtime-probe.json",
|
|
// Declared, but applied by the index generator; the bundle never reads it.
|
|
VITE_PRODUCT_FEATURES: "unreferenced-probe-13",
|
|
});
|
|
|
|
describe("build variables in the bundle (13-06)", () => {
|
|
it("keeps the values the app reads and nothing else", async () => {
|
|
const first = await build({ ...DECLARED, VITE_BUILD_ID: "build-probe-1" });
|
|
expect(first.run.stderr).toBe("");
|
|
expect(first.run.status).toBe(0);
|
|
const bundled = (value: string) =>
|
|
first.contents.some(({ text }) => text.includes(value));
|
|
expect(bundled("build-probe-1")).toBe(true);
|
|
expect(bundled("commit-probe-13")).toBe(true);
|
|
expect(bundled("/base-probe/runtime-probe.json")).toBe(true);
|
|
const html = first.contents.find(({ file }) => file.endsWith("index.html"));
|
|
expect(html?.text).toContain('src="/base-probe/assets/');
|
|
expect(bundled("unreferenced-probe-13")).toBe(false);
|
|
|
|
const second = await build({ ...DECLARED, VITE_BUILD_ID: "build-probe-2" });
|
|
expect(second.run.status).toBe(0);
|
|
const designSystem = async (outDir: string) =>
|
|
(await readdir(path.join(outDir, "assets"))).filter((name) =>
|
|
name.startsWith("design-system-"),
|
|
);
|
|
const firstChunks = await designSystem(first.outDir);
|
|
expect(firstChunks).toHaveLength(1);
|
|
expect(await designSystem(second.outDir)).toEqual(firstChunks);
|
|
}, 120_000);
|
|
|
|
it("refuses a VITE_* variable the build does not declare", async () => {
|
|
const refused = await build({ VITE_PAYMENT_KEY: "sk_live_probe" });
|
|
expect(refused.run.status).not.toBe(0);
|
|
expect(refused.run.stderr + refused.run.stdout).toMatch(
|
|
/Undeclared VITE_\* build variables: VITE_PAYMENT_KEY/u,
|
|
);
|
|
}, 120_000);
|
|
});
|