9

我正在使用WebPack CommonsChunkPlugin提取重复代码并减少 JavaScript 代码大小。我有两个 html 页面和两个条目。我还添加了ReactJs供应商条目。到目前为止,在webpack.config.js我们有:

var path = require("path");
var webpack = require('webpack');
var BundleTracker = require('webpack-bundle-tracker');
var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;

module.exports = {
    context: __dirname,
    entry: {
        react: ["react", "react-dom"],
        home: './assets/js/home.jsx',
        about: './assets/js/about.jsx',
    },

    output: {
        path: path.resolve('./assets/bundles/'),
        filename: "[name].js",
    },

    plugins: [
        new BundleTracker({filename: './webpack-stats.json'}),

        new webpack.optimize.CommonsChunkPlugin({
            name: 'react',
            minChunks: Infinity
        }),

        new BundleAnalyzerPlugin(),
    ],

    module: {
        rules: [
            {
              test: /\.jsx?$/, 
              exclude: /node_modules/,
              loader: 'babel-loader',
              options: { 
                  plugins: [["lodash", { "id": ["semantic-ui-react"] }]],
                  presets: ["es2015", "react"]
              }  
            },
        ],
    },

    resolve: {
        modules: ['node_modules', 'bower_components'],
        extensions: ['*', '.js', '.jsx']
    },
};

此配置结果与webpack-bundle-analyzer

webpack 包分析器输出

如您所见,有一些重复的代码,一些在红色区域,另一些在绿色区域。我想从家中提取这个 js 代码并将包提取到一个单独的包中。为了提取红色区域代码,即lodash库,我在 webpack 配置中添加了这些行:

new webpack.optimize.CommonsChunkPlugin({
    name: 'lodash',
    minChunks: function(module, count) {
        return module.context.indexOf('node_modules/lodash') >= 0;
    }
}), 

但它没有按预期工作,lodash 库代码仍然在 home 和 about bundles 中,webpack 还创建了一个名为 lodash 的 bundle,它几乎是空的并且不包含任何 js 库。

关于如何解决它的任何想法?提取绿色代码怎么样?

4

2 回答 2

3

您的问题是您在每个文件中导入第三方库,.js/.jsx而之前没有将其导入公共文件(通常称为vendor.js)。
如果你有这个文件来导入你所有的依赖项,并且你把它作为入口包含到CommonsChunkPlugin中,webpack 不会在你的最终包(home.jsabout.js)中再次包含你的库。该技术在 webpack 文档 中称为代码拆分。

vendor.js(或适合您情况的名称)

import 'react';
import 'react-dom';
import 'lodash';
import 'semantic-ui-react';
//... all your npm packages

webpack.config.js

var webpack = require('webpack');
var path = require('path');

module.exports = {
    context: __dirname,
    entry: {
        vendor: './assets/js/vendor.js,
        home: './assets/js/home.jsx',
        about: './assets/js/about.jsx',
    },
    output: {
        path: path.resolve('./assets/bundles/'),
        filename: '[name].js',
    },
    plugins: [
        new webpack.optimize.CommonsChunkPlugin({
            name: 'vendor',
            minChunks: Infinity
        }),
    ],

    //Rest of Your config ...
};

索引.html

<body>
    <!-- AFTER YOUR HTML CODE -->

    <script type="text/javascript" src="/assets/bundles/vendor.js"></script>
    <script type="text/javascript" src="/assets/bundles/home.js"></script>
    <script type="text/javascript" src="/assets/bundles/about.js"></script>
</body>


检查 webpack 代码拆分文档:

于 2017-08-02T02:40:03.760 回答
1

我设法通过向插件添加一个公共块来解决这个问题。所以最终的 webpack 配置是:

var path = require("path");
var webpack = require('webpack');
var BundleTracker = require('webpack-bundle-tracker');

module.exports = {

    context: __dirname,

    entry: {
        react: ["react", "react-dom"],
        home: './assets/js/home.jsx',
        about: './assets/js/about.jsx',
    },

    output: {
        path: path.resolve('./assets/bundles/'),
        filename: "[name].js",
    },

    plugins: [

        new BundleTracker({filename: './webpack-stats.json'}),

        new webpack.optimize.CommonsChunkPlugin({
            name: 'react',
            filename: '[name].js',
            minChunks: Infinity,
        }),

        new webpack.optimize.CommonsChunkPlugin({
            name: 'common',
            chunks: ['home', 'about'],
            filename: '[name].js',
        }),
    ],

    module: {
        rules: [
            {
                test: /\.jsx?$/, 
                exclude: /node_modules/,
                loader: 'babel-loader',
                options: { 
                    plugins: [
                      ["lodash", { "id": ["semantic-ui-react"] }]
                    ],
                    presets: ["es2015", "react"]
                }
            },
        ],
    },

    resolve: {
        modules: ['node_modules', 'bower_components'],
        extensions: ['*', '.js', '.jsx']
    },
};

现在捆绑分析器的输出是这样的:

分析仪输出

如图所示,常见的 semantic-ui-react 和 lodash 库现在只是在公共包中,不再重复。

于 2017-08-02T21:23:51.150 回答