Skip to content

Commit

Permalink
fix: throw an error on unsupported image formats (sharp) (#359)
Browse files Browse the repository at this point in the history
  • Loading branch information
RAX7 authored Sep 19, 2022
1 parent b2a5015 commit c0b193b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,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

0 comments on commit c0b193b

Please sign in to comment.