Skip to content

Commit

Permalink
fix: add jsx dev runtime to optimizeDeps (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBarre committed Apr 17, 2023
1 parent f8217d6 commit 3bbd8f0
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/plugin-react/README.md
Expand Up @@ -40,7 +40,7 @@ export default defineConfig({
### jsxImportSource

Control where the JSX factory is imported from. For TS projects this is inferred from the tsconfig. If you have some React code outside JSX/TSX files, this will be used to detect the presence of React code and apply Fast Refresh.
Control where the JSX factory is imported from. Default to `'react'`

```js
react({ jsxImportSource: '@emotion/react' })
Expand Down
9 changes: 4 additions & 5 deletions packages/plugin-react/src/index.ts
Expand Up @@ -29,7 +29,7 @@ export interface Options {
/**
* Control where the JSX factory is imported from.
* https://esbuild.github.io/api/#jsx-import-source
* For TS projects this is read from tsconfig
* @default 'react'
*/
jsxImportSource?: string
/**
Expand Down Expand Up @@ -91,6 +91,7 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
// Provide default values for Rollup compat.
let devBase = '/'
const filter = createFilter(opts.include ?? defaultIncludeRE, opts.exclude)
const devRuntime = `${opts.jsxImportSource ?? 'react'}/jsx-dev-runtime`
let needHiresSourcemap = false
let isProduction = true
let projectRoot = process.cwd()
Expand Down Expand Up @@ -181,9 +182,7 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
!ssr &&
(isJSX ||
(opts.jsxRuntime === 'classic'
? code.includes(
`${opts.jsxImportSource ?? 'react'}/jsx-dev-runtime`,
)
? code.includes(devRuntime)
: importReactRE.test(code)))
if (useFastRefresh) {
plugins.push([
Expand Down Expand Up @@ -280,7 +279,7 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
// 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'],
include: ['react', devRuntime],
},
resolve: {
dedupe: ['react', 'react-dom'],
Expand Down
1 change: 0 additions & 1 deletion playground/ssr-react/vite.config.js
Expand Up @@ -3,7 +3,6 @@ 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 3bbd8f0

Please sign in to comment.