-
-
Notifications
You must be signed in to change notification settings - Fork 193
fix(react-swc): fix @vitejs/plugin-react-swc/preamble on build
#962
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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` ```
There was a problem hiding this 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
vite-plugin-react/packages/plugin-react-swc/src/index.ts
Lines 119 to 136 in 99d0fc4
| 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 |
|
Yeah I would prefer to avoid an extra plugin which makes thinks harder to track |
|
Have updated the implementation |
@vitejs/plugin-react-swc/preamble on build
commit: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Description
Should ensure hmrDisabled is set to true during build, preventing React refresh runtime from being included in production bundles.
Otherwise: