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

JavascriptFront End TechnologyObject Oriented Programming

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,


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');

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

Published on 02-Dec-2019 11:26:35