我正在使用rollup.js创建一个react库,但是当我运行npm run build时,我会得到一个错误,就好像试图从null检索useState钩子一样。
Uncaught TypeError: Cannot read properties of null (reading 'useState')
at Object.useState (react.development.js:1617:1)
at ReactPokableLoving (index.esm.js:20:1)
at renderWithHooks (react-dom.development.js:14985:1)
at mountIndeterminateComponent (react-dom.development.js:17811:1)
at beginWork (react-dom.development.js:19049:1)
at HTMLUnknownElement.callCallback (react-dom.development.js:3945:1)
at Object.invokeGuardedCallbackDev (react-dom.development.js:3994:1)
at invokeGuardedCallback (react-dom.development.js:4056:1)
at beginWork$1 (react-dom.development.js:23964:1)
at performUnitOfWork (react-dom.development.js:22776:1)我的rollup.config.js
import { babel } from "@rollup/plugin-babel";
const config = {
input: "src/lib/index.js",
output: {
file: "dist/index.esm.js",
format: "esm",
},
external: [/@babel\/runtime/, "react", "react-dom"],
plugins: [
babel({
babelHelpers: "runtime",
plugins: ["@babel/plugin-transform-runtime"],
}),
],
};
export default config;我的.babelrc
{
"presets" : [["@babel/preset-env", {"targets" : "defaults"}],[
"@babel/preset-react",
{
"runtime": "automatic"
}
]]
}还有我的package.json
{
"name": "xxxxx",
"version": "1.0.7",
"author": "ndotie",
"keywords": [
"react",
"components",
"ui",
"pagination"
],
"module": "dist/index.esm.js",
"repository": {
"type": "git",
"url": "https://github.com/xxx/xxxx.git"
},
"files": [
"dist",
"README.md"
],
"private": false,
"dependencies": {
"@babel/polyfill": "^7.12.1",
"@babel/runtime": "^7.17.9",
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^12.1.4",
"@testing-library/user-event": "^13.5.0",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"react-scripts": "5.0.0",
"rollup": "^2.70.1",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"test": "react-scripts test",
"eject": "react-scripts eject",
"prebuild": "rimraf dist",
"build": "rollup -c"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"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/cli": "^7.17.6",
"@babel/core": "^7.17.9",
"@babel/plugin-transform-runtime": "^7.17.0",
"@babel/preset-env": "^7.16.11",
"@babel/preset-react": "^7.16.7",
"@rollup/plugin-babel": "^5.3.1",
"cross-env": "^7.0.3"
}
}发布于 2022-06-13 13:53:05
我也面临同样的问题,并成功地解决了这一问题,您在rollup.config文件中缺少了一些配置:
external: ["react", "react-dom"]我将附上我的完整配置,以创建一个npm包与汇总,反应(18)和类型记录。
rollup.config.js文件:
//This plugin prevents packages listed in peerDependencies from being bundled with our component library
import peerDepsExternal from "rollup-plugin-peer-deps-external";
//efficiently bundles third party dependencies we've installed and use in node_modules
import resolve from "@rollup/plugin-node-resolve";
// //enables transpilation into CommonJS (CJS) format
import commonjs from "@rollup/plugin-commonjs";
//transpiled our TypeScript code into JavaScript. This plugin will use all the settings we have set in tsconfig.json.
//We set "useTsconfigDeclarationDir": true so that it outputs the .d.ts files in the directory specified by in tsconfig.json
import typescript from "rollup-plugin-typescript2";
// transforms our Sass into CSS. In order to get this plugin working with Sass, we've installed sass
import postcss from "rollup-plugin-postcss";
const packageJson = require("./package.json");
export default {
input: "src/index.tsx",
output: [
{
file: packageJson.module,
format: "esm", // import '' from '...
sourcemap: true,
},
],
plugins: [
peerDepsExternal(),
resolve(),
commonjs(),
typescript({ useTsconfigDeclarationDir: true }),
postcss(),
],
external: ["react", "react-dom"],
};ts.config文件:
{
"compilerOptions": {
"target": "es5",
"outDir": "build",
"lib": ["dom", "dom.iterable", "esnext"],
"declaration": true,
"declarationDir": "build",
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": false,
"noEmit": true,
"jsx": "react-jsx",
"baseUrl": "./src"
},
"include": ["src"],
"exclude": ["node_modules", "build"]
}package.json文件:
{
"name": "test",
"version": "1.0.0",
"main": "build/index.js",
"module": "build/index.js",
"scripts": {
"build": "rollup -c",
"build-watch": "rollup -c -w"
},
"license": "MIT",
"devDependencies": {
"@rollup/plugin-commonjs": "^22.0.0",
"@rollup/plugin-node-resolve": "^13.3.0",
"@types/react": "^18.0.12",
"@types/react-dom": "^18.0.5",
"rollup": "^2.75.6",
"rollup-plugin-peer-deps-external": "^2.2.4",
"rollup-plugin-postcss": "^4.0.2",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-typescript2": "^0.31.2",
"sass": "^1.52.1",
"typescript": "^4.6.4",
"react": "^18.1.0",
"react-dom": "^18.1.0",
}
}注意到:如果您正在使用npm link和react应用程序测试库,您应该从包中删除react和react-dom依赖项,而不是将库链接到应用程序中的react和react-dom,这样做是为了避免在测试库时出现read的双重副本(请阅读更多的这里 )。
https://stackoverflow.com/questions/71848226
复制相似问题