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: better capability sharp and squoosh implementations #359

Merged
merged 1 commit into from
Sep 19, 2022
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
8 changes: 8 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,14 @@ async function sharpTransform(
const inputExt = path.extname(original.filename).slice(1).toLowerCase();

if (!SHARP_FORMATS.has(inputExt)) {
if (targetFormat) {
const error = new Error(
`Error with '${original.filename}': Input file has an unsupported format`
);

original.errors.push(error);
}

return null;
}

Expand Down
32 changes: 31 additions & 1 deletion test/ImageminPlugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1515,7 +1515,7 @@ describe("imagemin plugin", () => {
expect(/image\/png/i.test(ext.mime)).toBe(true);
});

it("should throw an error on unknown format", async () => {
it("should throw an error on unknown format (squooshGenerate)", async () => {
const stats = await runWebpack({
entry: path.join(fixturesPath, "generator-and-minimizer-5.js"),
imageminPluginOptions: {
Expand Down Expand Up @@ -1545,6 +1545,36 @@ describe("imagemin plugin", () => {
);
});

it("should throw an error on unknown format (sharpGenerate)", async () => {
const stats = await runWebpack({
entry: path.join(fixturesPath, "generator-and-minimizer-5.js"),
imageminPluginOptions: {
test: /\.(jpe?g|gif|json|svg|png|webp|txt)$/i,
generator: [
{
preset: "avif",
implementation: ImageMinimizerPlugin.sharpGenerate,
options: {
encodeOptions: {
webp: {
lossless: true,
},
},
},
},
],
},
});
const { compilation } = stats;
const { warnings, errors } = compilation;

expect(warnings).toHaveLength(0);
expect(errors).toHaveLength(1);
expect(errors[0].message).toMatch(
/Error with 'loader-test.txt': Input file has an unsupported format/g
);
});

it("should minimize and ignore unsupported data URI", async () => {
let minimized = 0;

Expand Down