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
5 changes: 5 additions & 0 deletions .changeset/fix-plugin-react-hmr-refresh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@lynx-js/react-rsbuild-plugin": patch
---

Respect `dev.hmr: false` when installing React Refresh integrations so disabled HMR no longer injects the refresh loader or plugin.
8 changes: 4 additions & 4 deletions packages/rspeedy/plugin-react/src/refresh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import { LAYERS } from '@lynx-js/react-webpack-plugin'
const PLUGIN_NAME_REACT_REFRESH = 'lynx:react:refresh'

export function applyRefresh(api: RsbuildPluginAPI): void {
api.modifyWebpackChain?.(async (chain, { CHAIN_ID, isProd }) => {
if (!isProd) {
api.modifyWebpackChain?.(async (chain, { CHAIN_ID, environment, isProd }) => {
if (!isProd && environment.config.dev?.hmr !== false) {
await applyRefreshRules(api, chain, CHAIN_ID, ReactRefreshWebpackPlugin)
}
})
api.modifyBundlerChain(async (chain, { isProd, CHAIN_ID }) => {
if (!isProd) {
api.modifyBundlerChain(async (chain, { isProd, CHAIN_ID, environment }) => {
if (!isProd && environment.config.dev?.hmr !== false) {
// biome-ignore lint/correctness/useHookAtTopLevel: not react hooks
const { resolve } = api.useExposed<
{ resolve: (request: string) => Promise<string> }
Expand Down
41 changes: 41 additions & 0 deletions packages/rspeedy/plugin-react/test/refresh.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,45 @@ describe('pluginReactLynx with react-refresh', () => {
).toBe(true)
expect(rspackConfig).toHaveLoader(ReactRefreshRspackPlugin.loader)
})

test('does not inject refresh loader and plugin when hmr is disabled', async () => {
const { pluginReactLynx } = await import('../src/index.js')
const { ReactRefreshRspackPlugin } = await import(
'@lynx-js/react-refresh-webpack-plugin'
)

const rsbuild = await createRspeedy({
rspeedyConfig: {
dev: {
hmr: false,
},
tools: {
rspack: {
output: {
chunkFormat: 'commonjs',
},
resolve: {
extensionAlias: {
'.js': ['.ts', '.js'],
},
},
},
},
plugins: [
pluginReactLynx(),
pluginStubRspeedyAPI(),
],
},
})

const [rspackConfig] = await rsbuild.initConfigs()

expect(rspackConfig?.mode).toBe('development')
expect(
rspackConfig?.plugins?.some(plugin =>
plugin instanceof ReactRefreshRspackPlugin
),
).toBe(false)
expect(rspackConfig).not.toHaveLoader(ReactRefreshRspackPlugin.loader)
})
})
Loading