Skip to content

Conversation

@HcySunYang
Copy link
Contributor

@HcySunYang HcySunYang commented Nov 4, 2025

Description

Should ensure hmrDisabled is set to true during build, preventing React refresh runtime from being included in production bundles.

Otherwise:

error during build:
[vite]: Rollup failed to resolve import "/@react-refresh" from "@vitejs/plugin-react-swc/preamble".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
`build.rollupOptions.external`

Add dedicated plugin to ensure hmrDisabled is set to true for all build configurations, preventing React refresh runtime from being included in production bundles.

Otherwise:

```✗ Build failed in 20ms
error during build:
[vite]: Rollup failed to resolve import "/@react-refresh" from "@vitejs/plugin-react-swc/preamble".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
`build.rollupOptions.external`
```
Copy link
Contributor

@hi-ogawa hi-ogawa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a bug fix? Probably I missed that hmrDisabled = true happens with apply: 'serve' currently

name: 'vite:react-swc',
apply: 'serve',
config: () => ({
esbuild: false,
// NOTE: oxc option only exists in rolldown-vite
oxc: false,
optimizeDeps: {
include: [`${options.jsxImportSource}/jsx-dev-runtime`],
...('rolldownVersion' in vite
? {
rollupOptions: { transform: { jsx: { runtime: 'automatic' } } },
}
: { esbuildOptions: { jsx: 'automatic' } }),
},
}),
configResolved(config) {
viteCacheRoot = config.cacheDir
if (config.server.hmr === false) hmrDisabled = true

I think it should be safe to flip the logic by starting from let hmrDisabled = true and then check config.server.hmr !== false, which becomes closer to @vitejs/plugin-react

let skipFastRefresh = true

@ArnaudBarre
Copy link
Member

Yeah I would prefer to avoid an extra plugin which makes thinks harder to track

@HcySunYang
Copy link
Contributor Author

Have updated the implementation

@hi-ogawa hi-ogawa changed the title feat: force disable HMR in vite-plugin-react-swc during builds fix(react-swc): fix @vitejs/plugin-react-swc/preamble on build Nov 5, 2025
@hi-ogawa hi-ogawa added the trigger: preview Trigger pkg.pr.new label Nov 5, 2025
@pkg-pr-new
Copy link

pkg-pr-new bot commented Nov 5, 2025

Open in StackBlitz

npm i https://pkg.pr.new/@vitejs/plugin-react@962
npm i https://pkg.pr.new/@vitejs/plugin-react-oxc@962
npm i https://pkg.pr.new/@vitejs/plugin-rsc@962
npm i https://pkg.pr.new/@vitejs/plugin-react-swc@962

commit: 8c65950

Copy link
Contributor

@hi-ogawa hi-ogawa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@hi-ogawa hi-ogawa merged commit 897a3be into vitejs:main Nov 5, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

trigger: preview Trigger pkg.pr.new

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants