From 1464533f942453e95438f4a968d953ee5a226878 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arnaud=20Barr=C3=A9?= Date: Sat, 30 Aug 2025 17:24:25 +0200 Subject: [PATCH] feat(react-swc): set SWC cacheRoot option --- packages/plugin-react-swc/CHANGELOG.md | 4 +++ packages/plugin-react-swc/src/index.ts | 30 ++++++++++++++++++---- packages/plugin-react-swc/tsdown.config.ts | 1 + 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/packages/plugin-react-swc/CHANGELOG.md b/packages/plugin-react-swc/CHANGELOG.md index 9ba987d21..14c4f2f0a 100644 --- a/packages/plugin-react-swc/CHANGELOG.md +++ b/packages/plugin-react-swc/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Set SWC cacheRoot options + +This is set to `{viteCacheDir}/swc` and override the default of `.swc`. + ## 4.0.1 (2025-08-19) ### Set `optimizeDeps.rollupOptions.transform.jsx` instead of `optimizeDeps.rollupOptions.jsx` for rolldown-vite ([#735](https://github.com/vitejs/vite-plugin-react/pull/735)) diff --git a/packages/plugin-react-swc/src/index.ts b/packages/plugin-react-swc/src/index.ts index 17fcb1f0c..45b3cad7d 100644 --- a/packages/plugin-react-swc/src/index.ts +++ b/packages/plugin-react-swc/src/index.ts @@ -85,6 +85,7 @@ type Options = { const react = (_options?: Options): Plugin[] => { let hmrDisabled = false + let viteCacheRoot: string | undefined const options = { jsxImportSource: _options?.jsxImportSource ?? 'react', tsDecorators: _options?.tsDecorators, @@ -139,6 +140,7 @@ const react = (_options?: Options): Plugin[] => { }, }), configResolved(config) { + viteCacheRoot = config.cacheDir if (config.server.hmr === false) hmrDisabled = true const mdxIndex = config.plugins.findIndex( (p) => p.name === '@mdx-js/rollup', @@ -184,6 +186,7 @@ const react = (_options?: Options): Plugin[] => { code, options.devTarget, options, + viteCacheRoot, { refresh, development: true, @@ -211,11 +214,21 @@ const react = (_options?: Options): Plugin[] => { config: (userConfig) => ({ build: silenceUseClientWarning(userConfig), }), + configResolved(config) { + viteCacheRoot = config.cacheDir + }, transform: (code, _id) => - transformWithOptions(_id.split('?')[0], code, 'esnext', options, { - runtime: 'automatic', - importSource: options.jsxImportSource, - }), + transformWithOptions( + _id.split('?')[0], + code, + 'esnext', + options, + viteCacheRoot, + { + runtime: 'automatic', + importSource: options.jsxImportSource, + }, + ), } : { name: 'vite:react-swc', @@ -230,6 +243,9 @@ const react = (_options?: Options): Plugin[] => { }, }, }), + configResolved(config) { + viteCacheRoot = config.cacheDir + }, }, ] } @@ -239,6 +255,7 @@ const transformWithOptions = async ( code: string, target: JscTarget, options: Options, + viteCacheRoot: string | undefined, reactConfig: ReactConfig, ) => { const decorators = options?.tsDecorators ?? false @@ -266,7 +283,10 @@ const transformWithOptions = async ( jsc: { target, parser, - experimental: { plugins: options.plugins }, + experimental: { + plugins: options.plugins, + cacheRoot: join(viteCacheRoot ?? 'node_modules/.vite', '.swc'), + }, transform: { useDefineForClassFields: true, react: reactConfig, diff --git a/packages/plugin-react-swc/tsdown.config.ts b/packages/plugin-react-swc/tsdown.config.ts index dea2c33a0..354d5f2e6 100644 --- a/packages/plugin-react-swc/tsdown.config.ts +++ b/packages/plugin-react-swc/tsdown.config.ts @@ -6,6 +6,7 @@ export default defineConfig({ entry: 'src/index.ts', dts: true, tsconfig: './tsconfig.src.json', // https://github.com/sxzz/rolldown-plugin-dts/issues/55 + ignoreWatch: ['playground', 'playground-temp', 'test-results'], copy: [ { from: 'node_modules/@vitejs/react-common/refresh-runtime.js',