Skip to content

Instantly share code, notes, and snippets.

@ellemedit
Created October 26, 2023 04:15
Show Gist options
  • Save ellemedit/9c13d5ec6aa1d0b37ef534115d5a749a to your computer and use it in GitHub Desktop.
Save ellemedit/9c13d5ec6aa1d0b37ef534115d5a749a to your computer and use it in GitHub Desktop.
next.js server component svg fix
const config = {
// ...
webpack(config) {
// prevent ehters.js node deps import warnings
config.externals.push({
bufferutil: "bufferutil",
"utf-8-validate": "utf-8-validate",
});
config.resolve.fallback = {
...config.resolve.fallback,
fs: false,
};
// Grab the existing rule that handles SVG imports
const builtinFileLoaderRule = config.module.rules.find((rule) =>
rule.test?.test?.(".svg")
);
// summary:
// import SomeSVG from "~/any.svg" -> string by url loader
// import SomeSVG from "~/anyIcon.svg" -> React component by svgr
config.module.rules.push(
{
...builtinFileLoaderRule,
test: /\.svg$/i,
exclude: /Icon\.svg$/i,
},
{
test: /Icon\.svg$/i,
// svgr setup ref: https://github.com/gregberge/svgr/issues/860#issuecomment-1531904399
issuer: { not: /\.(css|scss|sass)$/ },
loader: "@svgr/webpack",
options: {
titleProp: true,
icon: true,
replaceAttrValues: {
"#000": "currentColor",
},
},
}
);
// Modify the file loader rule to ignore *.svg, since we have it handled now.
builtinFileLoaderRule.exclude = /\.svg$/i;
return config;
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment