Moment Timezone Data Webpack Plugin

1.5.1 · active · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

This Webpack configuration demonstrates how to integrate `moment-timezone-data-webpack-plugin` to filter timezone data. It sets a date range (2020-2030), includes zones starting with 'Europe/', and all zones for Australia, significantly reducing the bundled `moment-timezone` data size.

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;

view raw JSON →