【发布时间】:2020-06-26 16:56:49
【问题描述】:
不能将 SVG 用作 NextJS 应用程序和 Storybook 的模块,使用绝对路径导入。通过许多尝试的设置,我可以在 Next 或 Storybook 中导入 SVG,但不能同时在两者中导入。我将此设置用于babel-plugin-inline-react-svg:
// .babelrc
{
...
"plugins": [
...
"inline-react-svg"
]
}
使用这个插件 Storybook 不需要任何配置,这个示例代码可以按预期工作:
import Wrapper from '../../../components/Wrapper';
import IconSVG from '../../../icons/sandwich.svg';
export const WrappedSVG = () => (
<Wrapper>
<IconSVG />
</Wrapper>
);
但以下不是:
import Wrapper from 'components/Wrapper';
import IconSVG from 'icons/sandwich.svg';
export const WrappedSVG = () => (
<Wrapper>
<IconSVG />
</Wrapper>
);
正在处理包装器,但图标未处理:Cannot find module
这里是svgr 设置:
// 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;
},
}
此配置在应用程序端运行良好,但在 Storybook 中我得到 DOMException: "String contains an invalid character"
我的npm run dev 脚本是这样的:ts-node --project tsconfig.server.json src/server.ts(通过nodemon)。
我希望有人能给我一个提示,如何让 SVG 组件的绝对导入同时适用于 NextJS 和 Storybook。
【问题讨论】:
标签: typescript next.js storybook