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:
@@ -16,6 +16,7 @@ import { patches } from "./plugins/patches.ts";
|
||||
import process from "node:process";
|
||||
import {
|
||||
specToName,
|
||||
TEST_FILE_PATTERN,
|
||||
UniqueNamer,
|
||||
UPDATE_INTERVAL,
|
||||
updateCheck,
|
||||
@@ -33,7 +34,7 @@ export function fresh(config?: FreshViteConfig): Plugin[] {
|
||||
clientEntry: config?.clientEntry ?? "client.ts",
|
||||
islandsDir: config?.islandsDir ?? "islands",
|
||||
routeDir: config?.routeDir ?? "routes",
|
||||
ignore: config?.ignore ?? [],
|
||||
ignore: config?.ignore ?? [TEST_FILE_PATTERN],
|
||||
islandSpecifiers: new Map(),
|
||||
namer: new UniqueNamer(),
|
||||
checkImports: config?.checkImports ?? [],
|
||||
|
||||
@@ -566,3 +566,38 @@ Deno.test({
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "vite build - excludes test files from routes",
|
||||
fn: async () => {
|
||||
const fixture = path.join(FIXTURE_DIR, "test_files_exclusion");
|
||||
await using res = await buildVite(fixture);
|
||||
|
||||
// Verify that test files in routes/ are not bundled
|
||||
const serverAssetsDir = path.join(res.tmp, "_fresh", "server", "assets");
|
||||
|
||||
// List all compiled route files
|
||||
const files: string[] = [];
|
||||
for await (const entry of Deno.readDir(serverAssetsDir)) {
|
||||
if (entry.isFile && entry.name.startsWith("_fresh-route")) {
|
||||
files.push(entry.name);
|
||||
}
|
||||
}
|
||||
|
||||
// Should have index route but NOT test files
|
||||
expect(files.some((f) => f.includes("_fresh-route___index"))).toBe(true);
|
||||
expect(files.some((f) => f.includes("index_test"))).toBe(false);
|
||||
expect(files.some((f) => f.includes("foo.test"))).toBe(false);
|
||||
|
||||
// Verify no test pattern files at all
|
||||
// Note: files with "_test" or ".test" in their name (not just a "tests" folder)
|
||||
const testPatterns = ["_test.", "_test-", ".test."];
|
||||
for (const file of files) {
|
||||
for (const pattern of testPatterns) {
|
||||
expect(file.includes(pattern)).toBe(false);
|
||||
}
|
||||
}
|
||||
},
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"imports": {
|
||||
"fresh": "jsr:@fresh/core@^2.0.0",
|
||||
"@fresh/plugin-vite": "jsr:@fresh/plugin-vite@^1.0.0",
|
||||
"preact": "npm:preact@^10.27.2",
|
||||
"preact/": "npm:/preact@^10.27.2/"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
import { App } from "fresh";
|
||||
|
||||
export const app = new App().fsRoutes();
|
||||
@@ -0,0 +1,4 @@
|
||||
// This test file should NOT be bundled into production
|
||||
export const handler = () => {
|
||||
return new Response("This should never be in production");
|
||||
};
|
||||
@@ -0,0 +1,3 @@
|
||||
export default function Home() {
|
||||
return <div>ok</div>;
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
// This test file should NOT be bundled into production
|
||||
export default function TestRoute() {
|
||||
return <div>This should never be in production</div>;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import { defineConfig } from "vite";
|
||||
import { fresh } from "@fresh/plugin-vite";
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [fresh()],
|
||||
});
|
||||
Reference in New Issue
Block a user