Home > OS >  use sequential number for chunkFileName in webpack
use sequential number for chunkFileName in webpack

Time:12-28

I would like to use a sequential number such as 1.js, 2.js,... for chunkFileName in production mode of webpack v5. I set [index].js for naming but webpack gives me following error:

Error: Conflict: Multiple chunks emit assets to the same filename [index].js (chunks 179 and 216)

Here's my config for webpack:

var config = {
    devtool: 'eval-source-map',
    cache: true,
    entry: {
        main: path.join(__dirname, "app", "App.js"),
    },
    output: {
        path: path.join(__dirname, 'scripts', 'js'),
        filename: '[name].js',
        chunkFilename: '[name].js',
        sourceMapFilename: '[file].map',
        publicPath: '/scripts/js/'
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        babelrc: false,
                        presets: [
                            ['es2015', { modules: false }],
                            'react',
                        ],
                        plugins: [
                            'syntax-dynamic-import',
                            'transform-object-rest-spread',
                            'transform-class-properties',
                            'transform-object-assign',
                        ],
                    }
                },
            },
            {
                // Preprocess our own .css files
                // This is the place to add your own loaders (e.g. sass/less etc.)
                // for a list of loaders, see https://webpack.js.org/loaders/#styling
                test: /\.css$/,
                exclude: /node_modules/,
                use: [
                    'style-loader',
                    'css-loader',
                ]
            }
        ],
    },
    optimization: {
        minimize: false,
        splitChunks: {
            cacheGroups: {
                defaultVendors: {
                    test: /[\\/]node_modules[\\/]/,
                    name: "vendors",
                    chunks: "all"
                }
            },

        }
    },
    plugins: [
        new CompressionPlugin({
            algorithm: 'gzip',
            test: /\.(js)$|\.(css)$|\.(html)$|\.eot?. $|\.ttf?. $|\.woff?. $|\.svg?. $/,
            //threshold: 10240,
            //minRatio: 0
        })
    ],
    resolve: {
        extensions: ['.js', '.jsx', '.css', '.ts'],
        alias: {
            'react-loadable': path.resolve(__dirname, 'app/app.js'),
        },
    },
};

if (process.env.NODE_ENV === 'production') {
    const TerserPlugin = require("terser-webpack-plugin");
    config.devtool = false;
    config.output.chunkFilename = '[index].js';
    config.optimization = {
        minimize: true,
        minimizer: [new TerserPlugin()],
        splitChunks: {
            cacheGroups: {
                vendors: {
                    test: /[\\/]node_modules[\\/]/,
                    name: "vendors",
                    chunks: "all",
                }
            },

        }
    },
        config.plugins = [
            new webpack.optimize.AggressiveMergingPlugin(),
            new webpack.DefinePlugin({
                'process.env': { NODE_ENV: JSON.stringify('production') }
            }),
            new CompressionPlugin({
                algorithm: 'gzip',
                test: /\.(js)$|\.(css)$|\.(html)$|\.eot?. $|\.ttf?. $|\.woff?. $|\.svg?. $/,
                //threshold: 10240,
                //minRatio: 0
            })
        ];
}
module.exports = config;

Does anyone know where is the problem? Thank you in advance!

CodePudding user response:

I've found my answer! There is a config for webpack's optimization section which can be set and it gives us a sequential numbers as chunkFileName and its chunkIds which should set to natural. So:

 config.optimization = {
        chunkIds: "natural",
        minimize: true,
        minimizer: [new TerserPlugin()],
        splitChunks: {
            cacheGroups: {
                vendors: {
                    test: /[\\/]node_modules[\\/]/,
                    name: "vendors",
                    chunks: "all"
                }
            },
        }
    }
  • Related