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 9f9f6738..fe8ea74d 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 'node_modules/.vite/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 ?? 'node_modules/.vite/swc', 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,