Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
moment-timezone-data-webpack-plugin
Advanced tools
Remove unneeded data from moment-timezone in a webpack build
The moment-timezone-data-webpack-plugin is a Webpack plugin designed to optimize the inclusion of moment-timezone data in your Webpack bundles. It allows you to include only the timezone data you need, reducing the overall bundle size and improving performance.
Include Specific Timezones
This feature allows you to include only specific timezones in your Webpack bundle. In this example, only the data for 'America/New_York' and 'Europe/London' timezones will be included.
const MomentTimezoneDataPlugin = require('moment-timezone-data-webpack-plugin');
module.exports = {
plugins: [
new MomentTimezoneDataPlugin({
matchZones: 'America/New_York|Europe/London'
})
]
};
Include Timezones by Region
This feature allows you to include all timezones within a specific region. In this example, all timezones in the 'America' region from the year 2000 to 2030 will be included.
const MomentTimezoneDataPlugin = require('moment-timezone-data-webpack-plugin');
module.exports = {
plugins: [
new MomentTimezoneDataPlugin({
startYear: 2000,
endYear: 2030,
matchZones: 'America/*'
})
]
};
Include Timezones by Year Range
This feature allows you to include timezone data for a specific range of years. In this example, timezone data from the year 2010 to 2020 will be included.
const MomentTimezoneDataPlugin = require('moment-timezone-data-webpack-plugin');
module.exports = {
plugins: [
new MomentTimezoneDataPlugin({
startYear: 2010,
endYear: 2020
})
]
};
The moment-timezone package provides timezone support for moment.js. It includes all timezone data by default, which can lead to larger bundle sizes. Unlike moment-timezone-data-webpack-plugin, it does not offer built-in Webpack optimizations to reduce bundle size by including only specific timezones.
The date-fns-tz package is an extension for date-fns that provides timezone support. It is more modular and tree-shakeable compared to moment-timezone, but it does not offer the same level of integration with Webpack for optimizing timezone data inclusion as moment-timezone-data-webpack-plugin.
Luxon is a modern JavaScript library for working with dates and times, built by one of the Moment.js developers. It includes built-in timezone support and is designed to be more lightweight and modular. However, it does not provide specific Webpack plugins for optimizing timezone data inclusion like moment-timezone-data-webpack-plugin.
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.
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:
npm install --save-dev moment-timezone-data-webpack-plugin
Or using yarn:
yarn add --dev moment-timezone-data-webpack-plugin
Add the plugin to your webpack config file:
const MomentTimezoneDataPlugin = require('moment-timezone-data-webpack-plugin');
module.exports = {
plugins: [
new MomentTimezoneDataPlugin({
// 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:
'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.
const currentYear = new Date().getFullYear();
const plugin = new MomentTimezoneDataPlugin({
startYear: currentYear - 2,
endYear: currentYear + 10,
});
const plugin = new MomentTimezoneDataPlugin({
matchZones: 'America/New_York',
});
const plugin = new MomentTimezoneDataPlugin({
// Includes 'Pacific/Auckland' and 'Pacific/Chatham'
matchCountries: 'NZ',
});
const plugin = new MomentTimezoneDataPlugin({
matchZones: /Europe\/(Belfast|London|Paris|Athens)/,
startYear: 2000,
endYear: 2030,
});
const plugin = new MomentTimezoneDataPlugin({
matchZones: [/^Australia/, 'Pacific/Auckland', 'Etc/UTC'],
startYear: 2000,
endYear: 2030,
});
const plugin = new MomentTimezoneDataPlugin({
matchCountries: ['US', 'CA'],
startYear: 2000,
endYear: 2030,
});
1.5.1 – 2022-08-19
FAQs
Remove unneeded data from moment-timezone in a webpack build
The npm package moment-timezone-data-webpack-plugin receives a total of 115,629 weekly downloads. As such, moment-timezone-data-webpack-plugin popularity was classified as popular.
We found that moment-timezone-data-webpack-plugin demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.