Skip to content

Commit

Permalink
fix: svgo plugin to use collision resistant IDs (#3334)
Browse files Browse the repository at this point in the history
* fix(svgo/r config): add prefixIds plugin to svgoConfig in webpack svgr-loader and vite-plugin-svgr in order to insure that there are no id collisions between svg files

* chore: add changeset

---------

Co-authored-by: Byron Wall <[email protected]>
Co-authored-by: Tobias Deekens <[email protected]>
  • Loading branch information
3 people authored Dec 7, 2023
1 parent 7e62ee1 commit 225c110
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .changeset/angry-apes-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@commercetools-frontend/mc-scripts": patch
---

Avoid collisions of SVG IDs in webpack's SVGR loader and Vite SVGR plugin.

More information about the approach can be found [here](https://github.com/svg/svgo/issues/1746#issuecomment-1803600573).
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import momentLocalesToKeep from /* preval */ './moment-locales';
import paths from './paths';
import vendorsToTranspile from './vendors-to-transpile';

let svgoPrefixIdsCount = 0;

const defaultToggleFlags: TWebpackConfigToggleFlagsForDevelopment = {
generateIndexHtml: true,
disableCoreJs: false,
Expand Down Expand Up @@ -250,6 +252,21 @@ function createWebpackConfigForDevelopment(
},
},
},
// Avoid collisions with ids in other SVGs,
// which was causing incorrect gradient directions
// https://github.com/svg/svgo/issues/1746#issuecomment-1803600573
//
// Previously, this was a problem where unique ids
// could not be generated since svgo@3
// https://github.com/svg/svgo/issues/674
// https://github.com/svg/svgo/issues/1746
{
name: 'prefixIds',
params: {
delim: '',
prefix: () => svgoPrefixIdsCount++,
},
},
],
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import momentLocalesToKeep from /* preval */ './moment-locales';
import paths from './paths';
import vendorsToTranspile from './vendors-to-transpile';

let svgoPrefixIdsCount = 0;

const defaultToggleFlags: TWebpackConfigToggleFlagsForProduction = {
// Allow to disable CSS extraction in case it's not necessary (e.g. for Storybook)
enableExtractCss: true,
Expand Down Expand Up @@ -270,6 +272,21 @@ function createWebpackConfigForProduction(
},
},
},
// Avoid collisions with ids in other SVGs,
// which was causing incorrect gradient directions
// https://github.com/svg/svgo/issues/1746#issuecomment-1803600573
//
// Previously, this was a problem where unique ids
// could not be generated since svgo@3
// https://github.com/svg/svgo/issues/674
// https://github.com/svg/svgo/issues/1746
{
name: 'prefixIds',
params: {
delim: '',
prefix: () => svgoPrefixIdsCount++,
},
},
],
},
},
Expand Down
17 changes: 17 additions & 0 deletions packages/mc-scripts/src/vite-plugins/vite-plugin-svgr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import fs from 'fs';
import { createFilter } from '@rollup/pluginutils';
import { transformWithEsbuild, type Plugin } from 'vite';

let svgoPrefixIdsCount = 0;

function vitePluginSvgr(): Plugin {
const filter = createFilter('**/*.react.svg');
return {
Expand All @@ -29,6 +31,21 @@ function vitePluginSvgr(): Plugin {
},
},
},
// Avoid collisions with ids in other SVGs,
// which was causing incorrect gradient directions
// https://github.com/svg/svgo/issues/1746#issuecomment-1803600573
//
// Previously, this was a problem where unique ids
// could not be generated since svgo@3
// https://github.com/svg/svgo/issues/674
// https://github.com/svg/svgo/issues/1746
{
name: 'prefixIds',
params: {
delim: '',
prefix: () => svgoPrefixIdsCount++ as unknown as string,
},
},
],
},
},
Expand Down

2 comments on commit 225c110

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for merchant-center-application-kit ready!

✅ Preview
https://merchant-center-application-6t1r8f8he-commercetools.vercel.app

Built with commit 225c110.
This pull request is being automatically deployed with vercel-action

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for application-kit-custom-views ready!

✅ Preview
https://application-kit-custom-views-1c3ua3jqq-commercetools.vercel.app

Built with commit 225c110.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.