How to prevent moment.js from loading locales with webpack?


A local file is a .json file that contains a set of translations for the text strings used in a theme template file. A separate local file is used for every language.

When you require moment.js in your code and pack it with webpack, the bundle size becomes huge because it includes all locale files.

You can remove all locale files using the IgnorePlugin. For example,

Example

const webpack = require('webpack');
module.exports = {
   plugins: [
      // Ignore all locale files of moment.js
      new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
   ],
};
// load specific locales in your code.
const moment = require('moment');
require('moment/locale/ja');
moment.locale('ja');

When bundling, webpack will only use the locale files for ja. This will greatly reduce the bundle size.

Updated on: 02-Dec-2019

359 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements