|
1 | | -# rsbuild-plugin-example |
| 1 | +# rsbuild-plugin-css-optimizer |
2 | 2 |
|
3 | | -Example plugin for Rsbuild. |
| 3 | +An Rsbuild plugin to customize CSS minification, allowing you to choose between [cssnano](https://github.com/cssnano/cssnano) (JavaScript-based) or [Lightning CSS](https://lightningcss.dev) (Rust-based) for high-performance CSS compression. |
| 4 | + |
| 5 | +`rsbuild-plugin-css-optimizer` internally integrates [css-minimizer-webpack-plugin](https://github.com/webpack-contrib/css-minimizer-webpack-plugin) to optimize CSS assets in production builds. |
4 | 6 |
|
5 | 7 | <p> |
6 | | - <a href="https://npmjs.com/package/rsbuild-plugin-example"> |
7 | | - <img src="https://img.shields.io/npm/v/rsbuild-plugin-example?style=flat-square&colorA=564341&colorB=EDED91" alt="npm version" /> |
| 8 | + <a href="https://npmjs.com/package/rsbuild-plugin-css-optimizer"> |
| 9 | + <img src="https://img.shields.io/npm/v/rsbuild-plugin-css-optimizer?style=flat-square&colorA=564341&colorB=EDED91" alt="npm version" /> |
8 | 10 | </a> |
9 | 11 | <img src="https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square&colorA=564341&colorB=EDED91" alt="license" /> |
10 | | - <a href="https://npmcharts.com/compare/rsbuild-plugin-example?minimal=true"><img src="https://img.shields.io/npm/dm/rsbuild-plugin-example.svg?style=flat-square&colorA=564341&colorB=EDED91" alt="downloads" /></a> |
11 | 12 | </p> |
12 | 13 |
|
13 | | -## Usage |
| 14 | +## Features |
| 15 | + |
| 16 | +- **Flexible Minifier Choice**: Switch between `cssnano` for extensive optimizations or `Lightning CSS` for 5-10x faster minification. |
| 17 | +- **Performance**: Leverage `Lightning CSS`’s Rust-based engine for parallel processing and native performance. |
| 18 | +- **Type Safety**: TypeScript types ensure correct configuration for each minifier. |
| 19 | +- **Seamless Rsbuild Integration**: Automatically applies minification in production builds when `minify.css` is enabled. |
| 20 | +- **Customizable Options**: Fine-tune `cssnano` and `Lightning CSS` settings for browser targeting, CSS features, and more. |
14 | 21 |
|
15 | | -Install: |
| 22 | +## Installation |
| 23 | + |
| 24 | +Install the plugin and its dependency: |
16 | 25 |
|
17 | 26 | ```bash |
18 | | -npm add rsbuild-plugin-example -D |
| 27 | +npm add rsbuild-plugin-css-optimizer -D |
19 | 28 | ``` |
20 | 29 |
|
21 | | -Add plugin to your `rsbuild.config.ts`: |
| 30 | +## Usage |
| 31 | + |
| 32 | +Add the `pluginCssMinimizer` to your Rsbuild configuration in `rsbuild.config.ts`. The plugin enables CSS minification in production builds when `output.minify.css` is `true`. |
| 33 | + |
| 34 | +### Basic Example (Lightning CSS) |
22 | 35 |
|
23 | 36 | ```ts |
24 | 37 | // rsbuild.config.ts |
25 | | -import { pluginExample } from "rsbuild-plugin-example"; |
| 38 | +import { pluginCssMinimizer } from "rsbuild-plugin-css-optimizer"; |
26 | 39 |
|
27 | 40 | export default { |
28 | | - plugins: [pluginExample()], |
| 41 | + plugins: [ |
| 42 | + pluginCssMinimizer({ |
| 43 | + minifier: "lightningcss", |
| 44 | + lightningCssOptions: { |
| 45 | + minimizerOptions: { |
| 46 | + targets: ["> 0.25%", "not dead"], |
| 47 | + }, |
| 48 | + }, |
| 49 | + }), |
| 50 | + ], |
| 51 | +}; |
| 52 | +``` |
| 53 | + |
| 54 | +### Basic Example (cssnano) |
| 55 | + |
| 56 | +```ts |
| 57 | +// rsbuild.config.ts |
| 58 | +import { pluginCssMinimizer } from "rsbuild-plugin-css-optimizer"; |
| 59 | + |
| 60 | +export default { |
| 61 | + plugins: [ |
| 62 | + pluginCssMinimizer(), // Uses cssnano by default |
| 63 | + ], |
29 | 64 | }; |
30 | 65 | ``` |
31 | 66 |
|
32 | 67 | ## Options |
33 | 68 |
|
34 | | -### foo |
| 69 | +The plugin accepts a `PluginCssMinimizerOptions` object with the following properties: |
| 70 | + |
| 71 | +### `minifier` |
| 72 | + |
| 73 | +- **Type**: `'cssnano' | 'lightningcss'` |
| 74 | +- **Default**: `'cssnano'` |
| 75 | +- **Description**: Specifies the CSS minifier to use. |
| 76 | + - `'cssnano'`: JavaScript-based minifier with a wide range of optimizations. |
| 77 | + - `'lightningcss'`: Rust-based minifier for significantly faster performance. |
| 78 | +- **Example**: |
| 79 | + ```ts |
| 80 | + pluginCssMinimizer({ |
| 81 | + minifier: "lightningcss", |
| 82 | + }); |
| 83 | + ``` |
35 | 84 |
|
36 | | -Some description. |
| 85 | +### `cssnanoOptions` |
37 | 86 |
|
38 | | -- Type: `string` |
39 | | -- Default: `undefined` |
40 | | -- Example: |
| 87 | +- **Type**: `ConfigChain<CssMinimizerPlugin.BasePluginOptions & CssMinimizerPlugin.DefinedDefaultMinimizerAndOptions<CssNanoOptions>> | Function` |
| 88 | +- **Default**: |
| 89 | + ```ts |
| 90 | + { |
| 91 | + minify: CssMinimizerWebpackPlugin.cssnanoMinify, |
| 92 | + minimizerOptions: { |
| 93 | + preset: ['default', { mergeLonghand: false }], |
| 94 | + }, |
| 95 | + } |
| 96 | + ``` |
| 97 | +- **Description**: Configuration for `cssnano`, applied when `minifier` is `'cssnano'`. Can be an object merged with defaults or a function to modify options. See [cssnano documentation](https://cssnano.co/docs) for all available options. |
| 98 | +- **Sub-options**: |
| 99 | + - `configFile` (string, optional): Path to a cssnano configuration file. |
| 100 | + - `preset` (string | [string, object], optional): Preset name and configuration (e.g., `['default', { mergeLonghand: false }]`). |
| 101 | +- **Example (Object)**: |
| 102 | + ```ts |
| 103 | + pluginCssMinimizer({ |
| 104 | + minifier: "cssnano", |
| 105 | + cssnanoOptions: { |
| 106 | + minimizerOptions: { |
| 107 | + preset: ["advanced", { discardComments: { removeAll: true } }], |
| 108 | + }, |
| 109 | + }, |
| 110 | + }); |
| 111 | + ``` |
| 112 | +- **Example (Function)**: |
| 113 | + ```ts |
| 114 | + pluginCssMinimizer({ |
| 115 | + minifier: "cssnano", |
| 116 | + cssnanoOptions: (options) => { |
| 117 | + options.minimizerOptions = { |
| 118 | + preset: require.resolve("cssnano-preset-simple"), |
| 119 | + }; |
| 120 | + return options; |
| 121 | + }, |
| 122 | + }); |
| 123 | + ``` |
41 | 124 |
|
42 | | -```js |
43 | | -pluginExample({ |
44 | | - foo: "bar", |
45 | | -}); |
| 125 | +### `lightningCssOptions` |
| 126 | + |
| 127 | +- **Type**: `ConfigChain<CssMinimizerPlugin.BasePluginOptions & CssMinimizerPlugin.DefinedDefaultMinimizerAndOptions<LightningCssOptions>> | Function` |
| 128 | +- **Default**: |
| 129 | + ```ts |
| 130 | + { |
| 131 | + minify: CssMinimizerWebpackPlugin.lightningCssMinify, |
| 132 | + minimizerOptions: { |
| 133 | + targets: 'defaults', |
| 134 | + }, |
| 135 | + } |
| 136 | + ``` |
| 137 | +- **Description**: Configuration for `Lightning CSS`, applied when `minifier` is `'lightningcss'`. Can be an object merged with defaults or a function to modify options. See [Lightning CSS documentation](https://lightningcss.dev/options.html) for all available options. |
| 138 | +- **Sub-options**: |
| 139 | + - `targets` (string | string[] | { [key: string]: number }, optional): Browser targets using browserslist syntax (e.g., `['> 0.25%', 'not dead']`) or version objects (e.g., `{ chrome: 80 }`). |
| 140 | + - `drafts` (object, optional): Enable draft CSS features, such as `{ nesting: true }`. |
| 141 | +- **Example (Object)**: |
| 142 | + ```ts |
| 143 | + pluginCssMinimizer({ |
| 144 | + minifier: "lightningcss", |
| 145 | + lightningCssOptions: { |
| 146 | + minimizerOptions: { |
| 147 | + targets: ["chrome >= 80", "firefox >= 78"], |
| 148 | + drafts: { nesting: true }, |
| 149 | + }, |
| 150 | + }, |
| 151 | + }); |
| 152 | + ``` |
| 153 | +- **Example (Function)**: |
| 154 | + ```ts |
| 155 | + pluginCssMinimizer({ |
| 156 | + minifier: "lightningcss", |
| 157 | + lightningCssOptions: (options) => { |
| 158 | + options.minimizerOptions.targets = ["> 0.5%"]; |
| 159 | + return options; |
| 160 | + }, |
| 161 | + }); |
| 162 | + ``` |
| 163 | + |
| 164 | +## Full Example |
| 165 | + |
| 166 | +```ts |
| 167 | +// rsbuild.config.ts |
| 168 | +import { pluginCssMinimizer } from "rsbuild-plugin-css-optimizer"; |
| 169 | + |
| 170 | +export default { |
| 171 | + output: { |
| 172 | + minify: { |
| 173 | + css: true, // Enable CSS minification |
| 174 | + }, |
| 175 | + }, |
| 176 | + plugins: [ |
| 177 | + pluginCssMinimizer({ |
| 178 | + minifier: "lightningcss", |
| 179 | + lightningCssOptions: { |
| 180 | + minimizerOptions: { |
| 181 | + targets: ["> 0.25%", "not dead"], |
| 182 | + drafts: { nesting: true }, |
| 183 | + }, |
| 184 | + }, |
| 185 | + }), |
| 186 | + // Alternatively, use cssnano |
| 187 | + pluginCssMinimizer({ |
| 188 | + minifier: "cssnano", |
| 189 | + cssnanoOptions: { |
| 190 | + minimizerOptions: { |
| 191 | + preset: ["default", { discardComments: { removeAll: true } }], |
| 192 | + }, |
| 193 | + }, |
| 194 | + }), |
| 195 | + ], |
| 196 | +}; |
46 | 197 | ``` |
47 | 198 |
|
| 199 | +## Performance |
| 200 | + |
| 201 | +- **Lightning CSS**: |
| 202 | + - **Speed**: 5-10x faster than `cssnano` due to Rust’s compiled performance and parallel processing. |
| 203 | + - **Use Case**: Ideal for large projects or frequent builds where build speed is critical. |
| 204 | +- **cssnano**: |
| 205 | + - **Speed**: Slower but provides extensive optimization plugins for fine-grained control. |
| 206 | + - **Use Case**: Best for projects prioritizing minimal CSS output size over build performance. |
| 207 | + |
| 208 | +## Notes |
| 209 | + |
| 210 | +- **Source Maps**: Enable source maps in Rsbuild with `devtool: 'source-map'` for both minifiers. |
| 211 | +- **Browser Targets**: For `Lightning CSS`, use browserslist syntax (e.g., `['> 0.25%']`) or version objects (e.g., `{ chrome: 80 }`). The default (`'defaults'`) targets modern browsers. |
| 212 | +- **cssnano Compatibility**: The `mergeLonghand: false` default prevents issues with properties like `safe-area-inset-top` (see [cssnano issue #803](https://github.com/cssnano/cssnano/issues/803)). |
| 213 | +- **Production Only**: Minification applies only in production builds when `isProd` is `true` and `minify.css` is enabled. |
| 214 | + |
| 215 | +## Troubleshooting |
| 216 | + |
| 217 | +- **Minification Not Applied**: |
| 218 | + - Verify `output.minify.css` is `true` or `minify` is `true` in your Rsbuild config. |
| 219 | + - Ensure the build is in production mode (`isProd: true`). |
| 220 | +- **Invalid Options**: |
| 221 | + - Check TypeScript errors for incorrect `cssnanoOptions` or `lightningCssOptions`. |
| 222 | + - Refer to [cssnano](https://cssnano.co/docs) or [Lightning CSS](https://lightningcss.dev/options.html) documentation for valid options. |
| 223 | + |
| 224 | +## Contributing |
| 225 | + |
| 226 | +Contributions are welcome! Please submit issues or pull requests to the [plugin repository](https://github.com/aliezzahn/rsbuild-plugin-css-optimizer). |
| 227 | + |
48 | 228 | ## License |
49 | 229 |
|
50 | | -[MIT](./LICENSE). |
| 230 | +[MIT](./LICENSE) |
0 commit comments