From b2a50151b9dcd9f79307695e52a2b12db72a0a7e Mon Sep 17 00:00:00 2001 From: RAX7 Date: Mon, 19 Sep 2022 21:17:23 +0700 Subject: [PATCH] fix: fix resize option enabled flag (`squoosh`) (#356) --- src/utils.js | 42 +++++-------- test/loader-generator-option.test.js | 93 ++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 26 deletions(-) diff --git a/src/utils.js b/src/utils.js index 252b96dd..63023e2c 100644 --- a/src/utils.js +++ b/src/utils.js @@ -735,23 +735,18 @@ async function squooshGenerate(original, minifyOptions) { const squooshOptions = /** @type {SquooshOptions} */ (minifyOptions || {}); - /** - * @type {undefined | Object.} - */ - let preprocessors; - - for (const preprocessor of Object.keys(squoosh.preprocessors)) { - if (typeof squooshOptions[preprocessor] !== "undefined") { - if (!preprocessors) { - preprocessors = {}; + const preprocEntries = Object.entries(squooshOptions).filter( + ([key, value]) => { + if (key === "resize" && value?.enabled === false) { + return false; } - preprocessors[preprocessor] = squooshOptions[preprocessor]; + return typeof squoosh.preprocessors[key] !== "undefined"; } - } + ); - if (typeof preprocessors !== "undefined") { - await image.preprocess(preprocessors); + if (preprocEntries.length > 0) { + await image.preprocess(Object.fromEntries(preprocEntries)); } const { encodeOptions } = squooshOptions; @@ -860,23 +855,18 @@ async function squooshMinify(original, options) { const image = imagePool.ingestImage(new Uint8Array(original.data)); const squooshOptions = /** @type {SquooshOptions} */ (options || {}); - /** - * @type {undefined | Object.} - */ - let preprocessors; - - for (const preprocessor of Object.keys(squoosh.preprocessors)) { - if (typeof squooshOptions[preprocessor] !== "undefined") { - if (!preprocessors) { - preprocessors = {}; + const preprocEntries = Object.entries(squooshOptions).filter( + ([key, value]) => { + if (key === "resize" && value?.enabled === false) { + return false; } - preprocessors[preprocessor] = squooshOptions[preprocessor]; + return typeof squoosh.preprocessors[key] !== "undefined"; } - } + ); - if (typeof preprocessors !== "undefined") { - await image.preprocess(preprocessors); + if (preprocEntries.length > 0) { + await image.preprocess(Object.fromEntries(preprocEntries)); } const { encodeOptions = {} } = squooshOptions; diff --git a/test/loader-generator-option.test.js b/test/loader-generator-option.test.js index 2f4148b2..2acc9fa8 100644 --- a/test/loader-generator-option.test.js +++ b/test/loader-generator-option.test.js @@ -240,6 +240,54 @@ describe("loader generator option", () => { expect(errors).toHaveLength(0); }); + it("should generate and not resize (squooshGenerate)", async () => { + const stats = await runWebpack({ + entry: path.join(fixturesPath, "./loader-single.js"), + imageminLoaderOptions: { + generator: [ + { + preset: "webp", + implementation: ImageMinimizerPlugin.squooshGenerate, + options: { + resize: { + enabled: false, + width: 100, + height: 50, + }, + rotate: { + numRotations: 90, + }, + encodeOptions: { + webp: { + lossless: 1, + }, + }, + }, + }, + ], + }, + }); + + const { compilation } = stats; + const { warnings, errors } = compilation; + + const transformedAsset = path.resolve( + __dirname, + compilation.options.output.path, + "./nested/deep/loader-test.webp" + ); + + const transformedExt = await fileType.fromFile(transformedAsset); + const sizeOf = promisify(imageSize); + const dimensions = await sizeOf(transformedAsset); + + expect(dimensions.height).toBe(1); + expect(dimensions.width).toBe(1); + expect(/image\/webp/i.test(transformedExt.mime)).toBe(true); + expect(warnings).toHaveLength(0); + expect(errors).toHaveLength(0); + }); + it("should generate and resize (sharpGenerate)", async () => { const stats = await runWebpack({ entry: path.join(fixturesPath, "./loader-single.js"), @@ -285,6 +333,51 @@ describe("loader generator option", () => { expect(errors).toHaveLength(0); }); + it("should generate and not resize (sharpGenerate)", async () => { + const stats = await runWebpack({ + entry: path.join(fixturesPath, "./loader-single.js"), + imageminLoaderOptions: { + generator: [ + { + preset: "webp", + implementation: ImageMinimizerPlugin.sharpGenerate, + options: { + resize: { + enabled: false, + width: 100, + height: 50, + }, + encodeOptions: { + webp: { + lossless: true, + }, + }, + }, + }, + ], + }, + }); + + const { compilation } = stats; + const { warnings, errors } = compilation; + + const transformedAsset = path.resolve( + __dirname, + compilation.options.output.path, + "./nested/deep/loader-test.webp" + ); + + const transformedExt = await fileType.fromFile(transformedAsset); + const sizeOf = promisify(imageSize); + const dimensions = await sizeOf(transformedAsset); + + expect(dimensions.height).toBe(1); + expect(dimensions.width).toBe(1); + expect(/image\/webp/i.test(transformedExt.mime)).toBe(true); + expect(warnings).toHaveLength(0); + expect(errors).toHaveLength(0); + }); + it("should minify and rename (sharpMinify)", async () => { const stats = await runWebpack({ entry: path.join(fixturesPath, "./simple.js"),