From 43d2dc9ea0a74b75471d52f2894cf0d0c9a88898 Mon Sep 17 00:00:00 2001 From: Roman Hotsiy Date: Fri, 6 Jun 2025 21:29:52 +0800 Subject: [PATCH 1/2] feat: swcCacheDir option --- packages/plugin-react-swc/src/index.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/plugin-react-swc/src/index.ts b/packages/plugin-react-swc/src/index.ts index 9f9f6738..4b523554 100644 --- a/packages/plugin-react-swc/src/index.ts +++ b/packages/plugin-react-swc/src/index.ts @@ -47,6 +47,11 @@ type Options = { * @default undefined */ plugins?: [string, Record][] + /** + * Specify the location where SWC stores its intermediate cache files. + * @default '.swc' + */ + swcCacheDir?: string /** * Set the target for SWC in dev. This can avoid to down-transpile private class method for example. * For production target, see https://vite.dev/config/build-options.html#build-target @@ -88,6 +93,7 @@ const react = (_options?: Options): PluginOption[] => { const options = { jsxImportSource: _options?.jsxImportSource ?? 'react', tsDecorators: _options?.tsDecorators, + swcCacheDir: _options?.swcCacheDir, plugins: _options?.plugins ? _options?.plugins.map((el): typeof el => [resolve(el[0]), el[1]]) : undefined, @@ -264,7 +270,7 @@ const transformWithOptions = async ( jsc: { target, parser, - experimental: { plugins: options.plugins }, + experimental: { plugins: options.plugins, cacheRoot: options.swcCacheDir }, transform: { useDefineForClassFields: true, react: reactConfig, From 0103b5105bd21e38cc716a90938fb39c862b4843 Mon Sep 17 00:00:00 2001 From: Roman Hotsiy Date: Sun, 8 Jun 2025 16:00:47 +0800 Subject: [PATCH 2/2] chore: adjust default value and update readme --- packages/plugin-react-swc/README.md | 8 ++++++++ packages/plugin-react-swc/src/index.ts | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/plugin-react-swc/README.md b/packages/plugin-react-swc/README.md index c74f11d7..17c58908 100644 --- a/packages/plugin-react-swc/README.md +++ b/packages/plugin-react-swc/README.md @@ -125,6 +125,14 @@ If set, disables the recommendation to use `@vitejs/plugin-react-oxc` (which is react({ disableOxcRecommendation: true }) ``` +### swcCacheDir + +Specify the location where SWC stores its intermediate cache files. + +```js +react({ swcCacheDir: 'node_modules/.vite/swc' }) +``` + ## Consistent components exports For React refresh to work correctly, your file should only export React components. The best explanation I've read is the one from the [Gatsby docs](https://www.gatsbyjs.com/docs/reference/local-development/fast-refresh/#how-it-works). diff --git a/packages/plugin-react-swc/src/index.ts b/packages/plugin-react-swc/src/index.ts index 4b523554..fe8ea74d 100644 --- a/packages/plugin-react-swc/src/index.ts +++ b/packages/plugin-react-swc/src/index.ts @@ -49,7 +49,7 @@ type Options = { plugins?: [string, Record][] /** * Specify the location where SWC stores its intermediate cache files. - * @default '.swc' + * @default 'node_modules/.vite/swc' */ swcCacheDir?: string /** @@ -93,7 +93,7 @@ const react = (_options?: Options): PluginOption[] => { const options = { jsxImportSource: _options?.jsxImportSource ?? 'react', tsDecorators: _options?.tsDecorators, - swcCacheDir: _options?.swcCacheDir, + swcCacheDir: _options?.swcCacheDir ?? 'node_modules/.vite/swc', plugins: _options?.plugins ? _options?.plugins.map((el): typeof el => [resolve(el[0]), el[1]]) : undefined,