Despite following various tutorials on setting up CommonsChunkPlugin, I am unable to get it to work. I have also gone through the existing posts related to this issue without any success.
In my project, I have three TypeScript files that require the same libraries (fancybox, moment, and pikaday) using ES module syntax:
import * as fancybox from 'fancybox';
import * as moment from 'moment';
import * as pikaday from 'pikaday';
My tsconfig.json
is configured 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"
]
}
Here is 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'
}];
Even though the configuration seems correct, instead of moving pikaday, moment, and fancybox into commons-bundle.js
, all three libraries are being included in each output file when I run webpack
.
Your assistance with this matter would be greatly appreciated.