-
-
Notifications
You must be signed in to change notification settings - Fork 262
feat(react): add native React Compiler support #1419
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
Open
Boshen
wants to merge
5
commits into
vitejs:main
Choose a base branch
from
Boshen:agent/react-compiler
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.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f030e7f
feat(react): add built-in React Compiler support
Boshen 756ed0d
docs(react): distinguish compiler implementations
Boshen 89c2017
test(react): simplify compiler coverage
Boshen ebe5298
fix(react): address compiler review feedback
Boshen b03e99d
chore(react): bump oxc-transform-react to 0.144.0
Boshen 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
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,139 @@ | ||
| import path from 'node:path' | ||
| import { type Plugin, rolldown } from 'rolldown' | ||
| import { describe, expect, test } from 'vitest' | ||
| import pluginReact, { | ||
| type Options, | ||
| type ReactCompilerOptions, | ||
| } from '../src/index.ts' | ||
|
|
||
| describe('compiler option', () => { | ||
| test('compiles React components', async () => { | ||
| const output = await bundle( | ||
| { compiler: true }, | ||
| ` | ||
| export function App({ name }: { name: string }) { | ||
| return <div>{name}</div> | ||
| } | ||
| `, | ||
| ) | ||
|
|
||
| expect(output.code).toContain('react/compiler-runtime') | ||
| expect(output.code).toMatch(/\bc\(2\)/) | ||
| expect( | ||
| output.map?.sources.some((source) => source.endsWith('entry.tsx')), | ||
| ).toBe(true) | ||
| }) | ||
|
|
||
| test('forwards compiler options', async () => { | ||
| const code = ` | ||
| export function App({ name }) { | ||
| return <div>{name}</div> | ||
| } | ||
| ` | ||
| const unannotated = await bundle( | ||
| { compiler: { compilationMode: 'annotation' } }, | ||
| code, | ||
| ) | ||
| const annotated = await bundle( | ||
| { compiler: { compilationMode: 'annotation' } }, | ||
| ` | ||
| export function App({ name }) { | ||
| 'use memo' | ||
| return <div>{name}</div> | ||
| } | ||
| `, | ||
| ) | ||
| const react18 = await bundle({ compiler: { target: '18' } }, code) | ||
|
|
||
| expect(unannotated.code).not.toContain('react/compiler-runtime') | ||
| expect(annotated.code).toContain('react/compiler-runtime') | ||
| expect(react18.code).toContain('react-compiler-runtime') | ||
| }) | ||
|
|
||
| test('uses the React plugin filters', async () => { | ||
| const output = await bundle( | ||
| { compiler: true, exclude: /entry\.tsx$/ }, | ||
| `export function App({ name }) { return <div>{name}</div> }`, | ||
| ) | ||
|
|
||
| expect(output.code).not.toContain('react/compiler-runtime') | ||
| }) | ||
|
|
||
| test('uses the Vite build sourcemap setting by default', async () => { | ||
| expect((await transformWithBuildConfig({}, false)).map).toBeFalsy() | ||
| expect((await transformWithBuildConfig({}, true)).map).toBeTruthy() | ||
| expect( | ||
| (await transformWithBuildConfig({ sourcemap: false }, true)).map, | ||
| ).toBeFalsy() | ||
| expect( | ||
| (await transformWithBuildConfig({ sourcemap: true }, false)).map, | ||
| ).toBeTruthy() | ||
| }) | ||
| }) | ||
|
|
||
| async function transformWithBuildConfig( | ||
| compiler: ReactCompilerOptions, | ||
| buildSourcemap: boolean, | ||
| ) { | ||
| const plugin = pluginReact({ compiler }).find( | ||
| (plugin) => plugin.name === 'vite:react-compiler', | ||
| )! | ||
| const context = { | ||
| error(message: unknown): never { | ||
| throw new Error(String(message)) | ||
| }, | ||
| warn() {}, | ||
| } | ||
|
|
||
| if (typeof plugin.config !== 'function') | ||
| throw new Error('Missing config hook') | ||
| await plugin.config.call( | ||
| context as any, | ||
| {}, | ||
| { command: 'build', mode: 'production' }, | ||
| ) | ||
|
|
||
| if (typeof plugin.configResolved !== 'function') { | ||
| throw new Error('Missing configResolved hook') | ||
| } | ||
| await plugin.configResolved.call( | ||
| context as any, | ||
| { | ||
| command: 'build', | ||
| build: { sourcemap: buildSourcemap }, | ||
| } as any, | ||
| ) | ||
|
|
||
| if (typeof plugin.transform !== 'object') { | ||
| throw new Error('Missing transform hook') | ||
| } | ||
| return plugin.transform.handler.call( | ||
| context as any, | ||
| `export function App({ name }) { return <div>{name}</div> }`, | ||
| '/entry.tsx', | ||
| ) | ||
| } | ||
|
|
||
| async function bundle(options: Options, code: string) { | ||
| const entry = '/entry.tsx' | ||
| const build = await rolldown({ | ||
| input: entry, | ||
| plugins: [virtualFilePlugin(entry, code), pluginReact(options)], | ||
| external: [/^react(\/|$)/, /^react-compiler-runtime$/], | ||
| }) | ||
| const { output } = await build.generate({ format: 'esm', sourcemap: true }) | ||
| return output[0] | ||
| } | ||
|
|
||
| function virtualFilePlugin(entry: string, code: string): Plugin { | ||
| return { | ||
| name: 'virtual-file', | ||
| resolveId(id, importer) { | ||
| const baseDir = importer ? path.posix.dirname(importer) : '/' | ||
| if (path.posix.resolve(baseDir, id) === entry) return entry | ||
| }, | ||
| load(id) { | ||
| if (id === entry) return code | ||
| }, | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
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.
Having this plugin doing this transformation with JSX preserve and then the builtin rolldown doing the jsx transformation will make the jsxDev output have wrong line numbers. This breaks various plugin and browser extensions that are using this to jump from the client to the editor in dev mode.
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.
Re both comments:
oxc-transform-reactis intended to provide or react related transforms.For
@vitejs/plugin-react, I want to keep the blast radius minimal so it only uses the react compiler transform fromoxc-transform-react.Is this a bug that I should fix? Does it happen with babel react compiler as well?