Skip to content

Commit

Permalink
fix!: don't force optimization of jsx-runtime (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBarre committed Mar 30, 2023
1 parent 7663e92 commit e26bb67
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 24 deletions.
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

0 comments on commit e26bb67

Please sign in to comment.