@@ -8,8 +8,6 @@ import type {
8
8
ResolvedConfig ,
9
9
UserConfig ,
10
10
} from 'vite'
11
- import MagicString from 'magic-string'
12
- import type { SourceMap } from 'magic-string'
13
11
import {
14
12
addRefreshWrapper ,
15
13
preambleCode ,
@@ -20,18 +18,17 @@ import {
20
18
export interface Options {
21
19
include ?: string | RegExp | Array < string | RegExp >
22
20
exclude ?: string | RegExp | Array < string | RegExp >
23
- /**
24
- * @deprecated All tools now support the automatic runtime, and it has been backported
25
- * up to React 16. This allows to skip the React import and can produce smaller bundlers.
26
- * @default "automatic"
27
- */
28
- jsxRuntime ?: 'classic' | 'automatic'
29
21
/**
30
22
* Control where the JSX factory is imported from.
31
23
* https://esbuild.github.io/api/#jsx-import-source
32
24
* @default 'react'
33
25
*/
34
26
jsxImportSource ?: string
27
+ /**
28
+ * Note: Skipping React import with classic runtime is not supported from v4
29
+ * @default "automatic"
30
+ */
31
+ jsxRuntime ?: 'classic' | 'automatic'
35
32
/**
36
33
* Babel configuration applied in both dev and prod.
37
34
*/
@@ -82,7 +79,6 @@ declare module 'vite' {
82
79
}
83
80
}
84
81
85
- const prependReactImportCode = "import React from 'react'; "
86
82
const refreshContentRE = / \$ R e f r e s h (?: R e g | S i g ) \$ \( /
87
83
const defaultIncludeRE = / \. [ t j ] s x ? $ /
88
84
const tsRE = / \. t s x ? $ /
@@ -92,7 +88,6 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
92
88
let devBase = '/'
93
89
const filter = createFilter ( opts . include ?? defaultIncludeRE , opts . exclude )
94
90
const devRuntime = `${ opts . jsxImportSource ?? 'react' } /jsx-dev-runtime`
95
- let needHiresSourcemap = false
96
91
let isProduction = true
97
92
let projectRoot = process . cwd ( )
98
93
let skipFastRefresh = false
@@ -105,7 +100,7 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
105
100
// - import * as React from 'react';
106
101
// - import React from 'react';
107
102
// - import React, {useEffect} from 'react';
108
- const importReactRE = / (?: ^ | \n ) i m p o r t \s + (?: \* \s + a s \s + ) ? R e a c t (?: , | \s + ) /
103
+ const importReactRE = / (?: ^ | \s ) i m p o r t \s + (?: \* \s + a s \s + ) ? R e a c t (?: , | \s + ) /
109
104
110
105
const viteBabel : Plugin = {
111
106
name : 'vite:react-babel' ,
@@ -129,16 +124,9 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
129
124
configResolved ( config ) {
130
125
devBase = config . base
131
126
projectRoot = config . root
132
- needHiresSourcemap =
133
- config . command === 'build' && ! ! config . build . sourcemap
134
127
isProduction = config . isProduction
135
128
skipFastRefresh = isProduction || config . command === 'build'
136
129
137
- if ( opts . jsxRuntime === 'classic' ) {
138
- config . logger . warnOnce (
139
- '[@vitejs/plugin-react] Support for classic runtime is deprecated.' ,
140
- )
141
- }
142
130
if ( 'jsxPure' in opts ) {
143
131
config . logger . warnOnce (
144
132
'[@vitejs/plugin-react] jsxPure was removed. You can configure esbuild.jsxSideEffects directly.' ,
@@ -191,7 +179,6 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
191
179
] )
192
180
}
193
181
194
- let prependReactImport = false
195
182
if ( opts . jsxRuntime === 'classic' && isJSX ) {
196
183
if ( ! isProduction ) {
197
184
// These development plugins are only needed for the classic runtime.
@@ -200,24 +187,6 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
200
187
await loadPlugin ( '@babel/plugin-transform-react-jsx-source' ) ,
201
188
)
202
189
}
203
-
204
- // Even if the automatic JSX runtime is not used, we can still
205
- // inject the React import for .jsx and .tsx modules.
206
- if ( ! importReactRE . test ( code ) ) {
207
- prependReactImport = true
208
- }
209
- }
210
-
211
- let inputMap : SourceMap | undefined
212
- if ( prependReactImport ) {
213
- if ( needHiresSourcemap ) {
214
- const s = new MagicString ( code )
215
- s . prepend ( prependReactImportCode )
216
- code = s . toString ( )
217
- inputMap = s . generateMap ( { hires : true , source : id } )
218
- } else {
219
- code = prependReactImportCode + code
220
- }
221
190
}
222
191
223
192
// Avoid parsing if no special transformation is needed
@@ -226,7 +195,7 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
226
195
! babelOptions . configFile &&
227
196
! babelOptions . babelrc
228
197
) {
229
- return { code , map : inputMap ?? null }
198
+ return
230
199
}
231
200
232
201
const parserPlugins = [ ...babelOptions . parserOpts . plugins ]
@@ -256,8 +225,6 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
256
225
} ,
257
226
plugins,
258
227
sourceMaps : true ,
259
- // Vite handles sourcemap flattening
260
- inputSourceMap : inputMap ?? ( false as any ) ,
261
228
} )
262
229
263
230
if ( result ) {
0 commit comments