Skip to content

Commit c450133

Browse files
feat: suggest vite-plugin-react-oxc when using rolldown-vite (#491)
Co-authored-by: 翠 <[email protected]>
1 parent 4bec551 commit c450133

File tree

6 files changed

+55
-1
lines changed

6 files changed

+55
-1
lines changed

packages/plugin-react-swc/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Suggest `@vitejs/plugin-react-oxc` if rolldown-vite is detected [#491](https://github.com/vitejs/vite-plugin-react/pull/491)
6+
7+
Emit a log which recommends `@vitejs/plugin-react-oxc` when `rolldown-vite` is detected to improve performance and use Oxc under the hood. The warning can be disabled by setting `disableOxcRecommendation: false` in the plugin options.
8+
59
### Use `optimizeDeps.rollupOptions` instead of `optimizeDeps.esbuildOptions` for rolldown-vite [#489](https://github.com/vitejs/vite-plugin-react/pull/489)
610

711
This suppresses the warning about `optimizeDeps.esbuildOptions` being deprecated in rolldown-vite.

packages/plugin-react-swc/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,14 @@ react({
117117
})
118118
```
119119

120+
### disableOxcRecommendation
121+
122+
If set, disables the recommendation to use `@vitejs/plugin-react-oxc` (which is shown when `rolldown-vite` is detected and neither `swc` plugins are used nor the `swc` options are mutated).
123+
124+
```ts
125+
react({ disableOxcRecommendation: true })
126+
```
127+
120128
## Consistent components exports
121129

122130
For React refresh to work correctly, your file should only export React components. The best explanation I've read is the one from the [Gatsby docs](https://www.gatsbyjs.com/docs/reference/local-development/fast-refresh/#how-it-works).

packages/plugin-react-swc/src/index.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import {
1212
transform,
1313
} from '@swc/core'
1414
import type { PluginOption } from 'vite'
15-
import * as vite from 'vite'
1615
import {
1716
addRefreshWrapper,
1817
getPreambleCode,
1918
runtimePublicPath,
2019
silenceUseClientWarning,
2120
} from '@vitejs/react-common'
21+
import * as vite from 'vite'
2222
import { exactRegex } from '@rolldown/pluginutils'
2323

2424
/* eslint-disable no-restricted-globals */
@@ -76,6 +76,11 @@ type Options = {
7676
* feature doesn't work is not fun, so we won't provide support for it, hence the name `useAtYourOwnRisk`
7777
*/
7878
useAtYourOwnRisk_mutateSwcOptions?: (options: SWCOptions) => void
79+
80+
/**
81+
* If set, disables the recommendation to use `@vitejs/plugin-react-oxc`
82+
*/
83+
disableOxcRecommendation?: boolean
7984
}
8085

8186
const react = (_options?: Options): PluginOption[] => {
@@ -91,6 +96,7 @@ const react = (_options?: Options): PluginOption[] => {
9196
reactRefreshHost: _options?.reactRefreshHost,
9297
useAtYourOwnRisk_mutateSwcOptions:
9398
_options?.useAtYourOwnRisk_mutateSwcOptions,
99+
disableOxcRecommendation: _options?.disableOxcRecommendation,
94100
}
95101

96102
return [
@@ -144,6 +150,17 @@ const react = (_options?: Options): PluginOption[] => {
144150
'[vite:react-swc] The MDX plugin should be placed before this plugin',
145151
)
146152
}
153+
154+
if (
155+
'rolldownVersion' in vite &&
156+
!options.plugins &&
157+
!options.useAtYourOwnRisk_mutateSwcOptions &&
158+
!options.disableOxcRecommendation
159+
) {
160+
config.logger.warn(
161+
'[vite:react-swc] We recommend switching to `@vitejs/plugin-react-oxc` for improved performance as no swc plugins are used. More information at https://vite.dev/rolldown',
162+
)
163+
}
147164
},
148165
transformIndexHtml: (_, config) => {
149166
if (!hmrDisabled) {

packages/plugin-react/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## Unreleased
44

5+
### Suggest `@vitejs/plugin-react-oxc` if rolldown-vite is detected [#491](https://github.com/vitejs/vite-plugin-react/pull/491)
6+
7+
Emit a log which recommends `@vitejs/plugin-react-oxc` when `rolldown-vite` is detected to improve performance and use Oxc under the hood. The warning can be disabled by setting `disableOxcRecommendation: false` in the plugin options.
8+
9+
510
### Use `optimizeDeps.rollupOptions` instead of `optimizeDeps.esbuildOptions` for rolldown-vite [#489](https://github.com/vitejs/vite-plugin-react/pull/489)
611

712
This suppresses the warning about `optimizeDeps.esbuildOptions` being deprecated in rolldown-vite.

packages/plugin-react/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ Otherwise, you'll probably get this error:
129129
Uncaught Error: @vitejs/plugin-react can't detect preamble. Something is wrong.
130130
```
131131

132+
### disableOxcRecommendation
133+
134+
If set, disables the recommendation to use `@vitejs/plugin-react-oxc` (which is shown when `rolldown-vite` is detected and `babel` is not configured).
135+
132136
## Consistent components exports
133137

134138
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).

packages/plugin-react/src/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ export interface Options {
6262
* reactRefreshHost: 'http://localhost:3000'
6363
*/
6464
reactRefreshHost?: string
65+
66+
/**
67+
* If set, disables the recommendation to use `@vitejs/plugin-react-oxc`
68+
*/
69+
disableOxcRecommendation?: boolean
6570
}
6671

6772
export type BabelOptions = Omit<
@@ -182,6 +187,17 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
182187
.map((plugin) => plugin.api?.reactBabel)
183188
.filter(defined)
184189

190+
if (
191+
'rolldownVersion' in vite &&
192+
!opts.babel &&
193+
!hooks.length &&
194+
!opts.disableOxcRecommendation
195+
) {
196+
config.logger.warn(
197+
'[vite:react-babel] We recommend switching to `@vitejs/plugin-react-oxc` for improved performance. More information at https://vite.dev/rolldown',
198+
)
199+
}
200+
185201
if (hooks.length > 0) {
186202
runPluginOverrides = (babelOptions, context) => {
187203
hooks.forEach((hook) => hook(babelOptions, context, config))

0 commit comments

Comments
 (0)