Skip to content

Commit 608b22d

Browse files
authored
feat: allow to use with Rspack (#18)
1 parent 10f58ad commit 608b22d

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ export default {
3131
};
3232
```
3333

34+
This plugin is compatible with both Rsbuild and Rspack. If you are using Rspack instead of Rsbuild, you can import the `ImageMinimizerPlugin` from the package, see [Rspack Usage](#rspack-usage).
35+
3436
## Default Compressors
3537

3638
By default, the plugin will enable `jpeg`, `png`, `ico` image compressors, which are equivalent to the following two examples:
@@ -98,6 +100,52 @@ For example, the `png` compressor will take precedence over the `pngLossless` co
98100
pluginImageCompress(["jpeg", "pngLossless", "ico", "png"]);
99101
```
100102

103+
## Rspack Usage
104+
105+
The plugin is also compatible with Rspack.
106+
107+
If you are using Rspack instead of Rsbuild, you can import the `ImageMinimizerPlugin` from the package, use it in the [optimization.minimizer](https://rspack.dev/config/optimization#optimizationminimizer) array.
108+
109+
```ts
110+
// rspack.config.mjs
111+
import { ImageMinimizerPlugin } from "@rsbuild/plugin-image-compress";
112+
import { defineConfig } from "@rspack/cli";
113+
114+
export default defineConfig({
115+
mode: process.env.NODE_ENV === "production" ? "production" : "development",
116+
optimization: {
117+
minimizer: [
118+
// Use `...` to preserve the default JS and CSS minimizers of Rspack
119+
'...',
120+
// Add the image minimizer plugins
121+
new ImageMinimizerPlugin({
122+
use: "jpeg",
123+
test: /\.(?:jpg|jpeg)$/,
124+
}),
125+
new ImageMinimizerPlugin({
126+
use: "png",
127+
test: /\.png$/,
128+
maxQuality: 50,
129+
}),
130+
new ImageMinimizerPlugin({
131+
use: "avif",
132+
test: /\.avif$/,
133+
quality: 80,
134+
}),
135+
new ImageMinimizerPlugin({
136+
use: "svg",
137+
test: /\.svg$/,
138+
floatPrecision: 2,
139+
}),
140+
new ImageMinimizerPlugin({
141+
use: "ico",
142+
test: /\.(?:ico|icon)$/,
143+
}),
144+
],
145+
},
146+
});
147+
```
148+
101149
## License
102150

103151
[MIT](./LICENSE).

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ const normalizeOptions = (options: Options[]) => {
3535

3636
export const PLUGIN_IMAGE_COMPRESS_NAME = 'rsbuild:image-compress';
3737

38+
export { ImageMinimizerPlugin };
39+
3840
/** Options enable by default: {@link DEFAULT_OPTIONS} */
3941
export const pluginImageCompress: IPluginImageCompress = (
4042
...args

0 commit comments

Comments
 (0)