Webpack,优化分块给出 "Conflict: Multiple chunks emit assets to the same filename" 错误

Webpack, optimization chunking gives "Conflict: Multiple chunks emit assets to the same filename" error

信息

我正在尝试生成自己的 webpack 配置,但在运行时遇到了一些问题。

问题

当尝试使用优化将文件拆分成块时,我收到如下所示的错误

Error: Conflict: Multiple chunks emit assets to the same filename static/js/bundle.js (chunks main and vendors-node_modules_react-hot-loader_patch_js-node_modules_react_jsx-dev-runtime_js-node_mod-4610d2)

如果我删除优化部分,它会起作用但不会分块。我查看了 create react 应用程序 webpack.config.js 以在生成此应用程序时获得一些参考。

如您所见,他们在开发和生产中都有优化部分处理分块。为什么我在使用时会出现冲突错误?

代码

Minified/simplified 我的配置版本(禁用 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"}]]}

得到它的工作必须将 filename: "static/js/bundle.js" 更改为 filename: "static/js/[name].js"

output: {
    path: undefined,
    publicPath: "/",
    filename: "static/js/[name].js",
    chunkFilename: "static/js/[name].chunk.js",
}

如果您正在处理弹出的 Create React App 并且您遇到类似的错误

Multiple chunks emit assets to the same filename static/js/bundle.js (chunks main and runtime-main)

您只需更改输出配置中的文件名 属性 来自

filename: isEnvProduction
        ? 'static/js/[name].[contenthash:8].js'
        : isEnvDevelopment && 'static/js/bundle.js',

filename: isEnvProduction
        ? 'static/js/[name].[contenthash:8].js'
        : isEnvDevelopment && 'static/js/[name].js',

其中 [name] 占位符为每个输出包指定了不同的名称,而不是固定名称。

在我的例子中,它是由我在 optimization.

中使用 runtimeChunk 属性 生成的 runtime-main.js 文件引起的
runtimeChunk: {
  name: entrypoint => `runtime-${entrypoint.name}`,
},