Skip to content

Commit

Permalink
fix: pass the original image path to svgo (#441)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored May 29, 2024
1 parent 0d660ad commit d5522a6
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1234,7 +1234,10 @@ async function svgoMinify(original, minimizerOptions) {
let result;

try {
result = optimize(original.data.toString(), encodeOptions);
result = optimize(original.data.toString(), {
path: original.filename,
...encodeOptions,
});
} catch (error) {
const originalError =
error instanceof Error ? error : new Error(/** @type {string} */ (error));
Expand Down
33 changes: 33 additions & 0 deletions test/ImageminPlugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2605,4 +2605,37 @@ describe("imagemin plugin", () => {

expect(/image\/webp/i.test(ext.mime)).toBe(true);
});

it("should optimizes images svg image and prefix id (svgoMinify)", async () => {
const stats = await runWebpack({
name: "minified-[name].[ext]",
assetResource: true,
entry: path.join(fixturesPath, "svgo-prefix-id.js"),
imageminPluginOptions: {
test: /\.(jpe?g|png|webp|svg)$/i,
minimizer: {
implementation: ImageMinimizerPlugin.svgoMinify,
options: {
encodeOptions: {
plugins: ["prefixIds"],
},
},
},
},
});
const { compilation } = stats;
const { warnings, errors } = compilation;

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

const file = path.resolve(
__dirname,
compilation.options.output.path,
"./minified-svgo-id.svg",
);
const content = await fs.promises.readFile(file, "utf-8");

expect(content).toContain("svgo-id_svg__test");
});
});
4 changes: 4 additions & 0 deletions test/fixtures/svgo-id.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions test/fixtures/svgo-prefix-id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(new URL("../fixtures/svgo-id.svg", import.meta.url));

0 comments on commit d5522a6

Please sign in to comment.