Skip to content

Commit

Permalink
fix: support for animated images (sharp) (#358)
Browse files Browse the repository at this point in the history
  • Loading branch information
RAX7 authored Sep 19, 2022
1 parent c0b193b commit 3c30355
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,7 @@ async function sharpTransform(
/** @type {SharpLib} */
// eslint-disable-next-line node/no-unpublished-require
const sharp = require("sharp");
const imagePipeline = sharp(original.data);
const imagePipeline = sharp(original.data, { animated: true });

// ====== rotate ======

Expand Down
46 changes: 46 additions & 0 deletions test/ImageminPlugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,52 @@ describe("imagemin plugin", () => {
);
});

it("should optimizes and generate animated images (sharpGenerate)", async () => {
const stats = await runWebpack({
entry: path.join(fixturesPath, "generator-and-minimizer-animation.js"),
imageminPluginOptions: {
test: /\.(jpe?g|png|webp|gif)$/i,
generator: [
{
preset: "webp",
implementation: ImageMinimizerPlugin.sharpGenerate,
options: {
encodeOptions: {
webp: {
quality: 40,
},
},
},
},
],
minimizer: {
implementation: ImageMinimizerPlugin.sharpMinify,
options: {
encodeOptions: {
gif: {
colors: 8,
},
},
},
},
},
});
const { compilation } = stats;
const { warnings, errors } = compilation;

expect(warnings).toHaveLength(0);
expect(errors).toHaveLength(0);

const webpAsset = compilation.getAsset("animation-test.webp");
const gifAsset = compilation.getAsset("animation-test.gif");

expect(webpAsset.info.size).toBeGreaterThan(9_000);
expect(webpAsset.info.size).toBeLessThan(60_000);

expect(gifAsset.info.size).toBeGreaterThan(18_000);
expect(gifAsset.info.size).toBeLessThan(200_000);
});

it("should throw an error on empty minimizer", async () => {
await expect(async () => {
await runWebpack({
Expand Down
Binary file added test/fixtures/animation-test.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions test/fixtures/generator-and-minimizer-animation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require("./animation-test.gif");
require("./animation-test.gif?as=webp");

0 comments on commit 3c30355

Please sign in to comment.