Webpack 和 TypeScript CommonsChunkPlugin 不工作

Webpack & TypeScript CommonsChunkPlugin not working

我已经按照一些指南设置 CommonsChunkPlugin,但它似乎不起作用。我也搜索并阅读了这里的其他帖子,但没有成功。

我有三个文件 (TypeScript),它们正在导入一些相同的库(fancybox、moment 和 pikaday)。它们正在使用 ES 模块语法导入:

import * as fancybox from 'fancybox';
import * as moment from 'moment';
import * as pikaday from 'pikaday';

我的tsconfig.json设置为输出ES6语法和模块:

{
    "compileOnSave": true,
    "compilerOptions": {
        "target": "es6",
        "module": "es6",
        "noEmitOnError": true,
        "moduleResolution": "node",
        "sourceMap": true,
        "diagnostics": false,
        "noFallthroughCasesInSwitch": true,
        "noImplicitReturns": true,
        "traceResolution": false
    },
    "exclude": [
        "node_modules",
        "venue-finder"
    ]
}

我的webpack.config.js:

const webpack = require('webpack');
const path = require('path');
const WebpackNotifierPlugin = require('webpack-notifier');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;

module.exports = [{
    bail: true,
    entry: {
        global: path.resolve(__dirname, 'js/responsive/global.ts'),
        todo: path.resolve(__dirname, 'js/todo/todo.ts'),
        budget: path.resolve(__dirname, 'Planner/javascript/Budget/BudgetPlannerUI.ts'),
        promotions: path.resolve(__dirname, 'js/promotions-management.ts'),
        planner: path.resolve(__dirname, 'Planner/javascript/MyPlanner.ts')
    },
    output: {
        path: path.resolve(__dirname, 'dist/js'),
        filename: '[name].js'
    },
    module: {
        rules: [{
            test: /\.ts(x?)$/,
            exclude: /node_modules/,
            use: [
                {
                    loader: 'babel-loader',
                    options: {
                        'presets': [
                            ['env', {
                                'modules': false
                            }]
                        ]
                    }
                },
                {
                    loader: 'ts-loader'
                }
            ]
        }]
    },
    plugins: [
        new WebpackNotifierPlugin({ alwaysNotify: true }),
        new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
        new webpack.optimize.CommonsChunkPlugin({
            name: 'commons',
            filename: 'commons-bundle.js',
            minchunks: 2
        }),
        new BundleAnalyzerPlugin()
    ],
    resolve: {
        extensions: ['.js', '.ts', '.tsx']
    },
    devtool: 'none'
}];

据我所知,应该找到使用两次或更多次(共有三个)的任何块并将其移动到 commons-bundle.js 但是当我 运行 webpack 并查看输出文件,我可以看到 pikaday、moment 和 fancybox 都包含在每个文件中,而不是移动到 commons-bundle.js

如有任何帮助,我们将不胜感激。

您可能希望将 minChunks 选项作为函数而不是整数值。你的 fancybox、moment 和 pickaday 来自 node_modules,因此像下面这样修改 minChunks 应该有效 -

new webpack.optimize.CommonsChunkPlugin({
            name: 'commons',
            filename: 'commons-bundle.js',
            chunks: [global, todo, budget, promotions, planner], //optional, however I find this as good practice
            minChunks: function (module) {
               return module.context && module.context.indexOf("node_modules") !== -1;
            }
        }),