1 回答
TA贡献1890条经验 获得超9个赞
查看 webpack 配置,我可以看到,UI 工具包与react包含的内容捆绑在一起,这可能会导致问题。
为了避免这种情况,您可以使用 webpack externals。
https://webpack.js.org/configuration/externals/
externals 配置选项提供了一种从输出包中排除依赖项的方法。相反,创建的包依赖于存在于消费者环境中的依赖关系。此功能通常对库开发人员最有用,但是它有多种应用程序。
因此,您可以将 UI Kit webpack 配置更新为不包含react,并且 peerDependencies 应该负责库的任何使用者的依赖项处理。
更新了 webpack.config
const path = require("path");
module.exports = {
mode: "production",
entry: "./src/index.js",
output: {
path: path.resolve("dist"),
filename: "index.js",
libraryTarget: "commonjs2"
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /(node_modules)/,
use: "babel-loader"
},
{
test: /\.(eot|svg|ttf|woff|woff2|otf)$/,
use: [
{
loader: "file-loader",
options: {
name: "[name].[ext]",
limit: 10000,
mimetype: "application/font-woff"
}
}
]
}
]
},
resolve: {
alias: {
components: path.resolve(__dirname, "src/components/"),
utils: path.resolve(__dirname, "src/utils/"),
themes: path.resolve(__dirname, "src/themes/")
},
extensions: [".js", ".jsx"]
},
externals: {
// Use external version of React
react: "react"
},
devtool: false
};
我已经发布了一个测试包来确认这一点(react-ui-kit-dontuse)。
演示链接
v0.0.21(Without webpack externals)
https://stackblitz.com/edit/react-xyjgep
v0.0.23(With webpack externals)
https://stackblitz.com/edit/react-ihnmrl
测试包源码:https : //github.com/nithinthampi/react-ui-lib-test
希望这可以帮助!
添加回答
举报