diff --git a/src/utils.js b/src/utils.js index 8825d903..e5e28bce 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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 ====== diff --git a/test/ImageminPlugin.test.js b/test/ImageminPlugin.test.js index 27a20159..ad21c25e 100644 --- a/test/ImageminPlugin.test.js +++ b/test/ImageminPlugin.test.js @@ -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({ diff --git a/test/fixtures/animation-test.gif b/test/fixtures/animation-test.gif new file mode 100644 index 00000000..7c05ee1e Binary files /dev/null and b/test/fixtures/animation-test.gif differ diff --git a/test/fixtures/generator-and-minimizer-animation.js b/test/fixtures/generator-and-minimizer-animation.js new file mode 100644 index 00000000..0ac72c70 --- /dev/null +++ b/test/fixtures/generator-and-minimizer-animation.js @@ -0,0 +1,2 @@ +require("./animation-test.gif"); +require("./animation-test.gif?as=webp");