From 1c1dfe8258104421df2c499c039422c271d253d6 Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Wed, 29 May 2024 19:46:43 +0300 Subject: [PATCH] fix: pass the original path to svgo --- src/utils.js | 5 ++++- test/ImageminPlugin.test.js | 33 +++++++++++++++++++++++++++++++++ test/fixtures/svgo-id.svg | 4 ++++ test/fixtures/svgo-prefix-id.js | 1 + 4 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/svgo-id.svg create mode 100644 test/fixtures/svgo-prefix-id.js diff --git a/src/utils.js b/src/utils.js index c5adee6..2c7cf97 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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)); diff --git a/test/ImageminPlugin.test.js b/test/ImageminPlugin.test.js index 2aae58e..00293e3 100644 --- a/test/ImageminPlugin.test.js +++ b/test/ImageminPlugin.test.js @@ -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"); + }); }); diff --git a/test/fixtures/svgo-id.svg b/test/fixtures/svgo-id.svg new file mode 100644 index 0000000..fbb5e5e --- /dev/null +++ b/test/fixtures/svgo-id.svg @@ -0,0 +1,4 @@ + + + + diff --git a/test/fixtures/svgo-prefix-id.js b/test/fixtures/svgo-prefix-id.js new file mode 100644 index 0000000..1324da7 --- /dev/null +++ b/test/fixtures/svgo-prefix-id.js @@ -0,0 +1 @@ +console.log(new URL("../fixtures/svgo-id.svg", import.meta.url));