Skip to content

Commit

Permalink
fix: fix resize option enabled flag (squoosh) (#356)
Browse files Browse the repository at this point in the history
  • Loading branch information
RAX7 authored Sep 19, 2022
1 parent 593d116 commit b2a5015
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 26 deletions.
42 changes: 16 additions & 26 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -735,23 +735,18 @@ async function squooshGenerate(original, minifyOptions) {

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

/**
* @type {undefined | Object.<string, any>}
*/
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;
Expand Down Expand Up @@ -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.<string, any>}
*/
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;
Expand Down
93 changes: 93 additions & 0 deletions test/loader-generator-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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"),
Expand Down

0 comments on commit b2a5015

Please sign in to comment.