-
Notifications
You must be signed in to change notification settings - Fork 57
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(@netlify/build-info): add React Router 7 detection #5930
Open
serhalp
wants to merge
3
commits into
main
Choose a base branch
from
serhalp/frb-1511-support-react-router-7
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+192
−0
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,75 @@ | ||
import { beforeEach, expect, test } from 'vitest' | ||
|
||
import { mockFileSystem } from '../../tests/mock-file-system.js' | ||
import { NodeFS } from '../node/file-system.js' | ||
import { Project } from '../project.js' | ||
|
||
beforeEach((ctx) => { | ||
ctx.fs = new NodeFS() | ||
}) | ||
test('detects a React Router v7 site', async ({ fs }) => { | ||
const cwd = mockFileSystem({ | ||
'react-router.config.ts': '', | ||
'vite.config.ts': '', | ||
'package.json': JSON.stringify({ | ||
scripts: { | ||
build: 'react-router build', | ||
dev: 'react-router dev', | ||
start: 'react-router-serve ./build/server/index.js', | ||
typecheck: 'react-router typegen && tsc', | ||
}, | ||
dependencies: { | ||
'@react-router/node': '^7.0.2', | ||
'@react-router/serve': '^7.0.2', | ||
react: '^18.3.1', | ||
'react-dom': '^18.3.1', | ||
'react-router': '^7.0.2', | ||
}, | ||
devDependencies: { | ||
'@netlify/vite-plugin-react-router': '^1.0.0', | ||
'@react-router/dev': '^7.0.2', | ||
typescript: '^5.6.3', | ||
vite: '^5.4.11', | ||
'vite-tsconfig-paths': '^5.1.2', | ||
}, | ||
}), | ||
}) | ||
const detected = await new Project(fs, cwd).detectFrameworks() | ||
|
||
const detectedFrameworks = (detected ?? []).map((framework) => framework.id) | ||
expect(detectedFrameworks).not.toContain('remix') | ||
|
||
expect(detected?.[0]?.id).toBe('react-router') | ||
expect(detected?.[0]?.build?.command).toBe('react-router build') | ||
expect(detected?.[0]?.build?.directory).toBe('build/client') | ||
expect(detected?.[0]?.dev?.command).toBe('react-router dev') | ||
expect(detected?.[0]?.dev?.port).toBe(5173) | ||
}) | ||
|
||
test('does NOT detect a React Router <v7 site', async ({ fs }) => { | ||
const cwd = mockFileSystem({ | ||
'vite.config.ts': '', | ||
'package.json': JSON.stringify({ | ||
scripts: { | ||
build: 'vite build', | ||
dev: 'vite dev', | ||
}, | ||
dependencies: { | ||
react: '^18.2.0', | ||
'react-dom': '^18.2.0', | ||
'react-router-dom': '^6.15.0', | ||
}, | ||
devDependencies: { | ||
'@vitejs/plugin-react': '^3.0.1', | ||
typescript: '^4.9.5', | ||
vite: '^4.0.4', | ||
}, | ||
}), | ||
}) | ||
const detected = await new Project(fs, cwd).detectFrameworks() | ||
|
||
const detectedFrameworks = (detected ?? []).map((framework) => framework.id) | ||
expect(detectedFrameworks).not.toContain('react-router') | ||
|
||
expect(detected?.[0]?.id).toBe('vite') | ||
}) |
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,40 @@ | ||
import { lt } from 'semver' | ||
|
||
import { BaseFramework, Category, DetectedFramework, Framework } from './framework.js' | ||
|
||
export class ReactRouter extends BaseFramework implements Framework { | ||
readonly id = 'react-router' | ||
name = 'React Router' | ||
configFiles = ['react-router.config.ts', 'react-router.config.js'] | ||
npmDependencies = ['react-router'] | ||
category = Category.SSG | ||
|
||
dev = { | ||
port: 5173, | ||
command: 'react-router dev', | ||
} | ||
|
||
build = { | ||
command: 'react-router build', | ||
directory: 'build/client', | ||
} | ||
|
||
logo = { | ||
default: '/logos/react-router/light.svg', | ||
light: '/logos/react-router/light.svg', | ||
dark: '/logos/react-router/dark.svg', | ||
} | ||
|
||
async detect(): Promise<DetectedFramework | undefined> { | ||
await super.detect() | ||
|
||
if (this.detected) { | ||
// React Router wasn't a framework before v7. As of v7, it's... Remix. | ||
if (this.version && lt(this.version, '7.0.0')) { | ||
return | ||
} | ||
|
||
return this as DetectedFramework | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do we want to detect the plugin we built as well?
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.
Good question. We've been doing that for some of the frameworks, but I was thinking about it and couldn't come up with any reason for it, so I didn't include it. You definitely need react-router to use react-router; checking for additional dependencies won't help.