Skip to content

Commit b9b4cfb

Browse files
authored
fix: let lightningcss minimizer inherit loader options (#4392)
1 parent ba996d3 commit b9b4cfb

File tree

8 files changed

+206
-58
lines changed

8 files changed

+206
-58
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { readFile } from 'node:fs/promises';
2+
import { join } from 'node:path';
3+
import { build, dev, rspackOnlyTest } from '@e2e/helper';
4+
import { expect } from '@playwright/test';
5+
6+
rspackOnlyTest(
7+
'should let lightningcss minimizer inherit from tools.lightningcssLoader',
8+
async ({ page }) => {
9+
const cssIndex = join(__dirname, 'dist/static/css/index.css');
10+
11+
await dev({
12+
cwd: __dirname,
13+
page,
14+
});
15+
const devContent = await readFile(cssIndex, 'utf-8');
16+
expect(devContent).toContain('margin-inline-end: 100px;');
17+
18+
await build({
19+
cwd: __dirname,
20+
});
21+
const buildContent = await readFile(cssIndex, 'utf-8');
22+
expect(buildContent).toContain('margin-inline-end:100px');
23+
},
24+
);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { defineConfig } from '@rsbuild/core';
2+
3+
export default defineConfig({
4+
dev: {
5+
writeToDisk: true,
6+
},
7+
output: {
8+
filenameHash: false,
9+
overrideBrowserslist: ['Chrome >= 53'],
10+
},
11+
tools: {
12+
lightningcssLoader: {
13+
exclude: {
14+
logicalProperties: true,
15+
},
16+
},
17+
},
18+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.foo {
2+
margin-inline-end: 100px;
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './index.css';

packages/core/src/plugins/css.ts

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,29 @@ const getCSSModulesLocalIdentName = (
2626
? '[local]-[hash:base64:6]'
2727
: '[path][name]__[local]-[hash:base64:6]');
2828

29+
export const getLightningCSSLoaderOptions = (
30+
config: NormalizedEnvironmentConfig,
31+
targets: string[],
32+
): Rspack.LightningcssLoaderOptions => {
33+
const userOptions =
34+
typeof config.tools.lightningcssLoader === 'object'
35+
? config.tools.lightningcssLoader
36+
: {};
37+
38+
const initialOptions: Rspack.LightningcssLoaderOptions = {
39+
targets,
40+
};
41+
42+
if (config.mode === 'production' && config.output.injectStyles) {
43+
initialOptions.minify = true;
44+
}
45+
46+
return reduceConfigs<Rspack.LightningcssLoaderOptions>({
47+
initial: initialOptions,
48+
config: userOptions,
49+
});
50+
};
51+
2952
// If the target is not `web` and the modules option of css-loader is enabled,
3053
// we must enable exportOnlyLocals to only exports the modules identifier mappings.
3154
// Otherwise, the compiled CSS code may contain invalid code, such as `new URL`.
@@ -290,29 +313,15 @@ export const pluginCss = (): RsbuildPlugin => ({
290313
) {
291314
importLoaders++;
292315

293-
const userOptions =
294-
config.tools.lightningcssLoader === true
295-
? {}
296-
: config.tools.lightningcssLoader;
297-
298-
const initialOptions: Rspack.LightningcssLoaderOptions = {
299-
targets: environment.browserslist,
300-
};
301-
302-
if (config.mode === 'production' && config.output.injectStyles) {
303-
initialOptions.minify = true;
304-
}
305-
306-
const loaderOptions =
307-
reduceConfigs<Rspack.LightningcssLoaderOptions>({
308-
initial: initialOptions,
309-
config: userOptions,
310-
});
316+
const lightningcssOptions = getLightningCSSLoaderOptions(
317+
config,
318+
environment.browserslist,
319+
);
311320

312321
rule
313322
.use(CHAIN_ID.USE.LIGHTNINGCSS)
314323
.loader('builtin:lightningcss-loader')
315-
.options(loaderOptions);
324+
.options(lightningcssOptions);
316325
}
317326

318327
const postcssLoaderOptions = await getPostcssLoaderOptions({

packages/core/src/plugins/minimize.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import type {
44
} from '@rspack/core';
55
import { rspack } from '@rspack/core';
66
import deepmerge from 'deepmerge';
7+
import { isPlainObject, pick } from '../helpers';
78
import type { NormalizedEnvironmentConfig, RsbuildPlugin } from '../types';
9+
import { getLightningCSSLoaderOptions } from './css';
810

911
export const getSwcMinimizerOptions = (
1012
config: NormalizedEnvironmentConfig,
@@ -111,9 +113,28 @@ export const pluginMinimize = (): RsbuildPlugin => ({
111113
}
112114

113115
if (minifyCss && isRspack) {
116+
const loaderOptions = getLightningCSSLoaderOptions(
117+
config,
118+
environment.browserslist,
119+
);
120+
114121
const defaultOptions: LightningCssMinimizerRspackPluginOptions = {
122+
// If user has configured `tools.lightningcssLoader` options,
123+
// we should will use them as the default minimizer options.
124+
// This helps to keep development and production consistent.
115125
minimizerOptions: {
116-
targets: environment.browserslist,
126+
targets: isPlainObject(loaderOptions.targets)
127+
? environment.browserslist
128+
: loaderOptions.targets,
129+
...pick(loaderOptions, [
130+
'draft',
131+
'include',
132+
'exclude',
133+
'nonStandard',
134+
'pseudoClasses',
135+
'unusedSymbols',
136+
'errorRecovery',
137+
]),
117138
},
118139
};
119140

website/docs/en/config/output/minify.mdx

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ type Minify =
77
| boolean
88
| {
99
js?: boolean;
10-
jsOptions?: SwcJsMinimizerRspackPluginOptions;
10+
jsOptions?: Rspack.SwcJsMinimizerRspackPluginOptions;
1111
css?: boolean;
12-
cssOptions?: LightningCssMinimizerRspackPluginOptions;
12+
cssOptions?: Rspack.LightningcssMinimizerRspackPluginOptions;
1313
};
1414
```
1515

@@ -19,18 +19,13 @@ Configure whether to enable code minification in production mode, or to configur
1919

2020
By default, JS and CSS code will be automatically minimized in production mode to improve page performance. If you do not want to minify the code, you can set `minify` to `false` to disable minification for all code. Alternatively, you can control the behavior of code minification through detailed configuration of the `minify` option. Below are detailed explanations for each configuration option:
2121

22-
Here are explanations for each field:
23-
24-
- `js`: Whether to enable minification for JavaScript code.
25-
- `jsOptions`: JS code minification configuration, which will be merged with the default configuration and passed to SWC.
26-
- `css`: Whether to enable minification for CSS code.
27-
- `cssOptions`: CSS code minification configuration, which will be merged with the default configuration and passed to Lightning CSS.
28-
2922
## Example
3023

31-
### Disable all minification
24+
### Disable minification
3225

33-
```js
26+
Set `minify` to `false` to disable JS and CSS code minification:
27+
28+
```ts title="rsbuild.config.ts"
3429
export default {
3530
output: {
3631
minify: false,
@@ -39,12 +34,21 @@ export default {
3934
```
4035

4136
:::tip
42-
This configuration is usually used for debugging and troubleshooting. It is not recommended to disable code minification in production builds, as it will significantly degrade the page performance.
37+
This usage is usually used for debugging and troubleshooting. It is not recommended to disable code minification in production builds, as it will significantly degrade the page performance.
4338
:::
4439

45-
### Disable JavaScript minification
40+
## Options
41+
42+
### minify.js
43+
44+
- **Type:** `boolean`
45+
- **Default:** `mode === 'production'`
46+
47+
Whether to enable minification for JavaScript code.
48+
49+
For example, disable JavaScript minification:
4650

47-
```js
51+
```ts title="rsbuild.config.ts"
4852
export default {
4953
output: {
5054
minify: {
@@ -54,11 +58,16 @@ export default {
5458
};
5559
```
5660

57-
### JavaScript minify options
61+
### minify.jsOptions
62+
63+
- **Type:** `Rspack.SwcJsMinimizerRspackPluginOptions`
64+
- **Default:** `{}`
5865

5966
`output.minify.jsOptions` is used to configure SWC's minification options. For detailed configurations, please refer to [SwcJsMinimizerRspackPlugin](https://rspack.dev/plugins/rspack/swc-js-minimizer-rspack-plugin). The following configuration will override the default settings, disable the mangle feature.
6067

61-
```js
68+
For example, disable the mangle feature:
69+
70+
```ts title="rsbuild.config.ts"
6271
export default {
6372
output: {
6473
minify: {
@@ -74,11 +83,35 @@ export default {
7483

7584
> Refer to [Configure SWC](/guide/basic/configure-swc) for more details.
7685
77-
### CSS minify options
86+
### minify.css
87+
88+
- **Type:** `boolean`
89+
- **Default:** `mode === 'production'`
90+
91+
Whether to enable minification for CSS code.
92+
93+
For example, disable CSS minification:
94+
95+
```ts title="rsbuild.config.ts"
96+
export default {
97+
output: {
98+
minify: {
99+
css: false,
100+
},
101+
},
102+
};
103+
```
104+
105+
### minify.cssOptions
106+
107+
- **Type:** `Rspack.LightningcssMinimizerRspackPluginOptions`
108+
- **Default:** inherit from [tools.lightningcssLoader](/config/tools/lightningcss-loader)
78109

79110
`output.minify.cssOptions` is used to configure Lightning CSS's minification options. For specific configuration items, please refer to [LightningCssMinimizerRspackPlugin Documentation](https://rspack.dev/plugins/rspack/lightning-css-minimizer-rspack-plugin).
80111

81-
```js
112+
For example, disable error recovery:
113+
114+
```ts title="rsbuild.config.ts"
82115
export default {
83116
output: {
84117
minify: {
@@ -91,3 +124,7 @@ export default {
91124
},
92125
};
93126
```
127+
128+
:::tip
129+
When you configure some options in [tools.lightningcssLoader](/config/tools/lightningcss-loader), `output.minify.cssOptions` will automatically inherit these options, which ensures that the CSS code transformation behavior in the development build is consistent with that in the production build.
130+
:::

0 commit comments

Comments
 (0)