英文:
Why am I getting styled is not a function when building my component library with Rollup?
问题
我有一个使用React、styled-components、framer-motion、Rollup和Storybook的内部组件库。我有一个使用该库的NextJS网站,但当我尝试使用它时,出现以下错误:
TypeError: styled is not a function
下面是错误指向的构建库中的代码。
var React = require('react');
var styled = require('styled-components');
var framerMotion = require('framer-motion');
const ButtonElm = styled(framerMotion.motion.button)`
padding: ${props => props.padding};
border-radius: ${props => props.theme.radii.default};
font-size: ${props => props.fontSize};
${props => (props.bold ? "font-weight: bold" : null)};
text-align: center;
cursor: pointer;
width: auto;
display: inline-block;
text-decoration: none;
`;
我不确定如何处理这个错误,尽管我已经尝试了一些更改构建配置的方法,但迄今为止都没有成功。我不确定到底是什么导致了这个问题。
提前感谢您的帮助。
英文:
I have an internal component library that is using React, styled-components, framer-motion, Rollup and Storybook. I have a NextJS website that consumes the library and when I try to use it I am getting the following error:
TypeError: styled is not a function
Bellow is the code from the built library which the error points to.
var React = require('react');
var styled = require('styled-components');
var framerMotion = require('framer-motion');
const ButtonElm = styled(framerMotion.motion.button)`
padding: ${props => props.padding};
border-radius: ${props => props.theme.radii.default};
font-size: ${props => props.fontSize};
${props => (props.bold ? "font-weight: bold" : null)};
text-align: center;
cursor: pointer;
width: auto;
display: inline-block;
text-decoration: none;
`;
I am not sure how to address this bug at all and while I have tried some ways of changing the build configuration, nothing has worked so far. I am not sure what exactly is causing the issue.
Thank you for your help in advance.
答案1
得分: 0
我通过查看RollupJS存储库上的GitHub问题(此问题)成功解决了此错误。
我通过在rollup.config.mjs配置文件以及更具体的CommonJS输出文件中添加了interop: "auto"
属性来实现的。最终看起来像这样:
output: [
{
file: packageJson.main,
format: "cjs",
sourcemap: true,
interop: "auto",
},
...
],
这会改变构建文件,如下所示:
var React = require('react');
var styled = require('styled-components');
var styled__default = _interopDefault(styled);
const ButtonElm = styled__default.default(framerMotion.motion.button)`
padding: ${props => props.padding};
border-radius: ${props => props.theme.radii.default};
font-size: ${props => props.fontSize};
${props => (props.bold ? "font-weight: bold" : null)};
text-align: center;
cursor: pointer;
width: auto;
display: inline-block;
text-decoration: none;
`;
这最终解决了错误。我认为问题与styled-components的默认导入没有正确处理有关,导致了错误。当运行console.log(typeof styled)
时,我得到了一个带有默认属性的对象。
英文:
I managed to solve the bug with the help of a GitHub issue on the RollupJS repository (this one).
I did it by adding a property of interop: "auto"
to the rollup.config.mjs configuration file and more specifically the CommonJS output file. It ended up looking something like this:
output: [
{
file: packageJson.main,
format: "cjs",
sourcemap: true,
interop: "auto",
},
...
],
This changed the build file in the following way:
var React = require('react');
var styled = require('styled-components');
var styled__default = _interopDefault(styled);
const ButtonElm = styled__default.default(framerMotion.motion.button)`
padding: ${props => props.padding};
border-radius: ${props => props.theme.radii.default};
font-size: ${props => props.fontSize};
${props => (props.bold ? "font-weight: bold" : null)};
text-align: center;
cursor: pointer;
width: auto;
display: inline-block;
text-decoration: none;
`;
which ended up fixing the error. I believe it had to do with the default import of styled-components not being handled properly, resulting in an error. I was getting an object with a default property when running console.log(typeof styled);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论