1 回答

TA貢獻1890條經(jīng)驗 獲得超9個贊
查看 webpack 配置,我可以看到,UI 工具包與react包含的內(nèi)容捆綁在一起,這可能會導(dǎo)致問題。
為了避免這種情況,您可以使用 webpack externals。
https://webpack.js.org/configuration/externals/
externals 配置選項提供了一種從輸出包中排除依賴項的方法。相反,創(chuàng)建的包依賴于存在于消費者環(huán)境中的依賴關(guān)系。此功能通常對庫開發(fā)人員最有用,但是它有多種應(yīng)用程序。
因此,您可以將 UI Kit webpack 配置更新為不包含react,并且 peerDependencies 應(yīng)該負責(zé)庫的任何使用者的依賴項處理。
更新了 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
};
我已經(jīng)發(fā)布了一個測試包來確認這一點(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
希望這可以幫助!
添加回答
舉報