Vite Plugin for Moment Timezone Data Optimization
vite-plugin-moment-timezone is a Vite plugin designed to optimize bundle size by selectively stripping unneeded time zone data from the `moment-timezone` library during the build phase. This is particularly useful for applications that only require a subset of time zones or a specific date range, as `moment-timezone` can be quite large. The current stable version is 0.0.4. Given its low version number and explicit mention of "extremely limited testing" and being "new," it does not yet have a predictable release cadence and should be used with caution, thoroughly testing builds before deployment. Its key differentiator is providing build-time tree-shaking for `moment-timezone` data within a Vite environment, allowing configuration of `zones`, `startYear`, and `endYear` to significantly reduce the final JavaScript bundle size compared to including all available `moment-timezone` data.
Common errors
-
Error: Cannot find module 'moment-timezone'
cause The `moment-timezone` package is a peer dependency and must be installed separately.fixRun `npm install moment-timezone` or `yarn add moment-timezone` in your project. -
TypeError: Cannot read properties of undefined (reading 'plugins')
cause The `momentTimezonePlugin` was not correctly imported or included in the `plugins` array in `vite.config.js`.fixVerify that `import momentTimezonePlugin from 'vite-plugin-moment-timezone';` is present and the plugin is added to the `plugins` array, e.g., `plugins: [momentTimezonePlugin({ ... })]`.
Warnings
- gotcha The plugin explicitly states "extremely limited testing" and is a new project. Production deployments should proceed with caution and thorough testing.
- breaking This plugin only runs during the build phase. Any `moment-timezone` data manipulation or loading behavior observed in development mode might differ from the production build.
- breaking The plugin has only been tested with Node.js 20, Vite 5, and moment-timezone v0.5.0 or higher. Using older or significantly newer versions of these dependencies may lead to unexpected behavior or build failures.
Install
-
npm install vite-plugin-moment-timezone -
yarn add vite-plugin-moment-timezone -
pnpm add vite-plugin-moment-timezone
Imports
- momentTimezonePlugin
const momentTimezonePlugin = require('vite-plugin-moment-timezone');import momentTimezonePlugin from 'vite-plugin-moment-timezone';
- UserConfig
import { defineConfig, type UserConfig } from 'vite';
Quickstart
import { defineConfig } from 'vite';
import momentTimezonePlugin from 'vite-plugin-moment-timezone';
export default defineConfig({
plugins: [
momentTimezonePlugin({
zones: ['America/Los_Angeles', 'Europe/London'],
startYear: 2020,
endYear: 2025
}),
],
build: {
// Example of a common Vite build setting, not directly related to the plugin but good context
minify: 'esbuild',
sourcemap: true,
},
});