All Projects → gilmoreorless → Moment Timezone Data Webpack Plugin

gilmoreorless / Moment Timezone Data Webpack Plugin

Licence: mit
Reduce moment-timezone data size for a webpack build

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Moment Timezone Data Webpack Plugin

Cloudflare Workers Webpack Plugin
Launch Cloudflare Workers to the Edge from the comfort of your build step 🚀
Stars: ✭ 18 (-77.5%)
Mutual labels:  webpack-plugin
Dotenv Webpack
A secure webpack plugin that supports dotenv and other environment variables and only exposes what you choose and use.
Stars: ✭ 1,022 (+1177.5%)
Mutual labels:  webpack-plugin
Optimize Css Assets Webpack Plugin
A Webpack plugin to optimize \ minimize CSS assets.
Stars: ✭ 1,082 (+1252.5%)
Mutual labels:  webpack-plugin
Event Hooks Webpack Plugin
Event hooks plugin for webpack
Stars: ✭ 30 (-62.5%)
Mutual labels:  webpack-plugin
Webpack Aliyun Oss
一个webpack(version >= 4)插件,上传资源到阿里云oss。可以作为webpack插件使用,也可独立使用
Stars: ✭ 36 (-55%)
Mutual labels:  webpack-plugin
Html Inline Css Webpack Plugin
☄️ A webpack plugin for convert external stylesheet to the embedded stylesheet
Stars: ✭ 48 (-40%)
Mutual labels:  webpack-plugin
Error Overlay Webpack Plugin
Catch errors with style 💥✨
Stars: ✭ 821 (+926.25%)
Mutual labels:  webpack-plugin
Wcer
Webpack plugin to enable reloading while developing Chrome extensions.
Stars: ✭ 71 (-11.25%)
Mutual labels:  webpack-plugin
Persistgraphql Webpack Plugin
PersistGraphQL Webpack Plugin
Stars: ✭ 39 (-51.25%)
Mutual labels:  webpack-plugin
Nvue
master分支:webpack4实现一个vue的打包的项目,incremental: 实现增量模块打包
Stars: ✭ 55 (-31.25%)
Mutual labels:  webpack-plugin
Tinypng Webpack Plugin
a tinyPNG plugin for webpack
Stars: ✭ 31 (-61.25%)
Mutual labels:  webpack-plugin
Webpack Alioss Plugin
阿里 oss-webpack 自动上传插件
Stars: ✭ 35 (-56.25%)
Mutual labels:  webpack-plugin
Treat
🍬 Themeable, statically extracted CSS‑in‑JS with near‑zero runtime.
Stars: ✭ 1,058 (+1222.5%)
Mutual labels:  webpack-plugin
Webpack Common Shake
CommonJS Tree Shaker plugin for WebPack
Stars: ✭ 875 (+993.75%)
Mutual labels:  webpack-plugin
Snackui
SnackUI 🍑 - the final React style library. With an *optimizing compiler* that lets you write views naturally, with easier DX, working on native and web at once, all while being faster than hand-rolling your own CSS.
Stars: ✭ 55 (-31.25%)
Mutual labels:  webpack-plugin
Prerender Spa Plugin
Prerenders static HTML in a single-page application.
Stars: ✭ 7,018 (+8672.5%)
Mutual labels:  webpack-plugin
Webpack Webextension Plugin
Webpack plugin that compiles WebExtension manifest.json files and adds smart auto reload
Stars: ✭ 47 (-41.25%)
Mutual labels:  webpack-plugin
Svg Sprite Webpack Plugin
Webpack plugin for loading/extracting SVGs into a sprite file
Stars: ✭ 73 (-8.75%)
Mutual labels:  webpack-plugin
Vue Builder Webpack Plugin
Webpack plugin to build vue files automatically
Stars: ✭ 70 (-12.5%)
Mutual labels:  webpack-plugin
Node Env Webpack Plugin
Simplified `NODE_ENV` handling with webpack
Stars: ✭ 51 (-36.25%)
Mutual labels:  webpack-plugin

moment-timezone-data-webpack-plugin

npm Build Status

Oof, that’s a clunky name, but at least it’s descriptive.

This is a plugin for webpack which reduces data for moment-timezone.

Why is this needed?

Moment Timezone is a comprehensive library for working with time zones in JavaScript. But that comprehensiveness comes with a file size cost. The full time zone data file is 903KiB raw, or 36KiB minified and gzipped (as of moment-timezone version 0.5.23).

That’s a lot of data to send to someone’s browser, especially if you don’t need all of it. Some of the time zones have data dating back to the 19th century. Thankfully there is an API to produce a custom data bundle containing only the time zone definitions you require.

Unfortunately, if you’re building your project with webpack, you don’t get to use a custom data bundle. A webpack build uses the Node.js version of moment-timezone, which automatically includes all the time zone data. Even if you configure Moment Timezone to use a custom data bundle at run-time, the full data file will still be present in your JavaScript bundle.

This plugin allows you to configure which time zone data you want. Any unwanted data is then automatically stripped from the compiled JS bundle at build time.

Use it in combination with the moment-locales-webpack-plugin to further reduce the compiled JS bundle size.

Example

Take a super-simple file which does nothing more than require('moment-timezone'). Building this with webpack in production mode results in over 1 MiB of minified JS code.

What if you only need the default English locale, and time zone data for Australia and New Zealand from 2018 to 2028? (This is a realistic scenario from a recent project.)

Running webpack in production mode results in the following file sizes:

Configuration Raw size Gzipped
Default 1164 KiB 105 KiB
Strip locales 959 KiB (~82%) 56 KiB (~53%)
Strip tz data 265 KiB (~23%) 69 KiB (~66%)
Strip locales & tz data 60 KiB (~5%) 20 KiB (~19%)

(Testing done with [email protected], [email protected], [email protected].)

Even if you still need all the time zones available, reducing the data to a much smaller date range can produce significant file size savings. Building the above example file with data for all zones from 2018 to 2028 produces a file size of 288KiB, or 74KiB gzipped.

⚠️ Make sure you know what you’re doing ❗️️

Dealing with time zones can be tricky, and bugs can pop up in unexpected places. That’s doubly true when you’re auto-removing data at build time. When using this plugin, make absolutely sure that you won’t need the data you’re removing.

For example, if you know for certain that your web site/application...

  1. ...will never deal with past dates & times earlier than a certain point (e.g. the launch date of the application).
  2. ...will never deal with future dates & times beyond a certain point (e.g. details of a specific event).
    • It’s (relatively) safe to remove any data beyond that date (using the endYear option).
  3. ...will only deal with a fixed set of time zones (e.g. rendering times relative to a set of physical buildings in a single country).

However, if you’re allowing users to choose their time zone preference — with no theoretical limit on the range of dates you’ll handle — then you’re going to need all the data you can get.

If you’re in doubt about whether to include some data, err on the side of caution and include it.

Usage

Installation

Using npm:

npm install --save-dev moment-timezone-data-webpack-plugin

Or using yarn:

yarn add --dev moment-timezone-data-webpack-plugin

Configuration

Add the plugin to your webpack config file:

const MomentTimezoneDataPlugin = require('moment-timezone-data-webpack-plugin');

module.exports = {
  plugins: [
    new MomentTimezoneDataPlugin({
      // options
    }),
  ]
};

Plugin options

There are four available options to filter the time zone data. At least one option must be provided.

  • startYear (integer) — Only include data from this year onwards.
  • endYear (integer) — Only include data up to (and including) this year.
  • matchZones — Only include data for time zones with names matching this value. matchZones can be any of these types:
    • string — Include only this zone name as an exact match (e.g. 'Australia/Sydney').
    • regexp — Include zones with names matching the regular expression (e.g. /^Australia\//).
    • array (of the above types) — Include zones matching any of the values of the array. Each value can be a string or a regular expression, which will be matched following the rules above.
  • matchCountries — Only include data for time zones associated with specific countries, as determined by Moment Timezone’s zonesForCountry() API. matchCountries works with ISO 3166 2-letter country codes, and can be any of these types:
    • string — Include zones for this country code as an exact match (e.g. 'AU').
    • regexp — Include zones for country codes matching the regular expression (e.g. /^A|NZ/).
    • array (of the above types) — Include zones for country codes matching any of the values of the array. Each value can be a string or a regular expression, which will be matched following the rules above.

NOTE: The matchCountries option will only work when used with moment-timezone version 0.5.28 or later. If this option is used with a non-compliant version of moment-timezone, an error will be thrown.

All filtering options are AND (a.k.a. conjunction) filters — that is, they become more restrictive as each one is applied. Only zone data that match all the provided filters will be added to the final output. For this reason, it’s probably safer to provide only one of matchZones or matchCountries; providing both is allowed, but you may not get the results you expect.

There are also some non-filtering options that can be provided to configure other behaviour around file locations.

  • cacheDir (string) — A path where the generated files will be cached. If not provided, the files will be cached in an automatically-generated location.
  • momentTimezoneContext (regexp) — A regexp matching a context where moment-timezone is located. The timezone file will be replaced only if it is located in this context. Other instances of the timezone file out of this context will not be touched. This is useful in case you are using a version stored outside of node_modules (e.g. if module or vendor directory is vendor\moment-timezone the context could be matched for example with vendor[\\/]moment-timezone$). Defaults to /node_modules[\\/]moment-timezone$/.

Version support

This plugin has been tested with and officially supports the following dependencies:

  • Node.js 8 or higher
  • webpack 4 and 5
  • moment-timezone v0.1.0 or higher

It theoretically supports older versions of webpack (as it uses built-in webpack plugins internally), but this hasn’t been tested.

Examples

All zones, with a limited date range

const currentYear = new Date().getFullYear();
const plugin = new MomentTimezoneDataPlugin({
  startYear: currentYear - 2,
  endYear: currentYear + 10,
});

All data for a specific zone

const plugin = new MomentTimezoneDataPlugin({
  matchZones: 'America/New_York',
});

All data for a specific country

const plugin = new MomentTimezoneDataPlugin({
  // Includes 'Pacific/Auckland' and 'Pacific/Chatham'
  matchCountries: 'NZ',
});

Limited data for a set of zones (single regular expression)

const plugin = new MomentTimezoneDataPlugin({
  matchZones: /Europe\/(Belfast|London|Paris|Athens)/,
  startYear: 2000,
  endYear: 2030,
});

Limited data for a set of zones (array of values)

const plugin = new MomentTimezoneDataPlugin({
  matchZones: [/^Australia/, 'Pacific/Auckland', 'Etc/UTC'],
  startYear: 2000,
  endYear: 2030,
});

Limited data for a set of countries

const plugin = new MomentTimezoneDataPlugin({
  matchCountries: ['US', 'CA'],
  startYear: 2000,
  endYear: 2030,
});

License

MIT License © Gilmore Davidson

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].