Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: compatibility with imagemin@9 #444

Merged
merged 2 commits into from
Jun 4, 2024
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
3 changes: 0 additions & 3 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx --no-install commitlint --edit $1
3 changes: 0 additions & 3 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx --no-install lint-staged
5,940 changes: 3,248 additions & 2,692 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"test:coverage": "npm run test:only -- --collectCoverageFrom=\"src/**/*.js\" --coverage",
"pretest": "npm run lint",
"test": "npm run test:coverage",
"prepare": "husky install && npm run build",
"prepare": "husky && npm run build",
"release": "standard-version"
},
"files": [
Expand Down Expand Up @@ -76,10 +76,10 @@
"@babel/cli": "^7.24.5",
"@babel/core": "^7.24.5",
"@babel/preset-env": "^7.24.5",
"@commitlint/cli": "^18.6.1",
"@commitlint/config-conventional": "^18.6.2",
"@commitlint/cli": "^19.3.0",
"@commitlint/config-conventional": "^19.2.2",
"@squoosh/lib": "^0.5.3",
"@types/imagemin": "^8.0.5",
"@types/imagemin": "^9.0.0",
"@types/node": "^20.12.8",
"@types/serialize-javascript": "^5.0.4",
"@types/sharp": "^0.32.0",
Expand All @@ -101,9 +101,9 @@
"eslint-plugin-unicorn": "^44.0.2",
"file-loader": "^6.2.0",
"file-type": "^16.5.4",
"husky": "^8.0.3",
"husky": "^9.0.11",
"image-size": "^1.1.1",
"imagemin": "^8.0.1",
"imagemin": "^9.0.0",
"imagemin-avif": "^0.1.6",
"imagemin-gifsicle": "^7.0.0",
"imagemin-mozjpeg": "^9.0.0",
Expand All @@ -121,7 +121,7 @@
"remark-preset-lint-itgalaxy": "^16.0.0",
"sharp": "^0.33.3",
"standard-version": "^9.5.0",
"svgo": "^3.2.0",
"svgo": "^3.3.2",
"tempy": "^1.0.1",
"typescript": "^5.4.5",
"url-loader": "^4.1.1",
Expand Down
17 changes: 9 additions & 8 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ async function imageminGenerate(original, minimizerOptions) {
const minimizerOptionsNormalized = /** @type {ImageminOptions} */ (
await imageminNormalizeConfig(
/** @type {ImageminOptions} */ (
/** @type {?} */ (minimizerOptions || {})
/** @type {?} */ (minimizerOptions ?? {})
),
)
);
Expand Down Expand Up @@ -657,7 +657,8 @@ async function imageminGenerate(original, minimizerOptions) {

return {
filename: newFilename,
data: result,
// imagemin@8 returns buffer, but imagemin@9 returns uint8array
data: !Buffer.isBuffer(result) ? Buffer.from(result) : result,
warnings: [...original.warnings],
errors: [...original.errors],
info: {
Expand All @@ -677,7 +678,7 @@ async function imageminGenerate(original, minimizerOptions) {
async function imageminMinify(original, options) {
const minimizerOptionsNormalized = /** @type {ImageminOptions} */ (
await imageminNormalizeConfig(
/** @type {ImageminOptions} */ (/** @type {?} */ (options || {})),
/** @type {ImageminOptions} */ (/** @type {?} */ (options ?? {})),
)
);

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

return {
filename: original.filename,
data: result,
// imagemin@8 returns buffer, but imagemin@9 returns uint8array
data: !Buffer.isBuffer(result) ? Buffer.from(result) : result,
warnings: [...original.warnings],
errors: [...original.errors],
info: {
Expand Down Expand Up @@ -790,7 +792,7 @@ async function squooshGenerate(original, minifyOptions) {
const imagePool = pool || squooshImagePoolCreate();
const image = imagePool.ingestImage(new Uint8Array(original.data));

const squooshOptions = /** @type {SquooshOptions} */ (minifyOptions || {});
const squooshOptions = /** @type {SquooshOptions} */ (minifyOptions ?? {});

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

const preprocEntries = Object.entries(squooshOptions).filter(
([key, value]) => {
Expand Down Expand Up @@ -1227,8 +1229,7 @@ async function svgoMinify(original, minimizerOptions) {
/** @type {SvgoLib} */
// eslint-disable-next-line node/no-unpublished-require
const { optimize } = require("svgo");

const { encodeOptions } = /** @type {SvgoOptions} */ (minimizerOptions);
const { encodeOptions } = /** @type {SvgoOptions} */ (minimizerOptions ?? {});

/** @type {import("svgo").Output} */
let result;
Expand Down
32 changes: 32 additions & 0 deletions test/ImageminPlugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,38 @@ describe("imagemin plugin", () => {
expect(errors).toHaveLength(0);
});

it("should work with asset/inline (svgoMinify) without options", async () => {
const compiler = await runWebpack(
{
fileLoaderOff: true,
assetInline: true,
entry: path.join(fixturesPath, "./asset-inline.js"),
emitPlugin: true,
imageminPluginOptions: {
minimizer: {
implementation: ImageMinimizerPlugin.svgoMinify,
},
},
},
true,
);

const stats = await compile(compiler);
const { compilation } = stats;
const { warnings, errors } = compilation;

const result = readAsset("bundle.js", compiler, stats).toString();

const isInlineSvg =
/data:image\/svg\+xml;base64,PHN2ZyB3aWR0aD0iMTAwIiBoZWlnaHQ9IjEwMCI\+PGNpcmNsZSBjeD0iNTAiIGN5PSI1MCIgcj0iNDAiIHN0eWxlPSJzdHJva2U6IzAwMDtzdHJva2Utd2l0aDozO2ZpbGw6cmVkIi8\+PC9zdmc\+/.test(
result,
);

expect(isInlineSvg).toBe(true);
expect(warnings).toHaveLength(0);
expect(errors).toHaveLength(0);
});

it("should work and use the persistent cache by default (loader + plugin)", async () => {
const compiler = await runWebpack(
{
Expand Down
18 changes: 10 additions & 8 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,14 +266,16 @@ function isOptimized(originalPath, compilation) {
return Promise.resolve()
.then(() => pify(fs.readFile)(pathToOriginal))
.then((data) =>
imagemin.buffer(data, {
plugins: [
imageminGifsicle(),
imageminMozjpeg(),
imageminPngquant(),
imageminSvgo(),
],
}),
imagemin
.buffer(data, {
plugins: [
imageminGifsicle(),
imageminMozjpeg(),
imageminPngquant(),
imageminSvgo(),
],
})
.then((result) => Buffer.from(result)),
)
.then((optimizedBuffer) =>
Promise.resolve()
Expand Down
Loading