Skip to content

Commit da29600

Browse files
authored
feat(react-swc): set SWC cacheRoot option (#802)
1 parent 12b05bb commit da29600

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

packages/plugin-react-swc/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Set SWC cacheRoot options
6+
7+
This is set to `{viteCacheDir}/swc` and override the default of `.swc`.
8+
59
## 4.0.1 (2025-08-19)
610

711
### Set `optimizeDeps.rollupOptions.transform.jsx` instead of `optimizeDeps.rollupOptions.jsx` for rolldown-vite ([#735](https://github.com/vitejs/vite-plugin-react/pull/735))

packages/plugin-react-swc/src/index.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ type Options = {
8585

8686
const react = (_options?: Options): Plugin[] => {
8787
let hmrDisabled = false
88+
let viteCacheRoot: string | undefined
8889
const options = {
8990
jsxImportSource: _options?.jsxImportSource ?? 'react',
9091
tsDecorators: _options?.tsDecorators,
@@ -139,6 +140,7 @@ const react = (_options?: Options): Plugin[] => {
139140
},
140141
}),
141142
configResolved(config) {
143+
viteCacheRoot = config.cacheDir
142144
if (config.server.hmr === false) hmrDisabled = true
143145
const mdxIndex = config.plugins.findIndex(
144146
(p) => p.name === '@mdx-js/rollup',
@@ -184,6 +186,7 @@ const react = (_options?: Options): Plugin[] => {
184186
code,
185187
options.devTarget,
186188
options,
189+
viteCacheRoot,
187190
{
188191
refresh,
189192
development: true,
@@ -211,11 +214,21 @@ const react = (_options?: Options): Plugin[] => {
211214
config: (userConfig) => ({
212215
build: silenceUseClientWarning(userConfig),
213216
}),
217+
configResolved(config) {
218+
viteCacheRoot = config.cacheDir
219+
},
214220
transform: (code, _id) =>
215-
transformWithOptions(_id.split('?')[0], code, 'esnext', options, {
216-
runtime: 'automatic',
217-
importSource: options.jsxImportSource,
218-
}),
221+
transformWithOptions(
222+
_id.split('?')[0],
223+
code,
224+
'esnext',
225+
options,
226+
viteCacheRoot,
227+
{
228+
runtime: 'automatic',
229+
importSource: options.jsxImportSource,
230+
},
231+
),
219232
}
220233
: {
221234
name: 'vite:react-swc',
@@ -230,6 +243,9 @@ const react = (_options?: Options): Plugin[] => {
230243
},
231244
},
232245
}),
246+
configResolved(config) {
247+
viteCacheRoot = config.cacheDir
248+
},
233249
},
234250
]
235251
}
@@ -239,6 +255,7 @@ const transformWithOptions = async (
239255
code: string,
240256
target: JscTarget,
241257
options: Options,
258+
viteCacheRoot: string | undefined,
242259
reactConfig: ReactConfig,
243260
) => {
244261
const decorators = options?.tsDecorators ?? false
@@ -266,7 +283,10 @@ const transformWithOptions = async (
266283
jsc: {
267284
target,
268285
parser,
269-
experimental: { plugins: options.plugins },
286+
experimental: {
287+
plugins: options.plugins,
288+
cacheRoot: join(viteCacheRoot ?? 'node_modules/.vite', '.swc'),
289+
},
270290
transform: {
271291
useDefineForClassFields: true,
272292
react: reactConfig,

packages/plugin-react-swc/tsdown.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export default defineConfig({
66
entry: 'src/index.ts',
77
dts: true,
88
tsconfig: './tsconfig.src.json', // https://github.com/sxzz/rolldown-plugin-dts/issues/55
9+
ignoreWatch: ['playground', 'playground-temp', 'test-results'],
910
copy: [
1011
{
1112
from: 'node_modules/@vitejs/react-common/refresh-runtime.js',

0 commit comments

Comments
 (0)