typescriptwebpackbabeljscommonschunkplugin

Webpack & TypeScript CommonsChunkPlugin not working


I've followed a few guides to setting up CommonsChunkPlugin but it doesn't seem to be working. I've also searched and read the other posts on here but with no luck.

I have three files (TypeScript) which are importing a few of the same libraries (fancybox, moment and pikaday). They are being imported using ES module syntax:

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

My tsconfig.json is set to output ES6 syntax and modules:

{
    "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"
    ]
}

My 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'
}];

As far as I can see that should be finding any chunk that is used twice or more (which there are three) and moving it in to commons-bundle.js but when I run webpack and look at the output files I can see that pikaday, moment and fancybox are all being included in each of the files rather than being moved to commons-bundle.js

Any help would be much appreciated.


Solution

  • You probably want to have minChunks option as function instead of integer value. You fancybox, moment, and pickaday are coming from node_modules, hence modifying minChunks like below should work -

    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;
                }
            }),