mirror of
https://github.com/cloudflare/vinext.git
synced 2026-09-14 19:04:59 +08:00
f8e79b7ff6
Repeated vinext builds currently leave stale hashed server chunks in dist/server. Workers deployments with no_bundle upload every matched JS module, so stale chunks count against Worker size limits. The CLI assumed Vite's per-environment emptyOutDir would remove old output, but the multi-environment build and hybrid Pages Router pass preserve dist/server artifacts. Clean the vinext dist output once immediately before the Vite build starts, while preserving build.emptyOutDir: false as the escape hatch. Add focused tests for default cleaning, explicit preservation, and outside-root safety.
66 lines
2.1 KiB
TypeScript
66 lines
2.1 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it } from "vite-plus/test";
|
|
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { cleanBuildOutput } from "../packages/vinext/src/build/clean-output.js";
|
|
|
|
let tmpDir: string;
|
|
|
|
function writeFile(root: string, relativePath: string, content: string): string {
|
|
const target = path.join(root, relativePath);
|
|
fs.mkdirSync(path.dirname(target), { recursive: true });
|
|
fs.writeFileSync(target, content, "utf-8");
|
|
return target;
|
|
}
|
|
|
|
describe("cleanBuildOutput", () => {
|
|
beforeEach(() => {
|
|
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "vinext-clean-output-"));
|
|
});
|
|
|
|
afterEach(() => {
|
|
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
});
|
|
|
|
it("removes stale server modules from the build output by default", () => {
|
|
const staleModule = writeFile(
|
|
tmpDir,
|
|
"dist/server/ssr/_next/static/stale-dead-module.js",
|
|
"export default 1;\n",
|
|
);
|
|
|
|
const result = cleanBuildOutput({ root: tmpDir });
|
|
|
|
expect(result.cleaned).toBe(true);
|
|
expect(result.outDir).toBe(path.join(tmpDir, "dist"));
|
|
expect(fs.existsSync(staleModule)).toBe(false);
|
|
});
|
|
|
|
it("preserves the build output when emptyOutDir is explicitly disabled", () => {
|
|
const staleModule = writeFile(
|
|
tmpDir,
|
|
"dist/server/ssr/_next/static/stale-dead-module.js",
|
|
"export default 1;\n",
|
|
);
|
|
|
|
const result = cleanBuildOutput({ root: tmpDir, emptyOutDir: false });
|
|
|
|
expect(result.cleaned).toBe(false);
|
|
expect(fs.existsSync(staleModule)).toBe(true);
|
|
});
|
|
|
|
it("does not remove an output directory outside the project root by default", () => {
|
|
const externalOutDir = fs.mkdtempSync(path.join(os.tmpdir(), "vinext-external-output-"));
|
|
try {
|
|
const staleModule = writeFile(externalOutDir, "stale-dead-module.js", "export default 1;\n");
|
|
|
|
const result = cleanBuildOutput({ root: tmpDir, outDir: externalOutDir });
|
|
|
|
expect(result.cleaned).toBe(false);
|
|
expect(fs.existsSync(staleModule)).toBe(true);
|
|
} finally {
|
|
fs.rmSync(externalOutDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
});
|