Skip to content

Commit 97bb283

Browse files
authored
Support absolute file paths in react-router routes (#1659)
1 parent 0a6e93d commit 97bb283

9 files changed

Lines changed: 42 additions & 1 deletion

File tree

packages/knip/fixtures/plugins/react-router-with-absolute-paths/app/root.tsx

Whitespace-only changes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { fileURLToPath } from 'node:url';
2+
import { resolve, dirname } from 'node:path';
3+
4+
const dir = dirname(fileURLToPath(import.meta.url));
5+
6+
export default [
7+
{ file: resolve(dir, 'routes/home.tsx'), index: true },
8+
{
9+
file: resolve(dir, 'routes/layout.tsx'),
10+
children: [{ file: resolve(dir, 'routes/child.tsx') }],
11+
},
12+
];

packages/knip/fixtures/plugins/react-router-with-absolute-paths/app/routes/child.tsx

Whitespace-only changes.

packages/knip/fixtures/plugins/react-router-with-absolute-paths/app/routes/home.tsx

Whitespace-only changes.

packages/knip/fixtures/plugins/react-router-with-absolute-paths/app/routes/layout.tsx

Whitespace-only changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "@plugins/react-router-with-absolute-paths",
3+
"dependencies": {
4+
"@react-router/node": "*",
5+
"isbot": "*"
6+
},
7+
"devDependencies": {
8+
"@react-router/dev": "*"
9+
}
10+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { Config } from '@react-router/dev/config';
2+
3+
export default {
4+
// Config options...
5+
// Server-side render by default, to enable SPA mode set this to `false`
6+
ssr: true,
7+
} satisfies Config;

packages/knip/src/plugins/react-router/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const resolveConfig: ResolveConfig<PluginConfig> = async (localConfig, options)
4141
}
4242

4343
const mapRoute = (route: RouteConfigEntry): string[] => {
44-
return [join(appDir, route.file), ...(route.children ? route.children.flatMap(mapRoute) : [])];
44+
return [toAbsolute(route.file, appDir), ...(route.children ? route.children.flatMap(mapRoute) : [])];
4545
};
4646

4747
const routes = routeConfig

packages/knip/test/plugins/react-router.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ test('Find dependencies with the react-router plugin', async () => {
2323
});
2424
});
2525

26+
test('Find dependencies with the react-router plugin [with absolute paths]', async () => {
27+
const cwd = resolve('fixtures/plugins/react-router-with-absolute-paths');
28+
const options = await createOptions({ cwd });
29+
const { counters } = await main(options);
30+
31+
assert.deepEqual(counters, {
32+
...baseCounters,
33+
processed: 6,
34+
total: 6,
35+
});
36+
});
37+
2638
test('Find dependencies with the react-router plugin [with custom server entry]', async () => {
2739
const cwd = resolve('fixtures/plugins/react-router-with-server-entry');
2840
const options = await createOptions({ cwd });

0 commit comments

Comments
 (0)