Skip to content

Commit

Permalink
fix: support for generating images from svg (sharp)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Mar 10, 2023
1 parent e2bcc5f commit 5076734
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lint-staged.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

module.exports = {
"*": ["prettier --write --ignore-unknown", "cspell"],
"*": ["prettier --write --ignore-unknown", "cspell --no-must-find-files"],
"*.{js}": ["eslint --cache --fix"],
};
27 changes: 25 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,26 @@ squooshMinify.teardown = squooshImagePoolTeardown;
*/

// https://github.com/lovell/sharp/blob/e40a881ab4a5e7b0e37ba17e31b3b186aef8cbf6/lib/output.js#L7-L23
const SHARP_FORMATS = new Map([
const SHARP_GENERATE_FORMATS = new Map([
["avif", "avif"],
["gif", "gif"],
["heic", "heif"],
["heif", "heif"],
["j2c", "jp2"],
["j2k", "jp2"],
["jp2", "jp2"],
["jpeg", "jpeg"],
["jpg", "jpeg"],
["jpx", "jp2"],
["png", "png"],
["raw", "raw"],
["tif", "tiff"],
["tiff", "tiff"],
["webp", "webp"],
["svg", "svg"],
]);

const SHARP_MINIFY_FORMATS = new Map([
["avif", "avif"],
["gif", "gif"],
["heic", "heif"],
Expand Down Expand Up @@ -1011,7 +1030,11 @@ async function sharpTransform(
) {
const inputExt = path.extname(original.filename).slice(1).toLowerCase();

if (!SHARP_FORMATS.has(inputExt)) {
if (
!targetFormat
? !SHARP_MINIFY_FORMATS.has(inputExt)
: !SHARP_GENERATE_FORMATS.has(inputExt)
) {
if (targetFormat) {
const error = new Error(
`Error with '${original.filename}': Input file has an unsupported format`
Expand Down
36 changes: 36 additions & 0 deletions test/ImageminPlugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2556,4 +2556,40 @@ describe("imagemin plugin", () => {

expect(cssContent).toMatchSnapshot("main.css");
});

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

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

const file = path.resolve(
__dirname,
compilation.options.output.path,
"./loader-test.webp"
);
const ext = await fileType.fromFile(file);

expect(/image\/webp/i.test(ext.mime)).toBe(true);
});
});
1 change: 1 addition & 0 deletions test/fixtures/generator-and-minimizer-7.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require("./loader-test.svg?as=webp");
Empty file.
2 changes: 2 additions & 0 deletions test/fixtures/unknown-and-jpg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
console.log(new URL("../fixtures/loader-test.unknown", import.meta.url));
console.log(new URL("../fixtures/loader-test.jpg", import.meta.url));
4 changes: 2 additions & 2 deletions test/plugin-minimizer-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ describe("plugin minify option", () => {

it("should work with 'sharpMinify' minifier and don't rename unsupported asset", async () => {
const stats = await runWebpack({
entry: path.join(fixturesPath, "./svg-and-jpg.js"),
entry: path.join(fixturesPath, "./unknown-and-jpg.js"),
fileLoaderOff: true,
imageminPluginOptions: {
minimizer: {
Expand All @@ -666,7 +666,7 @@ describe("plugin minify option", () => {
const { warnings, errors, assets } = compilation;

const originalAsset = Object.keys(assets).filter((asset) =>
asset.includes("loader-test.svg")
asset.includes("loader-test.unknown")
);
const minifiedAsset = Object.keys(assets).filter((asset) =>
asset.includes("minified-xxx-loader-test-yyy.jpg")
Expand Down

0 comments on commit 5076734

Please sign in to comment.