Can't use SVG as modules for both NextJS app and Storybook, imported with absolute paths. With many tried setups I was able to import SVG either in Next or in Storybook, but not both. I used this setup for babel-plugin-inline-react-svg
:
// .babelrc
{
...
"plugins": [
...
"inline-react-svg"
]
}
With this plugin Storybook doesn't require any configuration and this example code works as expected:
import Wrapper from '../../../components/Wrapper';
import IconSVG from '../../../icons/sandwich.svg';
export const WrappedSVG = () => (
<Wrapper>
<IconSVG />
</Wrapper>
);
But the following does not:
import Wrapper from 'components/Wrapper';
import IconSVG from 'icons/sandwich.svg';
export const WrappedSVG = () => (
<Wrapper>
<IconSVG />
</Wrapper>
);
Wrapper is being processed, but not the icon: Cannot find module
Here is svgr
setup:
// next.config.js
module.exports = {
webpack(config, options) {
...
config.module.rules.push({
test: /\.svg$/,
use: ['@svgr/webpack'],
});
return config;
},
};
// .storybook/main.js
module.exports = {
...
webpackFinal: async config => {
...
config.module.rules.unshift({
test: /\.svg$/,
use: ['@svgr/webpack'],
});
return config;
},
}
This configuration works fine on the app side, but in Storybook I get DOMException: "String contains an invalid character"
My npm run dev
script is this: ts-node --project tsconfig.server.json src/server.ts
(via nodemon
).
I hope somebody would give me a hint how to make absolute import of SVG components work for both NextJS and Storybook.