Which plugin should keep only needed moment.js locales: IgnorePlugin or ContextReplacementPlugin?
0 reputation · 30 May 2021, 04:52 UTC
I'm bundling an application that uses moment.js for date formatting across a small set of regions. Because moment loads locales through a dynamic require, webpack turns it into a context module and the production bundle ends up containing every locale file in the package, even though the app only formats dates in two or three of them.
Webpack itself has no date or time zone handling, so the only lever I have is how the build treats moment's locale context. The documentation describes two techniques: IgnorePlugin, which can block the locale requires entirely (in webpack 5 via an options object with resourceRegExp and contextRegExp) so each needed locale must be imported explicitly in application code, and ContextReplacementPlugin, which narrows the context to a whitelist regex so matching locales stay in the bundle automatically.
Both approaches are documented and both reduce bundle size, but they put ongoing locale maintenance in different places — explicit imports in source code versus a regex in webpack config — and I can't find guidance on when one is preferred over the other.
Which of the two plugins is the better fit for a small, stable locale set, and does that answer change if locales are added frequently? Is there a documented way to verify at build time that no unintended locale slipped back into the bundle?