Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/plugin-react-swc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ react({
})
```

### disableOxcRecommendation

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

```ts
react({ disableOxcRecommendation: true })
```

## Consistent components exports

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).
Expand Down
25 changes: 24 additions & 1 deletion packages/plugin-react-swc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import {
transform,
} from '@swc/core'
import type { PluginOption } from 'vite'
import * as vite from 'vite'
import {
addRefreshWrapper,
getPreambleCode,
runtimePublicPath,
silenceUseClientWarning,
} from '@vitejs/react-common'
import * as vite from 'vite'
import { exactRegex } from '@rolldown/pluginutils'

/* eslint-disable no-restricted-globals */
Expand Down Expand Up @@ -76,6 +76,11 @@ type Options = {
* feature doesn't work is not fun, so we won't provide support for it, hence the name `useAtYourOwnRisk`
*/
useAtYourOwnRisk_mutateSwcOptions?: (options: SWCOptions) => void

/**
* If set, disables the recommendation to use `vite-plugin-react-oxc`
*/
disableOxcRecommendation?: boolean
}

const react = (_options?: Options): PluginOption[] => {
Expand All @@ -91,6 +96,7 @@ const react = (_options?: Options): PluginOption[] => {
reactRefreshHost: _options?.reactRefreshHost,
useAtYourOwnRisk_mutateSwcOptions:
_options?.useAtYourOwnRisk_mutateSwcOptions,
disableOxcRecommendation: _options?.disableOxcRecommendation,
}

return [
Expand Down Expand Up @@ -144,6 +150,23 @@ const react = (_options?: Options): PluginOption[] => {
'[vite:react-swc] The MDX plugin should be placed before this plugin',
)
}

/*
* Suggest to use vite-plugin-react-oxc if `rolldown-vite` is used and:
* No swc plugins are set
* mutateSwcOptions is not set
* It is not disabled by the user
*/
if (
'rolldownVersion' in vite &&
!options.plugins &&
!options.useAtYourOwnRisk_mutateSwcOptions &&
!options.disableOxcRecommendation
) {
config.logger.warn(
'[vite:react-swc] We recommend switching to `vite-plugin-react-oxc` for improved performance as no swc plugins are used. More information at https://vite.dev/rolldown',
)
}
},
transformIndexHtml: (_, config) => {
if (!hmrDisabled) {
Expand Down
4 changes: 4 additions & 0 deletions packages/plugin-react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ Otherwise, you'll probably get this error:
Uncaught Error: @vitejs/plugin-react can't detect preamble. Something is wrong.
```

### disableOxcRecommendation

If set, disables the recommendation to use `@vitejs/plugin-react-oxc` (which is shown when `rolldown-vite` is detected and `babel` is not configured).

## 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).
Expand Down
22 changes: 22 additions & 0 deletions packages/plugin-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ export interface Options {
* reactRefreshHost: 'http://localhost:3000'
*/
reactRefreshHost?: string

/**
* If set, disables the recommendation to use `vite-plugin-react-oxc`
*/
disableOxcRecommendation?: boolean
}

export type BabelOptions = Omit<
Expand Down Expand Up @@ -182,6 +187,23 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
.map((plugin) => plugin.api?.reactBabel)
.filter(defined)

if (
'rolldownVersion' in vite &&
!opts.babel &&
!hooks.length &&
!opts.disableOxcRecommendation
) {
/*
* Suggest to use vite-plugin-react-oxc if `rolldown-vite` is used and:
* No babel config is set
* No other plugin is using the `api.reactBabel` hook
* It is not disabled by the user
*/
config.logger.warn(
'[vite:react-babel] We recommend switching to `vite-plugin-react-oxc` for improved performance. More information at https://vite.dev/rolldown',
)
}

if (hooks.length > 0) {
runPluginOverrides = (babelOptions, context) => {
hooks.forEach((hook) => hook(babelOptions, context, config))
Expand Down
Loading