@@ -169,34 +169,69 @@ const react = (_options?: Options): Plugin[] => {
169169 ]
170170 }
171171 } ,
172- async transform ( code , _id , transformOptions ) {
173- const id = _id . split ( '?' ) [ 0 ]
174- const refresh = ! transformOptions ?. ssr && ! hmrDisabled
172+ transform : options . parserConfig
173+ ? // When custom parserConfig is provided, we can't add a filter
174+ // because the user controls which files are handled
175+ async ( code , _id , transformOptions ) => {
176+ const id = _id . split ( '?' ) [ 0 ]
177+ const refresh = ! transformOptions ?. ssr && ! hmrDisabled
175178
176- const result = await transformWithOptions (
177- id ,
178- code ,
179- options . devTarget ,
180- options ,
181- viteCacheRoot ,
182- {
183- refresh,
184- development : true ,
185- runtime : 'automatic' ,
186- importSource : options . jsxImportSource ,
187- } ,
188- )
189- if ( ! result ) return
190- if ( ! refresh ) return result
179+ const result = await transformWithOptions (
180+ id ,
181+ code ,
182+ options . devTarget ,
183+ options ,
184+ viteCacheRoot ,
185+ {
186+ refresh,
187+ development : true ,
188+ runtime : 'automatic' ,
189+ importSource : options . jsxImportSource ,
190+ } ,
191+ )
192+ if ( ! result ) return
193+ if ( ! refresh ) return result
191194
192- const newCode = addRefreshWrapper (
193- result . code ,
194- '@vitejs/plugin-react-swc' ,
195- id ,
196- options . reactRefreshHost ,
197- )
198- return { code : newCode ?? result . code , map : result . map }
199- } ,
195+ const newCode = addRefreshWrapper (
196+ result . code ,
197+ '@vitejs/plugin-react-swc' ,
198+ id ,
199+ options . reactRefreshHost ,
200+ )
201+ return { code : newCode ?? result . code , map : result . map }
202+ }
203+ : {
204+ // Add filter for default extensions: .tsx, .ts, .mts, .jsx, .mdx
205+ filter : { id : / \. ( t s x ? | m t s | j s x | m d x ) (?: $ | \? ) / } ,
206+ async handler ( code , _id , transformOptions ) {
207+ const id = _id . split ( '?' ) [ 0 ]
208+ const refresh = ! transformOptions ?. ssr && ! hmrDisabled
209+
210+ const result = await transformWithOptions (
211+ id ,
212+ code ,
213+ options . devTarget ,
214+ options ,
215+ viteCacheRoot ,
216+ {
217+ refresh,
218+ development : true ,
219+ runtime : 'automatic' ,
220+ importSource : options . jsxImportSource ,
221+ } ,
222+ )
223+ if ( ! result ) return
224+ if ( ! refresh ) return result
225+
226+ const newCode = addRefreshWrapper (
227+ result . code ,
228+ '@vitejs/plugin-react-swc' ,
229+ id ,
230+ options . reactRefreshHost ,
231+ )
232+ return { code : newCode ?? result . code , map : result . map }
233+ } ,
234+ } ,
200235 } ,
201236 options . plugins || options . useAtYourOwnRisk_mutateSwcOptions
202237 ? {
@@ -209,18 +244,37 @@ const react = (_options?: Options): Plugin[] => {
209244 configResolved ( config ) {
210245 viteCacheRoot = config . cacheDir
211246 } ,
212- transform : ( code , _id ) =>
213- transformWithOptions (
214- _id . split ( '?' ) [ 0 ] ,
215- code ,
216- 'esnext' ,
217- options ,
218- viteCacheRoot ,
219- {
220- runtime : 'automatic' ,
221- importSource : options . jsxImportSource ,
247+ transform : options . parserConfig
248+ ? // When custom parserConfig is provided, we can't add a filter
249+ // because the user controls which files are handled
250+ ( code , _id ) =>
251+ transformWithOptions (
252+ _id . split ( '?' ) [ 0 ] ,
253+ code ,
254+ 'esnext' ,
255+ options ,
256+ viteCacheRoot ,
257+ {
258+ runtime : 'automatic' ,
259+ importSource : options . jsxImportSource ,
260+ } ,
261+ )
262+ : {
263+ // Add filter for default extensions: .tsx, .ts, .mts, .jsx, .mdx
264+ filter : { id : / \. ( t s x ? | m t s | j s x | m d x ) (?: $ | \? ) / } ,
265+ handler : ( code , _id ) =>
266+ transformWithOptions (
267+ _id . split ( '?' ) [ 0 ] ,
268+ code ,
269+ 'esnext' ,
270+ options ,
271+ viteCacheRoot ,
272+ {
273+ runtime : 'automatic' ,
274+ importSource : options . jsxImportSource ,
275+ } ,
276+ ) ,
222277 } ,
223- ) ,
224278 }
225279 : {
226280 name : 'vite:react-swc' ,
0 commit comments