Reduce Moment.js Npm File Size For Webpack

Sep 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.

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.