-
-
Notifications
You must be signed in to change notification settings - Fork 201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add plugin for React router 7 framework mode #931
base: main
Are you sure you want to change the base?
Changes from all commits
390877d
22f0a57
bd3dc32
65f4823
5722825
615b28b
ea73062
0747255
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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" }], | ||
}, | ||
]; | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"name": "@fixtures/react-router", | ||
"version": "*", | ||
"devDependencies": { | ||
"@react-router/dev": "*" | ||
} | ||
} |
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; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
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: string[] = ['react-router.config.{js,ts}', ...vite.config]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added vite.config aswell because the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All Since this plugin doesn't have Just saying, the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any chance you could move There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moving |
||
|
||
const entry: string[] = []; | ||
|
||
const production: string[] = []; | ||
|
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe good idea to check if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I initialize There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, gotcha. Good! |
||
} | ||
|
||
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; |
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[]; | ||
} |
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, | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seeing things in the docs like
route("/", "./home.tsx")
- do we cater for that as well? or how's that returned when loadingapp/routes.ts
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
route("/", "./home.tsx")
returns a simple object containing{ file: "./home.tsx", path: "/" }
. Since we are joining the path like this:join(appDir, route.file)
and theroutes.ts
file is defined inappDir
this is catered for.