Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/plugin-react-swc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
30 changes: 25 additions & 5 deletions packages/plugin-react-swc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -184,6 +186,7 @@ const react = (_options?: Options): Plugin[] => {
code,
options.devTarget,
options,
viteCacheRoot,
{
refresh,
development: true,
Expand Down Expand Up @@ -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',
Expand All @@ -230,6 +243,9 @@ const react = (_options?: Options): Plugin[] => {
},
},
}),
configResolved(config) {
viteCacheRoot = config.cacheDir
},
},
]
}
Expand All @@ -239,6 +255,7 @@ const transformWithOptions = async (
code: string,
target: JscTarget,
options: Options,
viteCacheRoot: string | undefined,
reactConfig: ReactConfig,
) => {
const decorators = options?.tsDecorators ?? false
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-react-swc/tsdown.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Loading