我尝试用React.js创建组件库,我使用Rollup.js。当我构建应用程序时,我在控制台中看到这个错误。似乎Rollup无法检测默认导出,我尝试添加一个.babelrc文件配置,但不幸的是它不起作用。我做错了什么?如何解决这个问题?
//rollup.config.js
import babel from 'rollup-plugin-babel';
import resolve from '@rollup/plugin-node-resolve';
import external from 'rollup-plugin-peer-deps-external';
import { terser } from 'rollup-plugin-terser';
import postcss from 'rollup-plugin-postcss';
import commonjs from '@rollup/plugin-commonjs';
export default [
{
input: './src/index.js',
output: [
{
file: 'dist/index.js',
format: 'cjs',
},
{
file: 'dist/index.es.js',
format: 'es',
exports: 'named',
}
],
plugins: [
postcss({
plugins: [],
minimize: true,
}),
babel({
exclude: "node_modules/**",
presets: ["@babel/preset-react"],
}),
external(),
resolve(),
terser(),
]
}
];
//package.json
{
"name": "test",
"version": "0.1.0",
"private": false,
"dependencies": {
"@babel/preset-stage-1": "^7.8.3",
"@rollup/plugin-commonjs": "^21.0.3",
"@rollup/plugin-replace": "^2.4.2",
"@testing-library/jest-dom": "^5.16.3",
"@testing-library/react": "^12.1.4",
"@testing-library/user-event": "^13.5.0",
"babel-preset-latest": "^6.24.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "5.0.0",
"react-transform-hmr": "^1.0.4",
"typescript": "^4.6.3",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "tsc",
"test": "react-scripts test",
"eject": "react-scripts eject",
"storybook": "start-storybook -p 6006 -s public",
"build-storybook": "build-storybook -s public",
"build-lib": "rollup -c"
},
"files": [
"dist"
],
"main": "dist/index.js",
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
],
"overrides": [
{
"files": [
"**/*.stories.*"
],
"rules": {
"import/no-anonymous-default-export": "off"
}
}
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@babel/plugin-external-helpers": "^7.16.7",
"@babel/preset-react": "^7.16.7",
"@rollup/plugin-node-resolve": "^11.2.1",
"@storybook/addon-actions": "^6.4.19",
"@storybook/addon-essentials": "^6.4.19",
"@storybook/addon-interactions": "^6.4.19",
"@storybook/addon-links": "^6.4.19",
"@storybook/builder-webpack5": "^6.4.19",
"@storybook/manager-webpack5": "^6.4.19",
"@storybook/node-logger": "^6.4.19",
"@storybook/preset-create-react-app": "^4.1.0",
"@storybook/react": "^6.4.19",
"@storybook/testing-library": "^0.0.9",
"rollup": "^2.70.1",
"rollup-plugin-babel": "^4.4.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-peer-deps-external": "^2.2.4",
"rollup-plugin-postcss": "^4.0.2",
"semantic-release": "^19.0.2",
"webpack": "^5.70.0"
}
}
1条答案
按热度按时间qnyhuwrf1#
错误在于告诉你你需要知道的东西。为了解释这意味着什么,react-dom中没有
export default ...
(你可以通过查看the source code来验证这一点)。有几种方法可以解决此问题:
1.尝试一些
@rollup/plugin-commonjs
设置。抱歉,我不能告诉你哪一个会工作,也许从defaultIsModuleExports
开始。1.导入你需要的方法。对于react-dom,你可能只使用
render
,所以这样导入它:import { render } from 'react-dom'
1.导入所有内容并将别名作为ReactDOM:
import * as ReactDOM from 'react-dom'