简体   繁体   English

为什么 webpack 在生产模式下忽略 chunk?

[英]Why does webpack ignore chunk in production mode?

I have multiple entry points that share same code.我有多个共享相同代码的入口点。 What I need to do is to extract this code into one file using splitCode in webpack 4. This works fine in development mode but not in production .我需要做的是提取该代码放到一个文件中使用splitCode中的WebPack 4.本工作正常development模式,但不是在production

Configuration file:配置文件:

var path = require('path');
const ManifestPlugin = require('webpack-manifest-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const WebpackMd5Hash = require("webpack-md5-hash");
const CleanWebpackPlugin = require('clean-webpack-plugin');

module.exports = {
    mode: "development",
    module: {
        rules: [
            {
                test: /\.vue$/,
                loader: 'vue-loader'
            },
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {

                    loader: 'babel-loader',
                }
            },
            {
                test: /\.(sass|scss|css)$/,
                use: [
                    "style-loader",
                    MiniCssExtractPlugin.loader,
                    "css-loader",
                    "sass-loader"
                ]
            }
        ],
    },
    output: {
        path: path.join(__dirname, 'public'),
        filename: 'js/[name]-[chunkhash].js',
        chunkFilename: 'js/[name]-[chunkhash].js',
        publicPath: '/'
    },
    externals: {
        jquery: "jQuery"
    },
    optimization: {
        runtimeChunk: "single",
        splitChunks: {
            cacheGroups: {
                vendor: {
                    test: /[\\/]node_modules[\\/]/,
                    name: "vendor",
                    chunks: "all",
                    priority: 1
                },
                utilities: {
                    test: /\.s?js$/,
                    minChunks: 2,
                    name: "utilities",
                    chunks: "all",
                    priority: 0
                }
            }
        }
    },
    context: path.join(__dirname, 'resources/assets'),
    entry: {
        a: './js/a.js',
        b: './js/b.js'
    },
    plugins: [
        new CleanWebpackPlugin(['public/js/*.*', 'public/css/*.*'], {} ),

        new MiniCssExtractPlugin({
            filename: "css/[name]-[contenthash].css"
        }),

        new WebpackMd5Hash(),

        new ManifestPlugin({
            fileName: 'manifest.json'
        }),
    ]
};

In development mode Webpack creates two entry points, one runtime.js , vendor.js and utilities.js which is ok.在发展模式的WebPack创建两个入口点,一个runtime.jsvendor.jsutilities.js这是确定。

When I change mode from development to production , webpack ignores utilities cacheGroups and appends common codebase into two entry points.当我将模式从development更改为production ,webpack 会忽略utilities cacheGroups 并将公共代码库附加到两个入口点中。

What am I missing?我错过了什么?

Webpack version: 4.28.4 Node version: 8.15 Webpack 版本:4.28.4 节点版本:8.15

It seems like setting enforce to true does the job (but I'm not entirely really sure why).似乎将enforce设置为true完成这项工作(但我不完全确定为什么)。

It should be like this:应该是这样的:

utilities: {
    test: /\.s?js$/,
    minChunks: 2,
    name: "utilities",
    chunks: "all",
    priority: 0,
    enforce: true
}

From now on, utilities.js is being created not only in development mode, but also in production.从现在开始, utilities.js不仅在开发模式中创建,而且在生产中创建。

It's not a bug.这不是一个错误。 It's a feature这是一个特点

Webpack 4 splitchunks.cacheGroups is ignored in production mode IF the new chunk is less than the size of 30kb.如果新块小于 30kb 的大小,则在生产模式下会忽略 Webpack 4 splitchunks.cacheGroups

solution to override this default condition:覆盖此默认条件的解决方案:

user splitchunks.cacheGroups.runtime.enforce: true if you want to really make sure that these chunks are created用户splitchunks.cacheGroups.runtime.enforce: true如果你想真正确保这些块被创建

Check the documention for further details https://webpack.js.org/plugins/split-chunks-plugin/#defaults检查文档以获取更多详细信息https://webpack.js.org/plugins/split-chunks-plugin/#defaults

Specifying minChunks: 2 means it will only create a split bundle if the given common imports is specified in at least 2 modules.指定minChunks: 2意味着如果给定的公共导入至少在 2 个模块中指定,它只会创建一个拆分包。 You might want to verify but dropping it to 1.您可能想要验证但将其降为 1。

There are few additional default rules listed here: https://gist.github.com/sokra/1522d586b8e5c0f5072d7565c2bee693#defaults but mainly if the common codebase isn't larger than 30kb (before min+gz) then it won't get split out.这里列出了一些额外的默认规则: https : //gist.github.com/sokra/1522d586b8e5c0f5072d7565c2bee693#defaults但主要是如果公共代码库不大于 30kb(在 min+gz 之前)那么它不会被拆分. You can force it by updated the key minSize as listed in the default optimization config .您可以通过更新默认优化配置中列出的密钥minSize来强制它。

Webpack SplitChunksPlugin , by default , ignores any chunk smaller than 30kb.默认情况下,Webpack SplitChunksPlugin忽略任何小于 30kb 的块。 If you run Webpack in development mode, you'll be able to see the bundle size of utilities.js and enforce the split by setting optimization.splitChunks.minSize option smaller than the size of utilities.js .如果您在开发模式下运行的WebPack,你就可以看到的集束大小utilities.js并通过设置强制分流optimization.splitChunks.minSize比的尺寸选项utilities.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM