From 8f9a3586fd48cbe64968231ee199f32524104938 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Fri, 15 Aug 2025 10:07:45 +0900 Subject: [PATCH] refactor(react): simplify rolldown-vite only plugins --- packages/plugin-react/src/index.ts | 70 ++++++++++++++++-------------- 1 file changed, 37 insertions(+), 33 deletions(-) diff --git a/packages/plugin-react/src/index.ts b/packages/plugin-react/src/index.ts index 6700f5982..b111c5be1 100644 --- a/packages/plugin-react/src/index.ts +++ b/packages/plugin-react/src/index.ts @@ -340,43 +340,43 @@ export default function viteReact(opts: Options = {}): Plugin[] { }, } + // for rolldown-vite const viteRefreshWrapper: Plugin = { name: 'vite:react:refresh-wrapper', apply: 'serve', - transform: isRolldownVite - ? { - filter: { - id: { - include: makeIdFiltersToMatchWithQuery(include), - exclude: makeIdFiltersToMatchWithQuery(exclude), - }, - }, - handler(code, id, options) { - const ssr = options?.ssr === true - - const [filepath] = id.split('?') - const isJSX = filepath.endsWith('x') - const useFastRefresh = - !skipFastRefresh && - !ssr && - (isJSX || - code.includes(jsxImportDevRuntime) || - code.includes(jsxImportRuntime)) - if (!useFastRefresh) return - - const { code: newCode } = addRefreshWrapper( - code, - avoidSourceMapOption, - '@vitejs/plugin-react', - id, - opts.reactRefreshHost, - ) - return { code: newCode, map: null } - }, - } - : undefined, + transform: { + filter: { + id: { + include: makeIdFiltersToMatchWithQuery(include), + exclude: makeIdFiltersToMatchWithQuery(exclude), + }, + }, + handler(code, id, options) { + const ssr = options?.ssr === true + + const [filepath] = id.split('?') + const isJSX = filepath.endsWith('x') + const useFastRefresh = + !skipFastRefresh && + !ssr && + (isJSX || + code.includes(jsxImportDevRuntime) || + code.includes(jsxImportRuntime)) + if (!useFastRefresh) return + + const { code: newCode } = addRefreshWrapper( + code, + avoidSourceMapOption, + '@vitejs/plugin-react', + id, + opts.reactRefreshHost, + ) + return { code: newCode, map: null } + }, + }, } + // for rolldown-vite const viteConfigPost: Plugin = { name: 'vite:react:config-post', enforce: 'post', @@ -449,7 +449,11 @@ export default function viteReact(opts: Options = {}): Plugin[] { }, } - return [viteBabel, viteRefreshWrapper, viteConfigPost, viteReactRefresh] + return [ + viteBabel, + ...(isRolldownVite ? [viteRefreshWrapper, viteConfigPost] : []), + viteReactRefresh, + ] } viteReact.preambleCode = preambleCode