Files
cloudflare__vinext/tests/clean-build-output.test.ts
Jerry Zhao 1860632d87 refactor: migrate path handling from node:path to pathslash (#2502)
* refactor: migrate the CLI cluster path handling to pathslash

* refactor(routing): migrate the routing cluster path handling to pathslash

* refactor(plugins): migrate the plugins cluster path handling to pathslash

* refactor(server): migrate the server cluster path handling to pathslash

* refactor(build): migrate the build cluster path handling to pathslash

* refactor(core): migrate entries, config, and shared utils to pathslash

* refactor(plugin): migrate index.ts path handling to pathslash

* refactor: finish the pathslash migration — test sweep, stragglers, lint guard

* refactor: address final-review findings on the pathslash migration

* docs(agents): document the pathslash path-handling convention

* fix(build): emit inlined dependencies outside dist/node_modules

* fix: normalize wire- and bundler-shaped ids unconditionally

* docs(agents): document the unconditional-normalization exception

* fix: gate Windows-shaped path fixtures to Windows

* docs(agents): gate Windows-shaped fixtures with runIf

* refactor: migrate upstream prerender path discovery to pathslash

* fix(create-vinext-app): bundle pathslash into the scaffolder dist

---------

Co-authored-by: James <james@eli.cx>
2026-07-06 23:49:04 +01:00

67 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 { toSlash } from "pathslash";
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(toSlash(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 });
}
});
});