我创建了一个带有两个npm工作区的monorepo,交付的代码似乎是正确的,但仍然失败。
设置就像
- package
- JSX components to export
- vite config
- package.json
- react
- package.json
- ...
- next (with react)
- ...
- astro (with react)
- ...
- ...
我在react、next和astro中使用了react组件包,并使用了接近默认的npm工作区设置
在react/next/astro中
"workspaces": [
"../package"
],
"dependencies": [
"package" : 1.0.1
...
"react": "^18.2.0",
"react-dom": "^18.2.0"
]
级封装
{
"name": "package",
"version": "1.0.1",
"description": "",
"type": "module",
"main": "./dist/components.bundle.min.js",
"module": "./dist/components.bundle.min.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "node --max_old_space_size=16384 ./node_modules/vite/bin/vite.js build"
},
"exports": {
".": {
"import": "./dist/components.bundle.min.js"
}
},
"author": "",
"license": "ISC",
"devDependencies": {
"@vitejs/plugin-react": "^4.0.3",
"terser": "^5.19.0"
},
"dependencies": {
"@babel/plugin-transform-runtime": "^7.22.9",
"@rollup/plugin-babel": "^6.0.3",
"@vitejs/plugin-legacy": "^4.1.0",
"rollup-plugin-node-externals": "^6.1.1",
"vite": "^4.4.4"
},
"peerDependencies": {
"react": "18.2.0",
"react-dom": "18.2.0"
},
"browserslist": [
"> 0.5%",
"not dead"
]
}
正如你所看到的,我正在使用vite作为bundler,我猜问题在于我的vite配置:
// vite.config.js
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import { babel } from "@rollup/plugin-babel";
export default defineConfig({
plugins: [
react(),
babel({
babelHelpers: "runtime",
extensions: [".js", ".jsx", ".es6", ".es", ".mjs", "ts"],
}),
],
esbuild: {
jsxFactory: "React.createElement",
jsxFragment: "React.Fragment",
},
// optimizeDeps: {
// include: ["./Components/**/*.jsx"],
// },
build: {
minify: false,
lib: {
// Could also be a dictionary or array of multiple entry points
entry: "./components/TestComponent.jsx",
name: "jsxvideotest",
fileName: "components.bundle.min",
},
rollupOptions: {
// into your library
external: ["react"],
output: {
globals: {
react: "React",
},
},
},
},
resolve: {
dedupe: ["react", "react-dom"],
}
});
.我试着用npm run build
运行vite build
。这可以工作,也可以将组件导入到react/next/astro,但是在react和next中,编译失败,错误消息为Uncaught TypeError: dispatcher is null
。在太空中,这是不会发生的。另一个有趣的效果是,如果我使用Chrome而不是firefox,错误消息将更改为Uncaught TypeError: Cannot read properties of null (reading 'useState')
。
此外,还有那些典型的错误Warning: Invalid hook call. Hooks can only be called inside of the body of a function component. ...
你们知道为什么会这样吗我可以提供更多关于monorepo的信息,如果你需要他们,设置是相当大的,所以我缩短了它。
1条答案
按热度按时间mwkjh3gx1#
对我来说,我必须确保我的组件是客户端的,只使用
client:only="react"
,或者你的框架的任何东西。在此阅读更多信息:https://docs.astro.build/en/core-concepts/framework-components/#hydrating-interactive-components