Reduce Moment.js Npm File Size For Webpack

September 15, 2017

Install moment.js npm.

npm install moment --save

Edit webpack config file to configure IgnorePlugin to exclude all locale files.

var webpack = require('webpack')

module.exports = {
  ...
  plugins: [
    // Ignore all locale files of moment.js
    new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
  ],
  ...
};

If you need some locales, you can still load them within your code.

import moment from 'moment'
import 'moment/locale/zh-cn'

For moment.js v2.18.1, the full javascript file size is 453kB (non-gzip and non-minified), and javascript without locale file size is 126kB.

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.