@@ -361,40 +361,53 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
361
361
}
362
362
}
363
363
364
- // const runtimeId = 'react/jsx-runtime'
364
+ const reactJsxRuntimeId = 'react/jsx-runtime'
365
+ const reactJsxDevRuntimeId = 'react/jsx-dev-runtime'
366
+ const virtualReactJsxRuntimeId = '\0' + reactJsxRuntimeId
367
+ const virtualReactJsxDevRuntimeId = '\0' + reactJsxDevRuntimeId
365
368
// Adapted from https://github.com/alloc/vite-react-jsx
366
369
const viteReactJsx : Plugin = {
367
370
name : 'vite:react-jsx' ,
368
371
enforce : 'pre' ,
369
372
config ( ) {
370
373
return {
371
374
optimizeDeps : {
372
- include : [ 'react/jsx-dev-runtime' ]
375
+ include : [ reactJsxRuntimeId , reactJsxDevRuntimeId ]
373
376
}
374
377
}
375
- }
376
- // TODO: this optimization may not be necesary and it is breacking esbuild+rollup compat,
377
- // see https://github.com/vitejs/vite/pull/7246#discussion_r861552185
378
- // We could still do the same trick and resolve to the optimized dependency here
379
- /*
380
- resolveId(id: string) {
381
- return id === runtimeId ? id : null
382
378
} ,
383
- load(id: string) {
384
- if (id === runtimeId) {
385
- const runtimePath = resolve.sync(runtimeId, {
386
- basedir: projectRoot
387
- })
388
- const exports = ['jsx', 'jsxs', 'Fragment']
379
+ resolveId ( id , importer ) {
380
+ // Resolve runtime to a virtual path to be interoped.
381
+ // Since the interop code re-imports `id`, we need to prevent re-resolving
382
+ // to the virtual id if the importer is already the virtual id.
383
+ if ( id === reactJsxRuntimeId && importer !== virtualReactJsxRuntimeId ) {
384
+ return virtualReactJsxRuntimeId
385
+ }
386
+ if (
387
+ id === reactJsxDevRuntimeId &&
388
+ importer !== virtualReactJsxDevRuntimeId
389
+ ) {
390
+ return virtualReactJsxDevRuntimeId
391
+ }
392
+ } ,
393
+ load ( id ) {
394
+ // Apply manual interop
395
+ if ( id === virtualReactJsxRuntimeId ) {
389
396
return [
390
- `import * as jsxRuntime from ${JSON.stringify(runtimePath)}`,
391
- // We can't use `export * from` or else any callsite that uses
392
- // this module will be compiled to `jsxRuntime.exports.jsx`
393
- // instead of the more concise `jsx` alias.
394
- ...exports.map((name) => `export const ${name} = jsxRuntime.${name}`)
397
+ `import * as jsxRuntime from ${ JSON . stringify ( reactJsxRuntimeId ) } ` ,
398
+ `export const Fragment = jsxRuntime.Fragment` ,
399
+ `export const jsx = jsxRuntime.jsx` ,
400
+ `export const jsxs = jsxRuntime.jsxs`
395
401
] . join ( '\n' )
396
402
}
397
- } */
403
+ if ( id === virtualReactJsxDevRuntimeId ) {
404
+ return [
405
+ `import * as jsxRuntime from ${ JSON . stringify ( reactJsxDevRuntimeId ) } ` ,
406
+ `export const Fragment = jsxRuntime.Fragment` ,
407
+ `export const jsxDEV = jsxRuntime.jsxDEV`
408
+ ] . join ( '\n' )
409
+ }
410
+ }
398
411
}
399
412
400
413
return [ viteBabel , viteReactRefresh , useAutomaticRuntime && viteReactJsx ]
0 commit comments