-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
245bf48
commit ee14143
Showing
2 changed files
with
57 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// @ts-nocheck | ||
/* eslint-env node */ | ||
|
||
/** | ||
* Modify the svgo configuration (in place) to prevent it from minifying IDs in SVGs. | ||
* This is necessary because it doesn't account for the global ID namespace, and causes | ||
* ID collisions between the SVGs loaded into the same page. | ||
* | ||
* Refs: | ||
* - https://github.com/facebook/docusaurus/issues/8297 | ||
* - https://github.com/svg/svgo/issues/1714 | ||
* - https://linear.app/foxglove/issue/FG-7251/logos-are-cut-off-on-mcapdev | ||
* | ||
* @param {webpack.Configuration} config | ||
*/ | ||
module.exports = function modifySvgoConfigInPlace(config) { | ||
const NEW_SVGO_CONFIG = { | ||
plugins: [ | ||
{ | ||
name: "preset-default", | ||
params: { | ||
overrides: { | ||
removeTitle: false, | ||
removeViewBox: false, | ||
cleanupIDs: false, // do not change IDs | ||
}, | ||
}, | ||
}, | ||
], | ||
}; | ||
// try to find the svgo config rule and replace it | ||
let updated = false; | ||
try { | ||
for (const rule of config.module.rules) { | ||
if (rule.test?.toString() === "/\\.svg$/i") { | ||
for (const nestedRule of rule.oneOf) { | ||
for (const loader of nestedRule.use) { | ||
if ( | ||
/* cspell:disable */ | ||
loader.loader === require.resolve("@svgr/webpack") | ||
) { | ||
loader.options.svgoConfig = NEW_SVGO_CONFIG; | ||
updated = true; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} catch (e) { | ||
console.error("Error while attempting to modify svgo config: " + e); | ||
} | ||
if (!updated) { | ||
throw new Error("Failed to update svgo config"); | ||
} | ||
}; |