我正在尝试向私有存储库发布一个自定义的react组件。
我使用react-loadable按需加载子组件。在本地运行时,一切正常。index.js文件在需要时正确地向chunk.js文件发出请求。但是,当发布并从另一个项目中使用时,该组件在尝试请求子组件块时会遇到404错误。在编写库时,我应该如何分割块并按需加载它们?这可能吗,还是我想错了?
这是我的webpack.config.js,以防这是一个简单的配置问题:
module.exports = () => ({
context: __dirname,
mode: 'development',
entry: './src/index.jsx',
output: {
path: path.join(__dirname, './dist'),
filename: "index.js",
library: "reactcombobox",
libraryTarget: "umd",
publicPath: path.join(__dirname, './dist'),
umdNamedDefine: true
},
plugins: [
new webpack.LoaderOptionsPlugin({ options: { context: __dirname}}),
new CleanWebpackPlugin([path.join(__dirname, './dist')], {verbose: true, allowExternal: true})
],
module: {
rules: [
{
test: /.jsx?$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: [
['@babel/preset-env', {
"targets": {
"browsers": ["last 4 Chrome versions"]
}
}],
'@babel/preset-react'
],
plugins: [
'@babel/plugin-transform-react-jsx',
'@babel/plugin-proposal-class-properties',
'@babel/plugin-syntax-dynamic-import'
]
}
}
},
{
test: /.css$/,
use: ["style-loader","css-loader"]
}
]
},
externals: {
react: {
root: 'React',
commonjs2: 'react',
commonjs: 'react',
amd: 'react',
},
'react-dom': {
root: 'ReactDOM',
commonjs2: 'react-dom',
commonjs: 'react-dom',
amd: 'react-dom',
}
},
resolve: {
extensions: ['*', '.js', '.jsx']
}
});发布于 2022-07-15 01:38:42
如果组件没有相对,请拆分到更多文件,并使用导出{a1,a2};不要使用可加载。
https://stackoverflow.com/questions/51831438
复制相似问题