Kos*_*tya 6 javascript bundle reactjs webpack react-hooks
每次新尝试我都会得到 2 个案例:
“无效的钩子调用” - 由于双重 React 环境。
“无法解析“react”” - 如果我将其从挂钩库中的 devDependencies 中删除。
目标是:建立正确的钩子库构建环境。
webpack.config.js
const path = require("path")
module.exports = {
mode: "production",
entry: "./src/index.ts",
output: {
filename: "index.js",
path: path.resolve(__dirname, 'dist'),
clean: true,
library: "custom-hook-lib",
libraryTarget: "umd"
},
externals: {
'react': 'react',
'react-dom' : 'reactDOM'
},
resolve: {
extensions: [".js", ".jsx", ".json", ".ts", ".tsx"]
},
module: {
rules: [
{
test: /\.(j|t)sx?$/,
exclude: /(node_modules)/,
use: 'ts-loader'
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
包.json
{
"name": "custom-hook-lib",
"version": "1.0.0",
"description": "",
"main": "dist/index.js",
"module": "dist/index.js",
"files": [
"dist"
],
"keywords": [],
"license": "ISC",
"peerDependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"@types/react": "^18.0.17",
"typescript": "^4.8.2",
"webpack": "^5.74.0",
"webpack-cli": "^4.10.0",
"ts-loader": "^9.3.1"
}
}
Run Code Online (Sandbox Code Playgroud)