Skip to content

Commit

Permalink
fix(imagemin): use image min as loader
Browse files Browse the repository at this point in the history
RSPack does not support imagemin plugin
  • Loading branch information
Heymdall committed Oct 29, 2024
1 parent 9251a4a commit fe73e5d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,46 +1,63 @@
import ImageMinimizerPlugin from 'image-minimizer-webpack-plugin';

import configs from '../../../app-configs';
import { configs } from '../../../app-configs';

export const getImageMin = () => {
export function getImageMinLoader() {
const { svg, gif, jpg, png } = configs.imageMinimizer ?? {};
const doesAnyMinificationEnabled = svg?.enabled || gif?.enabled || jpg?.enabled || png?.enabled;

return [
doesAnyMinificationEnabled &&
new ImageMinimizerPlugin({
minimizer: {
implementation: ImageMinimizerPlugin.imageminMinify,
options: {
plugins: [
jpg?.enabled && [
'mozjpeg',
{
quality: jpg?.quality,
progressive: true,
},
],
png?.enabled && [
'optipng',
{
optimizationLevel: png?.optimizationLevel,
bitDepthReduction: png?.bitDepthReduction,
colorTypeReduction: png?.colorTypeReduction,
paletteReduction: png?.paletteReduction,
interlaced: png?.interlaced,
},
],
svg?.enabled && ['svgo'],
gif?.enabled && [
'gifsicle',
{
optimizationLevel: gif?.optimizationLevel,
interlaced: true,
},
],
].filter(Boolean),
if (!doesAnyMinificationEnabled) {
return false;
}

const loaderExtensions = [
svg?.enabled && 'svg',
gif?.enabled && 'gif',
jpg?.enabled && 'jpe?g',
png?.enabled && 'png',
].filter(Boolean);

return {
test: new RegExp(`\\.(${loaderExtensions.join('|')})$`),
type: 'assets',
use: [
{
loader: ImageMinimizerPlugin.loader,
options: {
minimizer: {
implementation: ImageMinimizerPlugin.imageminMinify,
options: {
plugins: [
jpg?.enabled && [
'mozjpeg',
{
quality: jpg?.quality,
progressive: true,
},
],
png?.enabled && [
'optipng',
{
optimizationLevel: png?.optimizationLevel,
bitDepthReduction: png?.bitDepthReduction,
colorTypeReduction: png?.colorTypeReduction,
paletteReduction: png?.paletteReduction,
interlaced: png?.interlaced,
},
],
svg?.enabled && ['svgo'],
gif?.enabled && [
'gifsicle',
{
optimizationLevel: gif?.optimizationLevel,
interlaced: true,
},
],
].filter(Boolean),
},
},
},
}),
];
};
},
],
}
}
4 changes: 2 additions & 2 deletions packages/arui-scripts/src/configs/webpack.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { WebpackDeduplicationPlugin } from 'webpack-deduplication-plugin';
import { AruiRuntimePlugin, getInsertCssRuntimeMethod } from '../plugins/arui-runtime';
import { htmlTemplate } from '../templates/html.template';

import { getImageMin } from './config-extras/minimizers';
import { getImageMinLoader } from './config-extras/minimizers';
import checkNodeVersion from './util/check-node-version';
import getEntry, { Entry } from './util/get-entry';
import { configs } from './app-configs';
Expand Down Expand Up @@ -63,7 +63,6 @@ function getMinimizeConfig(mode: 'dev' | 'prod') {
return {
minimize: true,
minimizer: [
...getImageMin(),
new TerserPlugin({
terserOptions: {
parse: {
Expand Down Expand Up @@ -302,6 +301,7 @@ export const createSingleClientWebpackConfig = (
},
},
},
mode === 'prod' && getImageMinLoader(),
{
exclude: [/\.(js|jsx|mjs|cjs|ts|tsx)$/, /\.(html|ejs)$/, /\.json$/],
type: 'asset',
Expand Down

0 comments on commit fe73e5d

Please sign in to comment.