-
-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add plugin for React router 7 framework mode (#931)
Co-authored-by: Lars Kappert <lars@webpro.nl>
- Loading branch information
1 parent
2afffe0
commit 012fd5b
Showing
16 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default [ | ||
{ file: "routes/home.tsx", index: true }, | ||
{ | ||
file: "routes/layout.tsx", | ||
children: [{ file: "./routes/another-route.tsx" }], | ||
}, | ||
]; |
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"name": "@fixtures/react-router", | ||
"version": "*", | ||
"devDependencies": { | ||
"@react-router/dev": "*" | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
packages/knip/fixtures/plugins/react-router/react-router.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import type { Config } from "@react-router/dev/config"; | ||
|
||
export default { | ||
// Config options... | ||
// Server-side render by default, to enable SPA mode set this to `false` | ||
ssr: true, | ||
} satisfies Config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { existsSync } from 'node:fs'; | ||
import type { IsPluginEnabled, Plugin, ResolveEntryPaths } from '../../types/config.js'; | ||
import { toEntry } from '../../util/input.js'; | ||
import { join } from '../../util/path.js'; | ||
import { hasDependency, load } from '../../util/plugin.js'; | ||
import vite from '../vite/index.js'; | ||
import type { PluginConfig, RouteConfigEntry } from './types.js'; | ||
|
||
// https://reactrouter.com/start/framework/routing | ||
|
||
const title = 'react-router'; | ||
|
||
const enablers = ['@react-router/dev']; | ||
|
||
const isEnabled: IsPluginEnabled = ({ dependencies }) => hasDependency(dependencies, enablers); | ||
|
||
const config = ['react-router.config.{js,ts}', ...vite.config]; | ||
|
||
const resolveEntryPaths: ResolveEntryPaths<PluginConfig> = async (localConfig, options) => { | ||
const { configFileDir } = options; | ||
const appDirectory = localConfig.appDirectory ?? 'app'; | ||
const appDir = join(configFileDir, appDirectory); | ||
|
||
// If using flatRoutes from @react-router/fs-routes it will throw an error if this variable is not defined | ||
// @ts-ignore | ||
globalThis.__reactRouterAppDirectory = appDir; | ||
|
||
let routeConfig: RouteConfigEntry[] = []; | ||
|
||
const routesPathTs = join(appDir, 'routes.ts'); | ||
const routesPathJs = join(appDir, 'routes.js'); | ||
|
||
if (existsSync(routesPathTs)) { | ||
routeConfig = await load(routesPathTs); | ||
} else if (existsSync(routesPathJs)) { | ||
routeConfig = await load(routesPathJs); | ||
} | ||
|
||
const mapRoute = (route: RouteConfigEntry): string[] => { | ||
return [join(appDir, route.file), ...(route.children ? route.children.flatMap(mapRoute) : [])]; | ||
}; | ||
|
||
const routes = routeConfig.flatMap(mapRoute); | ||
|
||
return [ | ||
join(appDir, 'routes.{js,ts}'), | ||
join(appDir, 'root.{jsx,tsx}'), | ||
join(appDir, 'entry.{client,server}.{js,jsx,ts,tsx}'), | ||
...routes, | ||
].map(toEntry); | ||
}; | ||
|
||
export default { | ||
title, | ||
enablers, | ||
isEnabled, | ||
config, | ||
entry, | ||
production, | ||
resolveEntryPaths, | ||
} satisfies Plugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export type PluginConfig = { | ||
appDirectory?: string; | ||
}; | ||
|
||
export interface RouteConfigEntry { | ||
file: string; | ||
children?: RouteConfigEntry[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { test } from 'bun:test'; | ||
import assert from 'node:assert/strict'; | ||
import { main } from '../../src/index.js'; | ||
import { resolve } from '../../src/util/path.js'; | ||
import baseArguments from '../helpers/baseArguments.js'; | ||
import baseCounters from '../helpers/baseCounters.js'; | ||
|
||
const cwd = resolve('fixtures/plugins/react-router'); | ||
|
||
test('Find dependencies with the react-router plugin', async () => { | ||
const { counters } = await main({ | ||
...baseArguments, | ||
cwd, | ||
}); | ||
|
||
assert.deepEqual(counters, { | ||
...baseCounters, | ||
processed: 8, | ||
total: 8, | ||
}); | ||
}); |