信息
我正在尝试生成自己的webpack配置,但在设置优化以将文件拆分成块时遇到了一些问题。
问题
当尝试使用优化功能将文件拆分成块时,会出现以下错误:
错误:冲突:多个块将资产发射到相同的文件名 static/js/bundle.js(chunks main 和 vendors-node_modules_react-hot-loader_patch_js-node_modules_react_jsx-dev-runtime_js-node_mod-4610d2)
如果我删除优化部分,它就可以工作,但没有块。我已经查看了create react app webpack.config.js 来参考其生成的内容。
正如您所看到的,他们已经在开发和生产中使用了优化部分,并将文件拆分成块。为什么我在使用它时会出现冲突错误?
代码
我的配置文件的简化版本(runtimeChunk被禁用,因为它也会导致相同的冲突错误)
webpack.config.js
module.exports = () => {
process.env.NODE_ENV = "development";
process.env.BABEL_ENV = "development";
return {
mode: "development",
entry: ["react-hot-loader/patch", "./src"],
output: {
path: undefined,
publicPath: "/",
filename: "static/js/bundle.js",
chunkFilename: "static/js/[name].chunk.js",
},
optimization: {
minimize: false,
splitChunks: {
chunks: "all",
name: false
},
// runtimeChunk: {
// name: (entrypoint) => `runtime-${entrypoint.name}`,
// },
},
resolve: {
modules: [path.join(__dirname, "src"), "node_modules"],
alias: {
"react-dom": "@hot-loader/react-dom",
},
},
module: {
rules: [
{
test: /\.(js|mjs|jsx|ts|tsx)$/,
include: path.resolve(__dirname, "./src"),
exclude: /node_modules/,
use: ["babel-loader"],
},
],
},
plugins: [
new HtmlWebpackPlugin({
inject: true,
template: path.resolve(__dirname, "./public/index.html"),
}),
new webpack.HotModuleReplacementPlugin(),
],
devServer: {
compress: true,
hot: true,
contentBase: "./build",
historyApiFallback: true,
},
devtool: "inline-source-map",
};
};
.babelrc
{"presets": [["react-app", {"runtime": "automatic"}]]}