为什么在使用Rollup构建我的组件库时会出现”styled is not a function”的错误?

huangapple go评论62阅读模式
英文:

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);

huangapple
  • 本文由 发表于 2023年7月13日 22:39:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76680630.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定