@@ -361,40 +361,53 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
361361 }
362362 }
363363
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
365368 // Adapted from https://github.com/alloc/vite-react-jsx
366369 const viteReactJsx : Plugin = {
367370 name : 'vite:react-jsx' ,
368371 enforce : 'pre' ,
369372 config ( ) {
370373 return {
371374 optimizeDeps : {
372- include : [ 'react/jsx-dev-runtime' ]
375+ include : [ reactJsxRuntimeId , reactJsxDevRuntimeId ]
373376 }
374377 }
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
382378 } ,
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 ) {
389396 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`
395401 ] . join ( '\n' )
396402 }
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+ }
398411 }
399412
400413 return [ viteBabel , viteReactRefresh , useAutomaticRuntime && viteReactJsx ]
0 commit comments