Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export default {
};
```

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).

## Default Compressors

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

## Rspack Usage

The plugin is also compatible with Rspack.

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.

```ts
// rspack.config.mjs
import { ImageMinimizerPlugin } from "@rsbuild/plugin-image-compress";
import { defineConfig } from "@rspack/cli";

export default defineConfig({
mode: process.env.NODE_ENV === "production" ? "production" : "development",
optimization: {
minimizer: [
// Use `...` to preserve the default JS and CSS minimizers of Rspack
'...',
// Add the image minimizer plugins
new ImageMinimizerPlugin({
use: "jpeg",
test: /\.(?:jpg|jpeg)$/,
}),
new ImageMinimizerPlugin({
use: "png",
test: /\.png$/,
maxQuality: 50,
}),
new ImageMinimizerPlugin({
use: "avif",
test: /\.avif$/,
quality: 80,
}),
new ImageMinimizerPlugin({
use: "svg",
test: /\.svg$/,
floatPrecision: 2,
}),
new ImageMinimizerPlugin({
use: "ico",
test: /\.(?:ico|icon)$/,
}),
],
},
});
```

## License

[MIT](./LICENSE).
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const normalizeOptions = (options: Options[]) => {

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

export { ImageMinimizerPlugin };

/** Options enable by default: {@link DEFAULT_OPTIONS} */
export const pluginImageCompress: IPluginImageCompress = (
...args
Expand Down