Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nervous-timers-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@react-router/fs-routes": patch
---

fix(react-router/fs-routes): correctly apply ignoredFilePatterns to files and folders in flatRoutes
50 changes: 50 additions & 0 deletions packages/react-router-fs-routes/__tests__/flatRoutes-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -912,4 +912,54 @@ describe("flatRoutes", () => {
);
});
});

describe("ignoredFilePatterns", () => {
let tempDir = path.join(
os.tmpdir(),
"react-router-fs-routes-test",
Math.random().toString(36).substring(2, 15),
);
let routesDir = path.join(tempDir, "routes");

beforeEach(() => {
mkdirSync(tempDir, { recursive: true });
mkdirSync(routesDir, { recursive: true });
writeFileSync(path.join(tempDir, "root.tsx"), "");
});
afterEach(() => {
rmSync(tempDir, { recursive: true, force: true });
});

test("should ignore a single file matching the pattern", () => {
let routeOne = path.join(routesDir, "one.tsx");
let routeTwo = path.join(routesDir, "two.tsx");
writeFileSync(routeOne, "");
writeFileSync(routeTwo, "");

let ignoredFilePatterns = ["two.tsx"];
let routeManifest = flatRoutes(tempDir, ignoredFilePatterns);

let routeIds = Object.keys(routeManifest);

expect(routeIds).not.toContain("routes/two");
expect(routeIds.length).toBe(1);
});

test("should ignore all files in a folder matching the pattern", () => {
let routeOne = path.join(routesDir, "one.tsx");
let routeTwoDir = path.join(routesDir, "two");
let routeTwo = path.join(routeTwoDir, "route.tsx");
writeFileSync(routeOne, "");
mkdirSync(routeTwoDir, { recursive: true });
writeFileSync(routeTwo, "");

let ignoredFilePatterns = ["two"];
let routeManifest = flatRoutes(tempDir, ignoredFilePatterns);

let routeIds = Object.keys(routeManifest);

expect(routeIds).not.toContain("routes/two");
expect(routeIds.length).toBe(1);
});
});
});
10 changes: 6 additions & 4 deletions packages/react-router-fs-routes/flatRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,12 @@ export function flatRoutes(
if (entry.isDirectory()) {
route = findRouteModuleForFolder(
appDirectory,
routesDir,
filepath,
ignoredFileRegex,
);
} else if (entry.isFile()) {
route = findRouteModuleForFile(appDirectory, filepath, ignoredFileRegex);
route = findRouteModuleForFile(routesDir, filepath, ignoredFileRegex);
}

if (route) routes.push(route);
Expand Down Expand Up @@ -297,22 +298,23 @@ export function flatRoutesUniversal(
}

function findRouteModuleForFile(
appDirectory: string,
routesDir: string,
filepath: string,
ignoredFileRegex: RegExp[],
): string | null {
let relativePath = normalizeSlashes(path.relative(appDirectory, filepath));
let relativePath = normalizeSlashes(path.relative(routesDir, filepath));
let isIgnored = ignoredFileRegex.some((regex) => regex.test(relativePath));
if (isIgnored) return null;
return filepath;
}

function findRouteModuleForFolder(
appDirectory: string,
routesDir: string,
filepath: string,
ignoredFileRegex: RegExp[],
): string | null {
let relativePath = path.relative(appDirectory, filepath);
let relativePath = path.relative(routesDir, filepath);
let isIgnored = ignoredFileRegex.some((regex) => regex.test(relativePath));
if (isIgnored) return null;

Expand Down