Optimize moment.js size using webpack
16 Jun 2017
import * as moment from 'moment';
When you run webpack-bundle-analyzer you will something crazy as below
As most projects are developed for targeted languages, so it doesn't require all locales to be loaded. So, it is sensible to require just the required one.
ContextReplacementPlugin, that allows us to configure what to require or includer using RegEx (Regular expression). so modify your WebpackConfig.json to import just the en locales with below script
var ContextReplacementPlugin = require('webpack/lib/ContextReplacementPlugin'); plugins: [ ..., new ContextReplacementPlugin(/moment[\/\\]locale$/, /en/), ...]
Now re-run the webpack build and see the analyzer output. It should look similar to below with size approx. 17KB which is way too better.
and My experience ;)