1
- import path from 'node:path'
2
1
import type { ParserOptions , TransformOptions , types as t } from '@babel/core'
3
2
import * as babel from '@babel/core'
4
- import { createFilter , normalizePath } from 'vite'
3
+ import { createFilter } from 'vite'
5
4
import type { Plugin , PluginOption , ResolvedConfig } from 'vite'
6
5
import MagicString from 'magic-string'
7
6
import type { SourceMap } from 'magic-string'
@@ -12,8 +11,6 @@ import {
12
11
runtimeCode ,
13
12
runtimePublicPath
14
13
} from './fast-refresh'
15
- import { babelImportToRequire } from './jsx-runtime/babel-import-to-require'
16
- import { restoreJSX } from './jsx-runtime/restore-jsx'
17
14
18
15
export interface Options {
19
16
include ?: string | RegExp | Array < string | RegExp >
@@ -40,11 +37,6 @@ export interface Options {
40
37
* @default true
41
38
*/
42
39
jsxPure ?: boolean
43
- /**
44
- * Toggles whether or not to throw an error if an XML namespaced tag name is used.
45
- * @default true
46
- */
47
- jsxThrowIfNamespace ?: boolean
48
40
/**
49
41
* Babel configuration applied in both dev and prod.
50
42
*/
@@ -100,7 +92,6 @@ const prependReactImportCode = "import React from 'react'; "
100
92
export default function viteReact ( opts : Options = { } ) : PluginOption [ ] {
101
93
// Provide default values for Rollup compat.
102
94
let devBase = '/'
103
- let resolvedCacheDir : string
104
95
let filter = createFilter ( opts . include , opts . exclude )
105
96
let needHiresSourcemap = false
106
97
let isProduction = true
@@ -127,21 +118,37 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
127
118
const viteBabel : Plugin = {
128
119
name : 'vite:react-babel' ,
129
120
enforce : 'pre' ,
130
- config ( ) {
121
+ config ( _ , { mode } ) {
122
+ // Copied from https://github.com/vitejs/vite/blob/4e9bdd4fb3654a9d43917e1cb682d3d2bad25115/packages/vite/src/node/config.ts#L488-L490
123
+ const isProduction =
124
+ ( process . env . NODE_ENV || process . env . VITE_USER_NODE_ENV || mode ) ===
125
+ 'production'
126
+
131
127
if ( opts . jsxRuntime === 'classic' ) {
132
128
return {
133
129
esbuild : {
134
130
logOverride : {
135
131
'this-is-undefined-in-esm' : 'silent'
136
- }
132
+ } ,
133
+ jsx : 'transform' ,
134
+ jsxImportSource : opts . jsxImportSource ,
135
+ jsxSideEffects : opts . jsxPure === false
136
+ }
137
+ }
138
+ } else {
139
+ return {
140
+ esbuild : {
141
+ jsxDev : ! isProduction ,
142
+ jsx : 'automatic' ,
143
+ jsxImportSource : opts . jsxImportSource ,
144
+ jsxSideEffects : opts . jsxPure === false
137
145
}
138
146
}
139
147
}
140
148
} ,
141
149
configResolved ( config ) {
142
150
devBase = config . base
143
151
projectRoot = config . root
144
- resolvedCacheDir = normalizePath ( path . resolve ( config . cacheDir ) )
145
152
filter = createFilter ( opts . include , opts . exclude , {
146
153
resolve : projectRoot
147
154
} )
@@ -231,39 +238,7 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
231
238
let ast : t . File | null | undefined
232
239
let prependReactImport = false
233
240
if ( ! isProjectFile || isJSX ) {
234
- if ( useAutomaticRuntime ) {
235
- // By reverse-compiling "React.createElement" calls into JSX,
236
- // React elements provided by dependencies will also use the
237
- // automatic runtime!
238
- // Avoid parsing the optimized react-dom since it will never
239
- // contain compiled JSX and it's a pretty big file (800kb).
240
- const isOptimizedReactDom =
241
- id . startsWith ( resolvedCacheDir ) && id . includes ( '/react-dom.js' )
242
- const [ restoredAst , isCommonJS ] =
243
- ! isProjectFile && ! isJSX && ! isOptimizedReactDom
244
- ? await restoreJSX ( babel , code , id )
245
- : [ null , false ]
246
-
247
- if ( isJSX || ( ast = restoredAst ) ) {
248
- plugins . push ( [
249
- await loadPlugin (
250
- '@babel/plugin-transform-react-jsx' +
251
- ( isProduction ? '' : '-development' )
252
- ) ,
253
- {
254
- runtime : 'automatic' ,
255
- importSource : opts . jsxImportSource ,
256
- pure : opts . jsxPure !== false ,
257
- throwIfNamespace : opts . jsxThrowIfNamespace
258
- }
259
- ] )
260
-
261
- // Avoid inserting `import` statements into CJS modules.
262
- if ( isCommonJS ) {
263
- plugins . push ( babelImportToRequire )
264
- }
265
- }
266
- } else if ( isProjectFile ) {
241
+ if ( ! useAutomaticRuntime && isProjectFile ) {
267
242
// These plugins are only needed for the classic runtime.
268
243
if ( ! isProduction ) {
269
244
plugins . push (
0 commit comments