@@ -99,10 +99,9 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
9999 let projectRoot = process . cwd ( )
100100 let skipFastRefresh = opts . fastRefresh === false
101101 const skipReactImport = false
102- let runPluginOverrides = (
103- options : ReactBabelOptions ,
104- context : ReactBabelHookContext ,
105- ) => false
102+ let runPluginOverrides :
103+ | ( ( options : ReactBabelOptions , context : ReactBabelHookContext ) => void )
104+ | undefined
106105 let staticBabelOptions : ReactBabelOptions | undefined
107106
108107 const useAutomaticRuntime = opts . jsxRuntime !== 'classic'
@@ -158,19 +157,16 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
158157 )
159158 }
160159
161- runPluginOverrides = ( babelOptions , context ) => {
162- const hooks = config . plugins
163- . map ( ( plugin ) => plugin . api ?. reactBabel )
164- . filter ( Boolean ) as ReactBabelHook [ ]
160+ const hooks = config . plugins
161+ . map ( ( plugin ) => plugin . api ?. reactBabel )
162+ . filter ( defined )
165163
166- if ( hooks . length > 0 ) {
167- return ( runPluginOverrides = ( babelOptions , context ) => {
168- hooks . forEach ( ( hook ) => hook ( babelOptions , context , config ) )
169- return true
170- } ) ( babelOptions , context )
164+ if ( hooks . length > 0 ) {
165+ runPluginOverrides = ( babelOptions , context ) => {
166+ hooks . forEach ( ( hook ) => hook ( babelOptions , context , config ) )
171167 }
172- runPluginOverrides = ( ) => false
173- return false
168+ } else if ( typeof opts . babel !== 'function' ) {
169+ staticBabelOptions = createBabelOptions ( opts . babel )
174170 }
175171 } ,
176172 async transform ( code , id , options ) {
@@ -188,17 +184,16 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
188184 const isProjectFile =
189185 ! isNodeModules && ( id [ 0 ] === '\0' || id . startsWith ( projectRoot + '/' ) )
190186
191- let babelOptions = staticBabelOptions
192- if ( typeof opts . babel === 'function' ) {
193- const rawOptions = opts . babel ( id , { ssr } )
194- babelOptions = createBabelOptions ( rawOptions )
195- runPluginOverrides ( babelOptions , { ssr, id : id } )
196- } else if ( ! babelOptions ) {
197- babelOptions = createBabelOptions ( opts . babel )
198- if ( ! runPluginOverrides ( babelOptions , { ssr, id : id } ) ) {
199- staticBabelOptions = babelOptions
200- }
201- }
187+ const babelOptions = ( ( ) => {
188+ if ( staticBabelOptions ) return staticBabelOptions
189+ const newBabelOptions = createBabelOptions (
190+ typeof opts . babel === 'function'
191+ ? opts . babel ( id , { ssr } )
192+ : opts . babel ,
193+ )
194+ runPluginOverrides ?.( newBabelOptions , { id, ssr } )
195+ return newBabelOptions
196+ } ) ( )
202197
203198 const plugins = isProjectFile ? [ ...babelOptions . plugins ] : [ ]
204199
@@ -389,3 +384,7 @@ function createBabelOptions(rawOptions?: BabelOptions) {
389384
390385 return babelOptions
391386}
387+
388+ function defined < T > ( value : T | undefined ) : value is T {
389+ return value !== undefined
390+ }
0 commit comments