Gathering detailed insights and metrics for moment-timezone-data-webpack-plugin
Gathering detailed insights and metrics for moment-timezone-data-webpack-plugin
Gathering detailed insights and metrics for moment-timezone-data-webpack-plugin
Gathering detailed insights and metrics for moment-timezone-data-webpack-plugin
moment-timezone
Parse and display moments in any timezone.
@fullcalendar/moment-timezone
Enhanced named time zone functionality with Moment Timezone
react-moment
React component for the moment date library.
dayjs-timezone-iana-plugin
DayJS timezone plugin alternative to manage DST correctly
Reduce moment-timezone data size for a webpack build
npm install moment-timezone-data-webpack-plugin
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
92 Stars
141 Commits
7 Forks
4 Watching
1 Branches
4 Contributors
Updated on 21 Nov 2024
Minified
Minified + Gzipped
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-1.7%
27,730
Compared to previous day
Last week
1%
145,834
Compared to previous week
Last month
28.2%
642,093
Compared to previous month
Last year
5%
6,498,942
Compared to previous year
2
2
Oof, that’s a clunky name, but at least it’s descriptive.
This is a plugin for webpack which reduces data for moment-timezone.
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.
As of late 2020, the Moment and Moment Timezone projects are in maintenance-only mode. But they are still getting occasional updates, especially for new time zone data. This plugin will be maintained as long as both webpack and Moment Timezone are maintained.
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 webpack@4.28.3
, moment@2.23.0
, moment-timezone@0.5.23
.)
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.
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...
startYear
option).endYear
option).matchZones
and/or matchCountries
options).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.
Using npm:
1npm install --save-dev moment-timezone-data-webpack-plugin
Or using yarn:
1yarn add --dev moment-timezone-data-webpack-plugin
Add the plugin to your webpack config file:
1const MomentTimezoneDataPlugin = require('moment-timezone-data-webpack-plugin'); 2 3module.exports = { 4 plugins: [ 5 new MomentTimezoneDataPlugin({ 6 // options 7 }), 8 ] 9};
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:
'Australia/Sydney'
)./^Australia\//
).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:
'AU'
)./^A|NZ/
).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$/
.This plugin has been tested with and officially supports the following dependencies:
It theoretically supports older versions of webpack (as it uses built-in webpack plugins internally), but this hasn’t been tested.
1const currentYear = new Date().getFullYear();
2const plugin = new MomentTimezoneDataPlugin({
3 startYear: currentYear - 2,
4 endYear: currentYear + 10,
5});
1const plugin = new MomentTimezoneDataPlugin({ 2 matchZones: 'America/New_York', 3});
1const plugin = new MomentTimezoneDataPlugin({ 2 // Includes 'Pacific/Auckland' and 'Pacific/Chatham' 3 matchCountries: 'NZ', 4});
1const plugin = new MomentTimezoneDataPlugin({ 2 matchZones: /Europe\/(Belfast|London|Paris|Athens)/, 3 startYear: 2000, 4 endYear: 2030, 5});
1const plugin = new MomentTimezoneDataPlugin({ 2 matchZones: [/^Australia/, 'Pacific/Auckland', 'Etc/UTC'], 3 startYear: 2000, 4 endYear: 2030, 5});
1const plugin = new MomentTimezoneDataPlugin({ 2 matchCountries: ['US', 'CA'], 3 startYear: 2000, 4 endYear: 2030, 5});
Moment Timezone has the concept of zone links, which are simple aliases from one zone name to another. These roughly match the Zone
and Link
definitions in the underlying IANA time zone database files. (But they don't exactly match the tzdb links, for complicated reasons.)
This plugin will automatically include linked zones in some circumstances:
matchZones
option will include only zones that match the provided value(s). If a zone is defined in Moment Timezone as a link, then the zone it points to is also included.matchCountries
option chooses which zones are included based on the countries data provided by Moment Timezone.
zone.tab
and zone1970.tab
files in the IANA time zone database.matchZones
, there is some link handling here, but only if the links are found in that primary countries
list.America/Puerto_Rico
has 20 links pointing to it). This has become more prevalent in recent years as the tzdb has been merging many zones together across country boundaries.For example, the zone US/Eastern
is a backwards-compatibility link to America/New_York
. Because it's a deprecated name, US/Eastern
doesn't appear in the zone*.tab
files, but America/New_York
does. Therefore:
matchZones: 'US/Eastern'
will include US/Eastern
and America/New_York
.matchZones: 'America/New_York
will include only America/New_York
.matchCountries: 'US'
will include America/New_York
, America/Detroit
, America/Los_Angeles
, etc., but will not include US/Eastern
.No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
1 existing vulnerabilities detected
Details
Reason
2 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 1
Reason
Found 1/17 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More