Repository: vercel/next.js
Pull request: feat: Rspack support config.experimental.useLightningcss to switch css-minimizer-plugin

<!-- 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 #

-->

Rspack support config.experimental.useLightningcss to switch css-minimizer-plugin.

Rspack using LightningCssMinimizerRspackPlugin by default.

If users encounter some CSS issues, they can switch back to the css-minimizer-plugin through `config.experimental.useLightningcss: false`.

In test mode, Rspack uses css-minimizer-plugin for compatibility.


Changed files:
filename: packages/next/src/build/webpack-config.ts
patch: @@ -1196,9 +1196,10 @@ export default async function getBaseWebpackConfig(
         (isClient ||
           isEdgeServer ||
           (isNodeServer && config.experimental.serverMinification)),
-      minimizer: isRspack
-        ? [
-            new (getRspackCore().SwcJsMinimizerRspackPlugin)({
+      minimizer: [
+        // Minify JavaScript
+        isRspack
+          ? new (getRspackCore().SwcJsMinimizerRspackPlugin)({
               // JS minimizer configuration
               // options should align with crates/napi/src/minify.rs#patch_opts
               minimizerOptions: {
@@ -1213,17 +1214,8 @@ export default async function getBaseWebpackConfig(
                   disableCharFreq: !isClient,
                 },
               },
-            }),
-            new (getRspackCore().LightningCssMinimizerRspackPlugin)({
-              // CSS minimizer configuration
-              minimizerOptions: {
-                targets: supportedBrowsers,
-              },
-            }),
-          ]
-        : [
-            // Minify JavaScript
-            (compiler: webpack.Compiler) => {
+            })
+          : (compiler: webpack.Compiler) => {
               // @ts-ignore No typings yet
               const { MinifyPlugin } =
                 require('./webpack/plugins/minify-webpack-plugin/src/index.js') as typeof import('./webpack/plugins/minify-webpack-plugin/src')
@@ -1232,8 +1224,21 @@ export default async function getBaseWebpackConfig(
                 disableCharFreq: !isClient,
               }).apply(compiler)
             },
-            // Minify CSS
-            (compiler: webpack.Compiler) => {
+        // Minify CSS
+        // By default, Rspack uses LightningCSS for CSS minification.
+        // Rspack uses css-minimizer-plugin by default for compatibility.
+        isRspack &&
+        (process.env.__NEXT_TEST_MODE
+          ? config.experimental.useLightningcss
+          : config.experimental?.useLightningcss === undefined ||
+            config.experimental.useLightningcss)
+          ? new (getRspackCore().LightningCssMinimizerRspackPlugin)({
+              // CSS minimizer configuration
+              minimizerOptions: {
+                targets: supportedBrowsers,
+              },
+            })
+          : (compiler: webpack.Compiler) => {
               const {
                 CssMinimizerPlugin,
               } = require('./webpack/plugins/css-minimizer-plugin')
@@ -1250,7 +1255,7 @@ export default async function getBaseWebpackConfig(
                 },
               }).apply(compiler)
             },
-          ],
+      ],
     },
     context: dir,
     // Kept as function to be backwards compatible

filename: test/integration/css-minify/test/index.test.js
patch: @@ -20,7 +20,7 @@ function runTests() {
     const $ = cheerio.load(html)
     const href = $('link[rel="preload"]').attr('href')
     const css = await renderViaHTTP(appPort, href)
-    if (process.env.IS_TURBOPACK_TEST || process.env.NEXT_RSPACK) {
+    if (process.env.IS_TURBOPACK_TEST) {
       expect(css).toContain(
         '.a{--var-1:-50%;--var-2:-50%}.b{--var-1:0;--var-2:-50%}'
       )