Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
indirectlylit committed Apr 11, 2024
1 parent 245bf48 commit ee14143
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 56 deletions.
58 changes: 2 additions & 56 deletions website/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,58 +9,7 @@ const util = require("util");
const webpack = require("webpack");
const execAsync = util.promisify(require("child_process").exec);

/**
* 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
*/
function modifySvgoConfig(config) {
const NEW_SVGO_CONFIG = {
plugins: [
{
name: "preset-default",
params: {
overrides: {
removeTitle: false,
removeViewBox: false,
cleanupIDs: false, // do not change IDs
},
},
},
],
};
// find the svgo config rule and replace it
if (config.module?.rules instanceof Array) {
for (const rule of config.module.rules) {
if (typeof rule === "object" && rule.test?.toString() === "/\\.svg$/i") {
if (rule.oneOf instanceof Array) {
for (const nestedRule of rule.oneOf) {
if (nestedRule.use instanceof Array) {
for (const loader of nestedRule.use) {
if (
typeof loader === "object" &&
/* cspell:disable */
loader.loader === require.resolve("@svgr/webpack")
) {
if (typeof loader.options === "object") {
loader.options.svgoConfig = NEW_SVGO_CONFIG;
}
}
}
}
}
}
}
}
}
}
const modifySvgoConfigInPlace = require("./modifySvgoConfigInPlace");

/** @type {import('@docusaurus/types').Config} */
const config = {
Expand All @@ -87,10 +36,7 @@ const config = {
(_context, _options) => ({
name: "MCAP website custom webpack config",
configureWebpack(config, _isServer, _utils, _content) {
// Update config.module.rules directly.
// (Unclear if this is possible with a mergeStrategy below.)
modifySvgoConfig(config);

modifySvgoConfigInPlace(config);
return {
mergeStrategy: {
"resolve.extensions": "replace",
Expand Down
55 changes: 55 additions & 0 deletions website/modifySvgoConfigInPlace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// @ts-nocheck

Check failure on line 1 in website/modifySvgoConfigInPlace.js

View workflow job for this annotation

GitHub Actions / spellcheck

Unknown word (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");
}
};

0 comments on commit ee14143

Please sign in to comment.