Skip to content

Commit d33f37d

Browse files
authored
refactor(react): simplify rolldown-vite only plugins (#720)
1 parent 71bb49c commit d33f37d

File tree

1 file changed

+37
-33
lines changed

1 file changed

+37
-33
lines changed

packages/plugin-react/src/index.ts

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -340,43 +340,43 @@ export default function viteReact(opts: Options = {}): Plugin[] {
340340
},
341341
}
342342

343+
// for rolldown-vite
343344
const viteRefreshWrapper: Plugin = {
344345
name: 'vite:react:refresh-wrapper',
345346
apply: 'serve',
346-
transform: isRolldownVite
347-
? {
348-
filter: {
349-
id: {
350-
include: makeIdFiltersToMatchWithQuery(include),
351-
exclude: makeIdFiltersToMatchWithQuery(exclude),
352-
},
353-
},
354-
handler(code, id, options) {
355-
const ssr = options?.ssr === true
356-
357-
const [filepath] = id.split('?')
358-
const isJSX = filepath.endsWith('x')
359-
const useFastRefresh =
360-
!skipFastRefresh &&
361-
!ssr &&
362-
(isJSX ||
363-
code.includes(jsxImportDevRuntime) ||
364-
code.includes(jsxImportRuntime))
365-
if (!useFastRefresh) return
366-
367-
const { code: newCode } = addRefreshWrapper(
368-
code,
369-
avoidSourceMapOption,
370-
'@vitejs/plugin-react',
371-
id,
372-
opts.reactRefreshHost,
373-
)
374-
return { code: newCode, map: null }
375-
},
376-
}
377-
: undefined,
347+
transform: {
348+
filter: {
349+
id: {
350+
include: makeIdFiltersToMatchWithQuery(include),
351+
exclude: makeIdFiltersToMatchWithQuery(exclude),
352+
},
353+
},
354+
handler(code, id, options) {
355+
const ssr = options?.ssr === true
356+
357+
const [filepath] = id.split('?')
358+
const isJSX = filepath.endsWith('x')
359+
const useFastRefresh =
360+
!skipFastRefresh &&
361+
!ssr &&
362+
(isJSX ||
363+
code.includes(jsxImportDevRuntime) ||
364+
code.includes(jsxImportRuntime))
365+
if (!useFastRefresh) return
366+
367+
const { code: newCode } = addRefreshWrapper(
368+
code,
369+
avoidSourceMapOption,
370+
'@vitejs/plugin-react',
371+
id,
372+
opts.reactRefreshHost,
373+
)
374+
return { code: newCode, map: null }
375+
},
376+
},
378377
}
379378

379+
// for rolldown-vite
380380
const viteConfigPost: Plugin = {
381381
name: 'vite:react:config-post',
382382
enforce: 'post',
@@ -449,7 +449,11 @@ export default function viteReact(opts: Options = {}): Plugin[] {
449449
},
450450
}
451451

452-
return [viteBabel, viteRefreshWrapper, viteConfigPost, viteReactRefresh]
452+
return [
453+
viteBabel,
454+
...(isRolldownVite ? [viteRefreshWrapper, viteConfigPost] : []),
455+
viteReactRefresh,
456+
]
453457
}
454458

455459
viteReact.preambleCode = preambleCode

0 commit comments

Comments
 (0)