Skip to content

Commit 3bbd8f0

Browse files
authored
fix: add jsx dev runtime to optimizeDeps (#147)
1 parent f8217d6 commit 3bbd8f0

File tree

3 files changed

+5
-7
lines changed

3 files changed

+5
-7
lines changed

packages/plugin-react/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export default defineConfig({
4040
4141
### jsxImportSource
4242

43-
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.
43+
Control where the JSX factory is imported from. Default to `'react'`
4444

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

packages/plugin-react/src/index.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export interface Options {
2929
/**
3030
* Control where the JSX factory is imported from.
3131
* https://esbuild.github.io/api/#jsx-import-source
32-
* For TS projects this is read from tsconfig
32+
* @default 'react'
3333
*/
3434
jsxImportSource?: string
3535
/**
@@ -91,6 +91,7 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
9191
// Provide default values for Rollup compat.
9292
let devBase = '/'
9393
const filter = createFilter(opts.include ?? defaultIncludeRE, opts.exclude)
94+
const devRuntime = `${opts.jsxImportSource ?? 'react'}/jsx-dev-runtime`
9495
let needHiresSourcemap = false
9596
let isProduction = true
9697
let projectRoot = process.cwd()
@@ -181,9 +182,7 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
181182
!ssr &&
182183
(isJSX ||
183184
(opts.jsxRuntime === 'classic'
184-
? code.includes(
185-
`${opts.jsxImportSource ?? 'react'}/jsx-dev-runtime`,
186-
)
185+
? code.includes(devRuntime)
187186
: importReactRE.test(code)))
188187
if (useFastRefresh) {
189188
plugins.push([
@@ -280,7 +279,7 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
280279
// We can't add `react-dom` because the dependency is `react-dom/client`
281280
// for React 18 while it's `react-dom` for React 17. We'd need to detect
282281
// what React version the user has installed.
283-
include: ['react'],
282+
include: ['react', devRuntime],
284283
},
285284
resolve: {
286285
dedupe: ['react', 'react-dom'],

playground/ssr-react/vite.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import react from '@vitejs/plugin-react'
33

44
export default defineConfig({
55
plugins: [react()],
6-
optimizeDeps: { include: ['react/jsx-dev-runtime'] },
76
build: {
87
minify: false,
98
},

0 commit comments

Comments
 (0)