Dwight Watson's blog

Ignoring Moment.js Locales with Webpacker

This blog post was originally published a little while ago. Please consider that it may no longer be relevant or even accurate.

There's a slight pain point when loading Moment.js through Webpack in that it pulls in every single locale available, regardless of whether you use it or not which adds to the file size substantially. It's something that's likely to be fixed in a new major release of Moment or it's follow up Luxon but luckily it's pretty easy to resolve now.

In your config/webpack/environment.js file you can instruct Webpacker to use an ignore plugin which will ignore loading any locale files in the context of Moment.

const { environment } = require("@rails/webpacker");
const webpack = require("webpack");

environment.plugins.insert(
"IgnorePlugin",
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)
);

module.exports = environment;

A blog about Laravel & Rails by Dwight Watson;

Picture of Dwight Watson

Follow me on Twitter, or GitHub.