Skip to content

Commit f1312ef

Browse files
authored
Alternate bundler: correctly inject react refresh loader (#77713)
Use the same inject react refresh loader logic with webpack, `test/integration/url-imports` should pass <!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change(s) that you're making: ## For Contributors ### Improving Documentation - Run `pnpm prettier-fix` to fix formatting issues before opening the PR. - Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide ### Adding or Updating Examples - The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md - Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md ### Fixing a bug - Related issues linked using `fixes #number` - Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ### Adding a feature - Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas) - Related issues/discussions are linked using `fixes #number` - e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) - Documentation added - Telemetry added. In case of a feature if it's used or not. - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ## For Maintainers - Minimal description (aim for explaining to someone not on the team to understand the PR) - When linking to a Slack thread, you might want to share details of the conclusion - Link both the Linear (Fixes NEXT-xxx) and the GitHub issues - Add review comments if necessary to explain to the reviewer the logic behind a change ### What? ### Why? ### How? Closes NEXT- Fixes # -->
1 parent 5982daf commit f1312ef

File tree

5 files changed

+123
-121
lines changed

5 files changed

+123
-121
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@
109109
"@next/third-parties": "workspace:*",
110110
"@opentelemetry/api": "1.4.1",
111111
"@picocss/pico": "1.5.10",
112-
"@rspack/core": "1.3.0",
113-
"@rspack/plugin-react-refresh": "1.0.1",
112+
"@rspack/core": "1.3.2",
113+
"@rspack/plugin-react-refresh": "1.1.0",
114114
"@svgr/webpack": "5.5.0",
115115
"@swc/cli": "0.1.55",
116116
"@swc/core": "1.9.3",

packages/next-rspack/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
},
88
"types": "index.d.ts",
99
"dependencies": {
10-
"@rspack/core": "1.3.0",
11-
"@rspack/plugin-react-refresh": "1.0.1",
10+
"@rspack/core": "1.3.2",
11+
"@rspack/plugin-react-refresh": "1.1.0",
1212
"react-refresh": "0.12.0"
1313
}
1414
}

packages/next/src/build/webpack-config.ts

+10-9
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,17 @@ let loggedIgnoredCompilerOptions = false
172172
const reactRefreshLoaderName =
173173
'next/dist/compiled/@next/react-refresh-utils/dist/loader'
174174

175+
function getReactRefreshLoader() {
176+
return process.env.NEXT_RSPACK
177+
? getRspackReactRefresh().loader
178+
: require.resolve(reactRefreshLoaderName)
179+
}
180+
175181
export function attachReactRefresh(
176182
webpackConfig: webpack.Configuration,
177183
targetLoader: webpack.RuleSetUseItem
178184
) {
179-
const reactRefreshLoader = require.resolve(reactRefreshLoaderName)
185+
const reactRefreshLoader = getReactRefreshLoader()
180186
webpackConfig.module?.rules?.forEach((rule) => {
181187
if (rule && typeof rule === 'object' && 'use' in rule) {
182188
const curr = rule.use
@@ -603,11 +609,7 @@ export default async function getBaseWebpackConfig(
603609
babelLoader,
604610
].filter(Boolean)
605611

606-
// Rspack will inject their own React Refresh loader in @rspack/plugin-react-refresh
607-
const reactRefreshLoaders =
608-
!isRspack && dev && isClient
609-
? [require.resolve(reactRefreshLoaderName)]
610-
: []
612+
const reactRefreshLoaders = dev && isClient ? [getReactRefreshLoader()] : []
611613

612614
// client components layers: SSR or browser
613615
const createClientLayerLoader = ({
@@ -1900,7 +1902,7 @@ export default async function getBaseWebpackConfig(
19001902
isClient &&
19011903
(isRspack
19021904
? // eslint-disable-next-line
1903-
new (getRspackReactRefresh() as any)()
1905+
new (getRspackReactRefresh() as any)({ injectLoader: false })
19041906
: new ReactRefreshWebpackPlugin(webpack)),
19051907
// Makes sure `Buffer` and `process` are polyfilled in client and flight bundles (same behavior as webpack 4)
19061908
(isClient || isEdgeServer) &&
@@ -2616,8 +2618,7 @@ export default async function getBaseWebpackConfig(
26162618
}
26172619

26182620
// Inject missing React Refresh loaders so that development mode is fast:
2619-
// Rspack will inject their own React Refresh loader in @rspack/plugin-react-refresh
2620-
if (!isRspack && dev && isClient) {
2621+
if (dev && isClient) {
26212622
attachReactRefresh(webpackConfig, defaultLoaders.babel)
26222623
}
26232624

packages/next/src/compiled/sass-loader/cjs.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)