Skip to content

Commit 859c346

Browse files
fix: compatibility with imagemin@9 (#444)
1 parent d86f90c commit 859c346

File tree

7 files changed

+3306
-2721
lines changed

7 files changed

+3306
-2721
lines changed

.husky/commit-msg

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
#!/bin/sh
2-
. "$(dirname "$0")/_/husky.sh"
3-
41
npx --no-install commitlint --edit $1

.husky/pre-commit

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
#!/bin/sh
2-
. "$(dirname "$0")/_/husky.sh"
3-
41
npx --no-install lint-staged

package-lock.json

Lines changed: 3248 additions & 2692 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"test:coverage": "npm run test:only -- --collectCoverageFrom=\"src/**/*.js\" --coverage",
3939
"pretest": "npm run lint",
4040
"test": "npm run test:coverage",
41-
"prepare": "husky install && npm run build",
41+
"prepare": "husky && npm run build",
4242
"release": "standard-version"
4343
},
4444
"files": [
@@ -76,10 +76,10 @@
7676
"@babel/cli": "^7.24.5",
7777
"@babel/core": "^7.24.5",
7878
"@babel/preset-env": "^7.24.5",
79-
"@commitlint/cli": "^18.6.1",
80-
"@commitlint/config-conventional": "^18.6.2",
79+
"@commitlint/cli": "^19.3.0",
80+
"@commitlint/config-conventional": "^19.2.2",
8181
"@squoosh/lib": "^0.5.3",
82-
"@types/imagemin": "^8.0.5",
82+
"@types/imagemin": "^9.0.0",
8383
"@types/node": "^20.12.8",
8484
"@types/serialize-javascript": "^5.0.4",
8585
"@types/sharp": "^0.32.0",
@@ -101,9 +101,9 @@
101101
"eslint-plugin-unicorn": "^44.0.2",
102102
"file-loader": "^6.2.0",
103103
"file-type": "^16.5.4",
104-
"husky": "^8.0.3",
104+
"husky": "^9.0.11",
105105
"image-size": "^1.1.1",
106-
"imagemin": "^8.0.1",
106+
"imagemin": "^9.0.0",
107107
"imagemin-avif": "^0.1.6",
108108
"imagemin-gifsicle": "^7.0.0",
109109
"imagemin-mozjpeg": "^9.0.0",
@@ -121,7 +121,7 @@
121121
"remark-preset-lint-itgalaxy": "^16.0.0",
122122
"sharp": "^0.33.3",
123123
"standard-version": "^9.5.0",
124-
"svgo": "^3.2.0",
124+
"svgo": "^3.3.2",
125125
"tempy": "^1.0.1",
126126
"typescript": "^5.4.5",
127127
"url-loader": "^4.1.1",

src/utils.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ async function imageminGenerate(original, minimizerOptions) {
621621
const minimizerOptionsNormalized = /** @type {ImageminOptions} */ (
622622
await imageminNormalizeConfig(
623623
/** @type {ImageminOptions} */ (
624-
/** @type {?} */ (minimizerOptions || {})
624+
/** @type {?} */ (minimizerOptions ?? {})
625625
),
626626
)
627627
);
@@ -657,7 +657,8 @@ async function imageminGenerate(original, minimizerOptions) {
657657

658658
return {
659659
filename: newFilename,
660-
data: result,
660+
// imagemin@8 returns buffer, but imagemin@9 returns uint8array
661+
data: !Buffer.isBuffer(result) ? Buffer.from(result) : result,
661662
warnings: [...original.warnings],
662663
errors: [...original.errors],
663664
info: {
@@ -677,7 +678,7 @@ async function imageminGenerate(original, minimizerOptions) {
677678
async function imageminMinify(original, options) {
678679
const minimizerOptionsNormalized = /** @type {ImageminOptions} */ (
679680
await imageminNormalizeConfig(
680-
/** @type {ImageminOptions} */ (/** @type {?} */ (options || {})),
681+
/** @type {ImageminOptions} */ (/** @type {?} */ (options ?? {})),
681682
)
682683
);
683684

@@ -718,7 +719,8 @@ async function imageminMinify(original, options) {
718719

719720
return {
720721
filename: original.filename,
721-
data: result,
722+
// imagemin@8 returns buffer, but imagemin@9 returns uint8array
723+
data: !Buffer.isBuffer(result) ? Buffer.from(result) : result,
722724
warnings: [...original.warnings],
723725
errors: [...original.errors],
724726
info: {
@@ -790,7 +792,7 @@ async function squooshGenerate(original, minifyOptions) {
790792
const imagePool = pool || squooshImagePoolCreate();
791793
const image = imagePool.ingestImage(new Uint8Array(original.data));
792794

793-
const squooshOptions = /** @type {SquooshOptions} */ (minifyOptions || {});
795+
const squooshOptions = /** @type {SquooshOptions} */ (minifyOptions ?? {});
794796

795797
const preprocEntries = Object.entries(squooshOptions).filter(
796798
([key, value]) => {
@@ -909,7 +911,7 @@ async function squooshMinify(original, options) {
909911
const isReusePool = Boolean(pool);
910912
const imagePool = pool || squooshImagePoolCreate();
911913
const image = imagePool.ingestImage(new Uint8Array(original.data));
912-
const squooshOptions = /** @type {SquooshOptions} */ (options || {});
914+
const squooshOptions = /** @type {SquooshOptions} */ (options ?? {});
913915

914916
const preprocEntries = Object.entries(squooshOptions).filter(
915917
([key, value]) => {
@@ -1227,8 +1229,7 @@ async function svgoMinify(original, minimizerOptions) {
12271229
/** @type {SvgoLib} */
12281230
// eslint-disable-next-line node/no-unpublished-require
12291231
const { optimize } = require("svgo");
1230-
1231-
const { encodeOptions } = /** @type {SvgoOptions} */ (minimizerOptions);
1232+
const { encodeOptions } = /** @type {SvgoOptions} */ (minimizerOptions ?? {});
12321233

12331234
/** @type {import("svgo").Output} */
12341235
let result;

test/ImageminPlugin.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,38 @@ describe("imagemin plugin", () => {
592592
expect(errors).toHaveLength(0);
593593
});
594594

595+
it("should work with asset/inline (svgoMinify) without options", async () => {
596+
const compiler = await runWebpack(
597+
{
598+
fileLoaderOff: true,
599+
assetInline: true,
600+
entry: path.join(fixturesPath, "./asset-inline.js"),
601+
emitPlugin: true,
602+
imageminPluginOptions: {
603+
minimizer: {
604+
implementation: ImageMinimizerPlugin.svgoMinify,
605+
},
606+
},
607+
},
608+
true,
609+
);
610+
611+
const stats = await compile(compiler);
612+
const { compilation } = stats;
613+
const { warnings, errors } = compilation;
614+
615+
const result = readAsset("bundle.js", compiler, stats).toString();
616+
617+
const isInlineSvg =
618+
/data:image\/svg\+xml;base64,PHN2ZyB3aWR0aD0iMTAwIiBoZWlnaHQ9IjEwMCI\+PGNpcmNsZSBjeD0iNTAiIGN5PSI1MCIgcj0iNDAiIHN0eWxlPSJzdHJva2U6IzAwMDtzdHJva2Utd2l0aDozO2ZpbGw6cmVkIi8\+PC9zdmc\+/.test(
619+
result,
620+
);
621+
622+
expect(isInlineSvg).toBe(true);
623+
expect(warnings).toHaveLength(0);
624+
expect(errors).toHaveLength(0);
625+
});
626+
595627
it("should work and use the persistent cache by default (loader + plugin)", async () => {
596628
const compiler = await runWebpack(
597629
{

test/helpers.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -266,14 +266,16 @@ function isOptimized(originalPath, compilation) {
266266
return Promise.resolve()
267267
.then(() => pify(fs.readFile)(pathToOriginal))
268268
.then((data) =>
269-
imagemin.buffer(data, {
270-
plugins: [
271-
imageminGifsicle(),
272-
imageminMozjpeg(),
273-
imageminPngquant(),
274-
imageminSvgo(),
275-
],
276-
}),
269+
imagemin
270+
.buffer(data, {
271+
plugins: [
272+
imageminGifsicle(),
273+
imageminMozjpeg(),
274+
imageminPngquant(),
275+
imageminSvgo(),
276+
],
277+
})
278+
.then((result) => Buffer.from(result)),
277279
)
278280
.then((optimizedBuffer) =>
279281
Promise.resolve()

0 commit comments

Comments
 (0)