fix(vite): exclude test files from production builds (#3559)

The Vite plugin was not filtering out test files from route discovery,
causing test files in routes/ (e.g., routes/api/counter_test.ts) to be
bundled into production builds, inflating bundle sizes by ~20KB per test
file and including unnecessary test dependencies.

Root cause: Vite plugin defaulted ignore patterns to [] while Builder
correctly defaults to [TEST_FILE_PATTERN].

This fix:

- Defines TEST_FILE_PATTERN in shared constants.ts for reusability
- Exports TEST_FILE_PATTERN from internals_dev.ts for Vite plugin access
- Updates Vite plugin default from [] to [TEST_FILE_PATTERN]
- Updates Builder to import TEST_FILE_PATTERN from constants.ts
- Ensures test files matching /[._]test\.(?:[tj]sx?|[mc][tj]s)$/ are
excluded
- Adds integration test to prevent regression
- Allows Deno best practice of colocating test files without bundling
them

Test coverage (prevents regression):

- Unit test: walkDir respects skip patterns
- Unit test: crawlRouteDir excludes test files
- Integration test: Vite builds exclude test files from output

Fixes #3558
This commit is contained in:
Brett Chalupa
2025-10-21 11:26:20 +02:00
committed by GitHub
parent 2a78005bc9
commit de1810e70d
14 changed files with 150 additions and 6 deletions
+2
View File
@@ -12,3 +12,5 @@ export const WEEK = DAY * 7;
export const ASSET_CACHE_BUST_KEY = "__frsh_c";
export const UPDATE_INTERVAL = DAY;
export const TEST_FILE_PATTERN = /[._]test\.(?:[tj]sx?|[mc][tj]s)$/;
+1 -3
View File
@@ -25,7 +25,7 @@ import { parseDirPath } from "../config.ts";
import { pathToExportName, UniqueNamer } from "../utils.ts";
import { checkDenoCompilerOptions } from "./check.ts";
import { crawlFsItem } from "./fs_crawl.ts";
import { UPDATE_INTERVAL } from "../constants.ts";
import { TEST_FILE_PATTERN, UPDATE_INTERVAL } from "../constants.ts";
export interface BuildOptions {
/**
@@ -104,8 +104,6 @@ export type ResolvedBuildConfig = Required<Omit<BuildOptions, "sourceMap">> & {
sourceMap?: FreshBundleOptions["sourceMap"];
};
const TEST_FILE_PATTERN = /[._]test\.(?:[tj]sx?|[mc][tj]s)$/;
// deno-lint-ignore no-explicit-any
export class Builder<State = any> {
#transformer: FileTransformer;
+25
View File
@@ -18,3 +18,28 @@ Deno.test("walkDir - ", async () => {
expect(files).toEqual(["foo/bar/baz.txt", "foo/bar.txt"]);
});
Deno.test("walkDir - respects skip patterns", async () => {
const fs = createFakeFs({
"routes/index.tsx": "foo",
"routes/index_test.tsx": "test",
"routes/about.tsx": "foo",
"routes/about.test.ts": "test",
"routes/api/users.ts": "foo",
"routes/api/users_test.ts": "test",
});
const files: string[] = [];
const TEST_FILE_PATTERN = /[._]test\.(?:[tj]sx?|[mc][tj]s)$/;
await walkDir(fs, "routes", (entry) => {
files.push(entry.path);
}, [TEST_FILE_PATTERN]);
// Should only include non-test files
expect(files).toEqual([
"routes/index.tsx",
"routes/about.tsx",
"routes/api/users.ts",
]);
});
+49
View File
@@ -1564,3 +1564,52 @@ Deno.test("fsRoutes - merge group methods", async () => {
res = await server.post("/bar");
expect(await res.text()).toEqual("POST ok");
});
Deno.test("fsRoutes - ignores test files in routes folder", async () => {
const fs = createFakeFs({
"routes/index.tsx": {
default: () => <>index</>,
},
"routes/index_test.tsx": {
default: () => <>index_test</>,
},
"routes/foo_test.ts": {
handler: () => new Response("foo_test"),
},
"routes/bar.test.ts": {
handler: () => new Response("bar_test"),
},
"routes/baz.test.tsx": {
default: () => <>baz_test</>,
},
"routes/qux_test.js": {
handler: () => new Response("qux_test"),
},
"routes/valid.tsx": {
default: () => <>valid</>,
},
});
const TEST_FILE_PATTERN = /[._]test\.(?:[tj]sx?|[mc][tj]s)$/;
const routeDir = path.join(fs.cwd(), "routes");
const rawFiles = await crawlRouteDir(
fs,
routeDir,
[TEST_FILE_PATTERN],
() => {},
);
// Only index.tsx and valid.tsx should be found, test files should be filtered out
expect(rawFiles.length).toEqual(2);
expect(rawFiles.some((f) => f.filePath.includes("index.tsx"))).toBe(true);
expect(rawFiles.some((f) => f.filePath.includes("valid.tsx"))).toBe(true);
// Ensure none of the test files are included
expect(rawFiles.some((f) => f.filePath.includes("index_test.tsx"))).toBe(
false,
);
expect(rawFiles.some((f) => f.filePath.includes("foo_test.ts"))).toBe(false);
expect(rawFiles.some((f) => f.filePath.includes("bar.test.ts"))).toBe(false);
expect(rawFiles.some((f) => f.filePath.includes("baz.test.tsx"))).toBe(false);
expect(rawFiles.some((f) => f.filePath.includes("qux_test.js"))).toBe(false);
});
+1 -1
View File
@@ -12,4 +12,4 @@ export {
export { specToName } from "./dev/builder.ts";
export { pathToSpec, UniqueNamer } from "./utils.ts";
export { updateCheck } from "./dev/update_check.ts";
export { UPDATE_INTERVAL } from "./constants.ts";
export { TEST_FILE_PATTERN, UPDATE_INTERVAL } from "./constants.ts";
+7 -1
View File
@@ -102,8 +102,14 @@ export function serveMiddleware<T>(
export function createFakeFs(files: Record<string, unknown>): FsAdapter {
return {
cwd: () => ".",
async *walk(_root) {
async *walk(_root, options) {
const skip = options?.skip ?? [];
for (const file of Object.keys(files)) {
// Check if file matches any skip pattern
if (skip.some((pattern) => pattern.test(file))) {
continue;
}
const entry: WalkEntry = {
isDirectory: false,
isFile: true,