-
-
Notifications
You must be signed in to change notification settings - Fork 213
feat: add plugin-react-oxc #439
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
Merged
Merged
Changes from 21 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
a95aa0b
feat: add plugin-react-oxc
sapphi-red 7dcb6a0
chore: use overrides to run test
sapphi-red e9cfe69
chore: fix types
sapphi-red 12158ce
chore: add oxc plugin to issue templates
sapphi-red 26479d9
chore: revert overrides
sapphi-red 9920d72
chore: setup tests
sapphi-red 3f8820f
chore: merge main
sapphi-red 8d56953
chore: run tests
sapphi-red 592add5
chore: merge main
sapphi-red 5b41dc5
chore: use pnpm catalog
sapphi-red 73a45a5
feat: add rolldown-vite only error
sapphi-red efb8976
docs: add link to rolldown-vite page in README
sapphi-red bbb3a73
chore: add description and keyword fields
sapphi-red 980b26b
chore: merge main
sapphi-red aa98f2c
chore: merge main
sapphi-red 45a177f
refactor: use react-common
sapphi-red b9a884e
fix: fix sourcemap for oxc plugin
sapphi-red 0d834be
docs: update misc
sapphi-red 700af5b
refactor: remove react-refresh dep
sapphi-red 76dbc77
fix: call JSON.parse only when needed
sapphi-red 2b00637
fix: sourcemap line count
sapphi-red 8f90cb7
refactor: remove `map: undefined` from `addRefreshWrapper`
sapphi-red 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,5 @@ | ||
| # Changelog | ||
|
|
||
| ## Unreleased | ||
|
|
||
| - Create Oxc plugin |
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,21 @@ | ||
| MIT License | ||
|
|
||
| Copyright (c) 2019-present, Yuxi (Evan) You and Vite contributors | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. |
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,84 @@ | ||
| # @vitejs/plugin-react-oxc [](https://npmjs.com/package/@vitejs/plugin-react-oxc) | ||
|
|
||
| The future default Vite plugin for React projects. | ||
|
|
||
| - enable [Fast Refresh](https://www.npmjs.com/package/react-refresh) in development | ||
| - use the [automatic JSX runtime](https://legacy.reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html) | ||
| - small installation size | ||
|
|
||
| ```js | ||
| // vite.config.js | ||
| import { defineConfig } from 'vite' | ||
| import react from '@vitejs/plugin-react-oxc' | ||
|
|
||
| export default defineConfig({ | ||
| plugins: [react()], | ||
| }) | ||
| ``` | ||
|
|
||
| ## Caveats | ||
|
|
||
| - `jsx runtime` is always `automatic` | ||
| - this plugin only works with [`rolldown-vite`](https://vitejs.dev/guide/rolldown) | ||
|
|
||
| ## Options | ||
|
|
||
| ### include/exclude | ||
|
|
||
| Includes `.js`, `.jsx`, `.ts` & `.tsx` by default. This option can be used to add fast refresh to `.mdx` files: | ||
|
|
||
| ```js | ||
| import { defineConfig } from 'vite' | ||
| import react from '@vitejs/plugin-react' | ||
| import mdx from '@mdx-js/rollup' | ||
|
|
||
| export default defineConfig({ | ||
| plugins: [ | ||
| { enforce: 'pre', ...mdx() }, | ||
| react({ include: /\.(mdx|js|jsx|ts|tsx)$/ }), | ||
| ], | ||
| }) | ||
| ``` | ||
|
|
||
| > `node_modules` are never processed by this plugin (but Oxc will) | ||
|
|
||
| ### jsxImportSource | ||
|
|
||
| Control where the JSX factory is imported from. Default to `'react'` | ||
|
|
||
| ```js | ||
| react({ jsxImportSource: '@emotion/react' }) | ||
| ``` | ||
|
|
||
| ## Middleware mode | ||
|
|
||
| In [middleware mode](https://vite.dev/config/server-options.html#server-middlewaremode), you should make sure your entry `index.html` file is transformed by Vite. Here's an example for an Express server: | ||
|
|
||
| ```js | ||
| app.get('/', async (req, res, next) => { | ||
| try { | ||
| let html = fs.readFileSync(path.resolve(root, 'index.html'), 'utf-8') | ||
|
|
||
| // Transform HTML using Vite plugins. | ||
| html = await viteServer.transformIndexHtml(req.url, html) | ||
|
|
||
| res.send(html) | ||
| } catch (e) { | ||
| return next(e) | ||
| } | ||
| }) | ||
| ``` | ||
|
|
||
| Otherwise, you'll probably get this error: | ||
|
|
||
| ``` | ||
| Uncaught Error: @vitejs/plugin-react-oxc can't detect preamble. Something is wrong. | ||
| ``` | ||
|
|
||
| ## Consistent components exports | ||
|
|
||
| For React refresh to work correctly, your file should only export React components. You can find a good explanation in the [Gatsby docs](https://www.gatsbyjs.com/docs/reference/local-development/fast-refresh/#how-it-works). | ||
|
|
||
| If an incompatible change in exports is found, the module will be invalidated and HMR will propagate. To make it easier to export simple constants alongside your component, the module is only invalidated when their value changes. | ||
|
|
||
| You can catch mistakes and get more detailed warning with this [eslint rule](https://github.com/ArnaudBarre/eslint-plugin-react-refresh). |
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,11 @@ | ||
| import { defineBuildConfig } from 'unbuild' | ||
|
|
||
| export default defineBuildConfig({ | ||
| entries: ['src/index'], | ||
| externals: ['vite'], | ||
| clean: true, | ||
| declaration: true, | ||
| rollup: { | ||
| inlineDependencies: true, | ||
| }, | ||
| }) |
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,50 @@ | ||
| { | ||
| "name": "@vitejs/plugin-react-oxc", | ||
| "version": "0.1.0", | ||
| "license": "MIT", | ||
| "author": "Evan You", | ||
| "contributors": [ | ||
| "Alec Larson", | ||
| "Arnaud Barré" | ||
| ], | ||
| "description": "The future default Vite plugin for React projects", | ||
| "keywords": [ | ||
| "vite", | ||
| "vite-plugin", | ||
| "react", | ||
| "oxc", | ||
| "react-refresh", | ||
| "fast refresh" | ||
| ], | ||
| "files": [ | ||
| "dist" | ||
| ], | ||
| "type": "module", | ||
| "types": "./dist/index.d.mts", | ||
| "exports": "./dist/index.mjs", | ||
| "scripts": { | ||
| "dev": "unbuild --stub", | ||
| "build": "unbuild && tsx scripts/copyRefreshRuntime.ts", | ||
| "prepublishOnly": "npm run build" | ||
| }, | ||
| "engines": { | ||
| "node": ">=20.0.0" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/vitejs/vite-plugin-react.git", | ||
| "directory": "packages/plugin-react-oxc" | ||
| }, | ||
| "bugs": { | ||
| "url": "https://github.com/vitejs/vite-plugin-react/issues" | ||
| }, | ||
| "homepage": "https://github.com/vitejs/vite-plugin-react/tree/main/packages/plugin-react#readme", | ||
| "peerDependencies": { | ||
| "vite": "^6.3.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@vitejs/react-common": "workspace:*", | ||
| "unbuild": "^3.5.0", | ||
| "vite": "catalog:rolldown-vite" | ||
| } | ||
| } |
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,6 @@ | ||
| import { copyFileSync } from 'node:fs' | ||
|
|
||
| copyFileSync( | ||
| 'node_modules/@vitejs/react-common/refresh-runtime.js', | ||
| 'dist/refresh-runtime.js', | ||
| ) |
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.
Uh oh!
There was an error while loading. Please reload this page.