Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would webpack 4 generate files named 0.js, 1.js 2.js?

Tags:

webpack

In my attempts to switch from webpack 3 to 4, I use a configuration that simplified looks like the following extract. The build is successful but generates some chunks that have only a number a filename and I cannot seem to understand why?

0.css   12.5 KiB                             0  [emitted]
 0.js    312 KiB                             0  [emitted]
 1.js   90.3 KiB                             1  [emitted]
 2.js    109 KiB                             2  [emitted]
 3.js    647 KiB                             3  [emitted]
 4.js   1.14 MiB                             4  [emitted]
5.css   33.5 KiB                             5  [emitted]
 5.js   1.56 MiB                             5  [emitted]
6.css  602 bytes                             6  [emitted]
 6.js   92.8 KiB                             6  [emitted]

configuration:

const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

module.exports = function (env, options) {
    const PRODUCTION = options.mode === 'production';

    return {
        entry: {
            common: ['libA', 'libB', './common/A.js', './common/A.js', /* ... */],
            pageA: ['./src/pageA/file1.js', './src/pageA/file2.js', /* ... */],
            pageB: ['./src/pageB/file1.js', './src/pageB/file2.js', /* ... */],
            /* ... */
        },
        output: {
            path: path.resolve('./dist'),
            filename: `[name]${PRODUCTION ? '.min' : ''}.js`,
            chunkFilename: `[name]${PRODUCTION ? '.min' : ''}.js`,
            libraryTarget: 'var'
        },
        module: {
            rules: [
                {
                    test: /\.js$/i,
                    include: /src/,
                    exclude: /node_modules/,
                    use: {
                        loader: 'babel-loader'
                    }
                },
                {
                    test: /\.css$/i,
                    include: /src/,
                    exclude: /node_modules/,
                    use: [
                        MiniCssExtractPlugin.loader,
                        {
                            loader: 'css-loader',
                            options: {
                                sourceMap: false,
                                importLoaders: 1
                            }
                        }
                    ]
                },
                /* ... */
            ]
        },
        plugins: [
            new MiniCssExtractPlugin({
                filename: `[name].${PRODUCTION ? 'min.css' : 'css'}`,
                chunkFilename: `[name].${PRODUCTION ? 'min.css' : 'css'}`
            })
        ],
        optimization: {
            splitChunks: {
                chunks: 'initial',
                name: false,
                cacheGroups: {
                    common: {
                        test: true,
                        name: 'common',
                        chunks: 'initial',
                        minSize: 0,
                        minChunks: 10,
                        reuseExistingChunk: true,
                        enforce: true
                    }
                }
            }
        }
    };
};
like image 947
doberkofler Avatar asked Apr 15 '18 20:04

doberkofler


People also ask

How do I bundle a Javascript file in webpack?

You can bundle your JavaScript using the CLI command by providing an entry file and output path. Webpack will automatically resolve all dependencies from import and require and bundle them into a single output together with your app's script.

Does webpack need Index js?

Out of the box, webpack won't require you to use a configuration file. However, it will assume the entry point of your project is src/index.js and will output the result in dist/main.js minified and optimized for production.

Where are webpack files stored?

To answer your specific question, the webpack configuration is stored wherever your global node_modules are installed; on Windows this is typically %AppData%\Roaming\npm\node_modules\powerbi-visuals-tools\lib\webpack. config. js.

What is the output of webpack?

Configuring the output configuration options tells webpack how to write the compiled files to disk. Note that, while there can be multiple entry points, only one output configuration is specified.


1 Answers

Each x.js file is created from dynamic import in your code.

Webpack supports import() & require.ensure() syntax.

Both of them supports chunk naming:

  1. import() - with a comment

import(/* webpackChunkName: "my-chunk-name" */ 'my-comp');

  1. require.ensure() - specifying the 4th parameter
require.ensure(['b'], function(require) {
   var c = require('c');
}, console.error, 'chunkName');
-----------------------^
like image 141
felixmosh Avatar answered Oct 18 '22 09:10

felixmosh