Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: pass the original path to svgo #441

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
Loading