Skip to content

Commit

Permalink
feat: add image-minimizer-webpack-plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
puehringer committed Jan 31, 2025
1 parent 2881512 commit 51b4fc2
Show file tree
Hide file tree
Showing 3 changed files with 544 additions and 71 deletions.
39 changes: 39 additions & 0 deletions config/rspack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
const ReactRefreshPlugin = require('@rspack/plugin-react-refresh');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { RsdoctorRspackPlugin } = require('@rsdoctor/rspack-plugin');
const ImageMinimizerPlugin = require('image-minimizer-webpack-plugin');

// Load the current .env and expand it
const parsedEnv = dotenvExpand.expand(dotenv.config());
Expand Down Expand Up @@ -216,12 +217,50 @@ module.exports = (webpackEnv, argv) => {
// A missing `test` is equivalent to a match.
{
test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
use: [
{
// Compress images as described in https://rspack.dev/guide/features/asset-module#using-optimizers-as-loaders
loader: ImageMinimizerPlugin.loader,
options: {
minimizer: {
implementation: ImageMinimizerPlugin.sharpMinify,
options: {
encodeOptions: {
// https://sharp.pixelplumbing.com/api-output
},
},
},
},
},
],
type: 'asset',
},
{
test: /\.svg(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
// css-loader is messing up SVGs: https://github.com/webpack/webpack/issues/13835
// Pin css-loader and always load them as file-resource.
use: [
{
// Compress images as described in https://rspack.dev/guide/features/asset-module#using-optimizers-as-loaders
loader: ImageMinimizerPlugin.loader,
options: {
minimizer: {
implementation: ImageMinimizerPlugin.svgoMinify,
options: {
encodeOptions: {
// Pass over SVGs multiple times to ensure all optimizations are applied. False by default
multipass: true,
plugins: [
// set of built-in plugins enabled by default
// see: https://github.com/svg/svgo#default-preset
'preset-default',
],
},
},
},
},
},
],
type: 'asset/resource',
},
{
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"html-loader": "~5.1.0",
"html-webpack-plugin": "^5.6.3",
"identity-obj-proxy": "^3.0.0",
"image-minimizer-webpack-plugin": "^4.1.3",
"jest": "^27.5.1",
"jest-raw-loader": "~1.0.1",
"jest-resolve": "^27.5.1",
Expand All @@ -88,7 +89,9 @@
"rimraf": "~6.0.1",
"sass-embedded": "~1.83.4",
"sass-loader": "^16.0.4",
"sharp": "^0.33.5",
"shx": "~0.3.4",
"svgo": "^3.3.2",
"tailwindcss": "^3.4.17",
"ts-checker-rspack-plugin": "^1.1.1",
"ts-node": "^10.9.2",
Expand Down
Loading

0 comments on commit 51b4fc2

Please sign in to comment.