Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 2 additions & 4 deletions packages/plugin-react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,10 @@ react({

### Configure the JSX import source

Control where the JSX factory is imported from. This option is ignored for classic `jsxRuntime`.
Control where the JSX factory is imported from. For TS projects this is inferred from the tsconfig.

```js
react({
jsxImportSource: '@emotion/react',
})
react({ jsxImportSource: '@emotion/react' })
```

## Babel configuration
Expand Down
29 changes: 9 additions & 20 deletions packages/plugin-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ export interface Options {
jsxRuntime?: 'classic' | 'automatic'
/**
* Control where the JSX factory is imported from.
* This option is ignored when `jsxRuntime` is not `"automatic"`.
* @default "react"
* https://esbuild.github.io/api/#jsx-import-source
* For TS projects this is read from tsconfig
*/
jsxImportSource?: string
/**
Expand Down Expand Up @@ -314,6 +314,12 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
name: 'vite:react-refresh',
enforce: 'pre',
config: () => ({
optimizeDeps: {
// We can't add `react-dom` because the dependency is `react-dom/client`
// for React 18 while it's `react-dom` for React 17. We'd need to detect
// what React version the user has installed.
include: ['react'],
},
resolve: {
dedupe: ['react', 'react-dom'],
},
Expand All @@ -340,24 +346,7 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
},
}

const reactJsxRuntimeId = 'react/jsx-runtime'
const reactJsxDevRuntimeId = 'react/jsx-dev-runtime'
const viteReactJsx: Plugin = {
name: 'vite:react-jsx',
enforce: 'pre',
config() {
return {
optimizeDeps: {
// We can't add `react-dom` because the dependency is `react-dom/client`
// for React 18 while it's `react-dom` for React 17. We'd need to detect
// what React version the user has installed.
include: [reactJsxRuntimeId, reactJsxDevRuntimeId, 'react'],
},
}
},
}

return [viteBabel, viteReactRefresh, useAutomaticRuntime && viteReactJsx]
return [viteBabel, viteReactRefresh]
}

viteReact.preambleCode = preambleCode
Expand Down
1 change: 1 addition & 0 deletions playground/ssr-react/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import react from '@vitejs/plugin-react'

export default defineConfig({
plugins: [react()],
optimizeDeps: { include: ['react/jsx-dev-runtime'] },
build: {
minify: false,
},
Expand Down