Moment Timezone Data Webpack Plugin
This webpack plugin, `moment-timezone-data-webpack-plugin`, is designed to significantly reduce the bundle size of `moment-timezone` by selectively stripping unneeded time zone data during the webpack build process. As of version 1.5.1, it provides critical functionality for applications that use `moment-timezone` but don't require its entire historical and global dataset, which can be over 900KiB raw. The plugin actively processes the `moment-timezone` data module, allowing developers to specify date ranges (e.g., `startYear`, `endYear`), specific zones via regular expressions (`matchZones`), or countries (`matchCountries`). This addresses a common issue where `moment-timezone` includes all data by default in webpack builds, even if runtime configuration limits the data usage. The plugin is in active maintenance, with recent updates ensuring compatibility with NodeJS 18 and Webpack 5. It works complementarily with `moment-locales-webpack-plugin` for further bundle size optimizations.
Common errors
-
TypeError: MomentTimezoneDataPlugin is not a constructor
cause The `new` keyword was omitted when instantiating the plugin in the webpack configuration.fixChange `plugins: [MomentTimezoneDataPlugin({...})]` to `plugins: [new MomentTimezoneDataPlugin({...})]`. -
Module not found: Error: Can't resolve 'moment-timezone' in...
cause The `moment-timezone` package is not installed or not resolvable by webpack.fixInstall `moment-timezone` using `npm install moment-timezone` or `yarn add moment-timezone`. -
ERROR in [MomentTimezoneDataPlugin] Error: The timezone data for '...' is not available in the specified range...
cause Your application is attempting to use a timezone or date range that was stripped by the plugin configuration.fixAdjust the plugin options (`startYear`, `endYear`, `matchZones`, `matchCountries`) to include the necessary timezone data. Thoroughly test all time-related functionalities. -
Webpack compilation error: Cannot read properties of undefined (reading 'compilation')
cause Often indicates an incompatibility between the plugin and the installed `webpack` version, or an issue with the webpack configuration itself where the plugin is not correctly initialized.fixVerify that your `webpack` version (`4.x.x` or `5.x.x`) is compatible with the plugin version. Ensure `webpack` and `moment-timezone-data-webpack-plugin` are correctly installed and configured in your `package.json` and `webpack.config.js`.
Warnings
- gotcha Over-aggressive data stripping can lead to runtime errors where `moment-timezone` reports missing data for dates or zones your application unexpectedly needs. Always thoroughly test your application after configuring this plugin.
- breaking As of v1.5.1, the plugin no longer officially tests against NodeJS 8 or 10. While no explicit code changes were made to break compatibility, usage on these End-of-Life Node.js versions is not guaranteed and unsupported.
- gotcha Incorrectly installing `moment-timezone` or `webpack` as non-peer dependencies, or incompatible versions, can cause build failures. The plugin relies on specific `webpack` APIs and `moment-timezone`'s internal structure.
- gotcha The plugin requires `new` keyword when instantiating in `webpack.config.js`. Forgetting it will result in an error indicating the plugin is not a constructor.
Install
-
npm install moment-timezone-data-webpack-plugin -
yarn add moment-timezone-data-webpack-plugin -
pnpm add moment-timezone-data-webpack-plugin
Imports
- default
import MomentTimezoneDataPlugin from 'moment-timezone-data-webpack-plugin';
const MomentTimezoneDataPlugin = require('moment-timezone-data-webpack-plugin'); - default
const MomentTimezoneDataPlugin = require('moment-timezone-data-webpack-plugin');import MomentTimezoneDataPlugin from 'moment-timezone-data-webpack-plugin';
- MomentTimezoneDataPlugin
plugins: [MomentTimezoneDataPlugin({ /* options */ })]plugins: [new MomentTimezoneDataPlugin({ /* options */ })]
Quickstart
import path from 'path';
import { Configuration } from 'webpack';
import MomentTimezoneDataPlugin from 'moment-timezone-data-webpack-plugin';
const config: Configuration = {
mode: 'production',
entry: './src/index.ts', // Assuming a simple entry point
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
},
module: {
rules: [
{
test: /\.ts$/,
use: 'ts-loader',
exclude: /node_modules/,
},
],
},
resolve: {
extensions: ['.ts', '.js'],
},
plugins: [
// This example limits timezone data to specific years and only for zones matching 'Europe/'
// and also includes all zones for 'AU' (Australia).
// It's crucial to ensure these settings cover all needed timezones for your application.
new MomentTimezoneDataPlugin({
startYear: 2020,
endYear: 2030,
matchZones: /Europe\//, // Only include zones matching 'Europe/'
matchCountries: ['AU'], // Include all zones for Australia
cacheDir: path.resolve(__dirname, '.moment-timezone-cache') // Optional: custom cache directory
}),
// Optionally, if also stripping locales
// new MomentLocalesWebpackPlugin({
// localesToKeep: ['en', 'es'],
// }),
],
};
export default config;