Repository: vercel/next.js
Pull request: fix: rspack framework and lib cacheGroups

This pull request updates the Webpack configuration in `webpack-config.ts` to add specific cache group settings for Rspack builds. The changes aim to improve the chunking strategy by introducing new cache groups for `framework` and `lib` when Rspack is used.

### Changes to Webpack configuration:

* [`packages/next/src/build/webpack-config.ts`](diffhunk://#diff-5f441142eb8ba9bcba515aa36fbc420a113307bd191e54a4e9e6c43529fc2613L1159-R1177): Added `framework` and `lib` cache groups for Rspack builds. The `framework` group targets React-related modules with a high priority and enforces chunking, while the `lib` group targets other modules, ensuring reusable chunks and prioritizing them slightly lower.

<!-- 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 #

-->


Changed files:
filename: packages/next/src/build/utils.ts
patch: @@ -1931,3 +1931,7 @@ export function collectMeta({
 
   return meta
 }
+
+export const RSPACK_DEFAULT_LAYERS_REGEX = new RegExp(
+  `^(|${[WEBPACK_LAYERS.pagesDirBrowser, WEBPACK_LAYERS.pagesDirEdge, WEBPACK_LAYERS.pagesDirNode].join('|')})$`
+)

filename: packages/next/src/build/webpack-config.ts
patch: @@ -12,8 +12,9 @@ import type { WebpackLayerName } from '../lib/constants'
 import {
   isWebpackBundledLayer,
   isWebpackClientOnlyLayer,
-  isWebpackDefaultLayer,
   isWebpackServerOnlyLayer,
+  isWebpackDefaultLayer,
+  RSPACK_DEFAULT_LAYERS_REGEX,
 } from './utils'
 import type { CustomRoutes } from '../lib/load-custom-routes.js'
 import {
@@ -1157,9 +1158,25 @@ export default async function getBaseWebpackConfig(
             : (chunk: any) =>
                 !/^(polyfills|main|pages\/_app)$/.test(chunk.name),
 
-          // TODO: investigate these cache groups with rspack
           cacheGroups: isRspack
-            ? {}
+            ? {
+                framework: {
+                  chunks: 'all' as const,
+                  name: 'framework',
+                  layer: RSPACK_DEFAULT_LAYERS_REGEX,
+                  test: /[/]node_modules[/](react|react-dom|next[/]dist[/]compiled[/](react|react-dom)(-experimental)?)[/]/,
+                  priority: 40,
+                  enforce: true,
+                },
+                lib: {
+                  test: /[/]node_modules[/](?!.*\.(css|scss|sass|less|styl)$)/,
+                  name: 'lib',
+                  chunks: 'all',
+                  priority: 30,
+                  minChunks: 1,
+                  reuseExistingChunk: true,
+                },
+              }
             : {
                 framework: frameworkCacheGroup,
                 lib: libCacheGroup,

filename: test/integration/fallback-modules/test/index.test.js
patch: @@ -5,6 +5,7 @@ import { nextBuild } from 'next-test-utils'
 import { join } from 'path'
 
 const fixturesDir = join(__dirname, '..', 'fixtures')
+const isRspack = process.env.NEXT_RSPACK !== undefined
 
 describe('Fallback Modules', () => {
   ;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
@@ -46,7 +47,13 @@ describe('Fallback Modules', () => {
 
           // expect(parseFloat(indexSize)).toBeLessThanOrEqual(3.1)
           // expect(parseFloat(indexSize)).toBeGreaterThanOrEqual(2)
-          expect(indexSize.endsWith('kB')).toBe(true)
+
+          if (isRspack) {
+            expect(indexSize.endsWith('B')).toBe(true)
+            expect(indexSize.endsWith('MB')).toBe(false)
+          } else {
+            expect(indexSize.endsWith('kB')).toBe(true)
+          }
 
           // expect(parseFloat(indexFirstLoad)).toBeLessThanOrEqual(67.9)
           // expect(parseFloat(indexFirstLoad)).toBeGreaterThanOrEqual(60)