Skip to content

Commit ebcc684

Browse files
Copilothi-ogawa
andcommitted
Use plugin hook filter internally
- plugin-react: Remove redundant filter check since hook filter handles it - plugin-react-swc: Add hook filters for default file extensions (.tsx, .ts, .mts, .jsx, .mdx) - Conditional filter application: Only use filters when parserConfig is not custom Co-authored-by: hi-ogawa <[email protected]>
1 parent c1bbac9 commit ebcc684

File tree

2 files changed

+91
-40
lines changed

2 files changed

+91
-40
lines changed

packages/plugin-react-swc/src/index.ts

Lines changed: 91 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -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: /\.(tsx?|mts|jsx|mdx)(?:$|\?)/ },
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: /\.(tsx?|mts|jsx|mdx)(?:$|\?)/ },
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',

packages/plugin-react/src/index.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { fileURLToPath } from 'node:url'
33
import { readFileSync } from 'node:fs'
44
import type * as babelCore from '@babel/core'
55
import type { ParserOptions, TransformOptions } from '@babel/core'
6-
import { createFilter } from 'vite'
76
import * as vite from 'vite'
87
import type { Plugin, ResolvedConfig } from 'vite'
98
import {
@@ -107,7 +106,6 @@ const compilerAnnotationRE = /['"]use memo['"]/
107106
export default function viteReact(opts: Options = {}): Plugin[] {
108107
const include = opts.include ?? defaultIncludeRE
109108
const exclude = opts.exclude ?? defaultExcludeRE
110-
const filter = createFilter(include, exclude)
111109

112110
const jsxImportSource = opts.jsxImportSource ?? 'react'
113111
const jsxImportRuntime = `${jsxImportSource}/jsx-runtime`
@@ -247,7 +245,6 @@ export default function viteReact(opts: Options = {}): Plugin[] {
247245
},
248246
async handler(code, id, options) {
249247
const [filepath] = id.split('?')
250-
if (!filter(filepath)) return
251248

252249
const ssr = options?.ssr === true
253250
const babelOptions = (() => {

0 commit comments

Comments
 (0)