From b778a6186e3416713d4ace99c5d7bb65741c7d89 Mon Sep 17 00:00:00 2001 From: Ferdinand Prantl Date: Sat, 17 Nov 2018 14:15:00 +0100 Subject: [PATCH 1/2] fix: Introduce a timeZone module allowing custom time zone data Load different data in different modules and reuse the custom module, which requests timezone-support with no data included. --- build/index.js | 27 +++----- build/rollup.config.js | 9 ++- docs/en/Plugin.md | 26 +++++++ package-lock.json | 6 +- package.json | 4 +- src/plugin/timeZone/1900-2050.js | 6 ++ src/plugin/timeZone/2012-2022.js | 6 ++ src/plugin/timeZone/custom.js | 112 ++++++++++++++++++++++++++++++ src/plugin/timeZone/index.js | 114 ++----------------------------- 9 files changed, 176 insertions(+), 134 deletions(-) create mode 100644 src/plugin/timeZone/1900-2050.js create mode 100644 src/plugin/timeZone/2012-2022.js create mode 100644 src/plugin/timeZone/custom.js diff --git a/build/index.js b/build/index.js index 2a8ce308d..0dae426c0 100644 --- a/build/index.js +++ b/build/index.js @@ -7,8 +7,6 @@ const configFactory = require('./rollup.config') const { promisify } = util const promisifyReadDir = promisify(fs.readdir) -const promisifyReadFile = promisify(fs.readFile) -const promisifyWriteFile = promisify(fs.writeFile) const formatName = n => n.replace(/\.js/, '').replace('-', '_') @@ -17,20 +15,6 @@ async function build(option) { await bundle.write(option.output) } -async function addLimitedTimeZonePluginVersions() { - const originalFile = path.join(__dirname, '../plugin/timeZone.js') - const originalContent = await promisifyReadFile(originalFile, { encoding: 'utf-8' }) - const limitedVersions = ['1900-2050', '2012-2022'] - for (let i = 0; i < limitedVersions.length; ++i) { // eslint-disable-line no-plusplus - const limitedVersion = limitedVersions[i] - const limitedFile = path.join(__dirname, `../plugin/timeZone-${limitedVersion}.js`) - const limitedContent = originalContent.replace('require("timezone-support")', - `require("timezone-support/dist/index-${limitedVersion}")`) - // eslint-disable-next-line no-await-in-loop - await promisifyWriteFile(limitedFile, limitedContent) - } -} - (async () => { try { const locales = await promisifyReadDir(path.join(__dirname, '../src/locale')) @@ -51,12 +35,19 @@ async function addLimitedTimeZonePluginVersions() { })) }) + const timeZoneVariants = ['custom', '1900-2050', '2012-2022'] + timeZoneVariants.forEach((moduleName) => { + build(configFactory({ + input: `./src/plugin/timeZone/${moduleName}`, + fileName: `./plugin/timeZone-${moduleName}.js`, + name: 'dayjs_plugin_timeZone' + })) + }) + build(configFactory({ input: './src/index.js', fileName: './dayjs.min.js' })) - - addLimitedTimeZonePluginVersions() } catch (e) { console.error(e) // eslint-disable-line no-console } diff --git a/build/rollup.config.js b/build/rollup.config.js index b1d54bf0c..4f49cca3f 100644 --- a/build/rollup.config.js +++ b/build/rollup.config.js @@ -7,7 +7,9 @@ module.exports = (config) => { input: { input, external: [ - 'dayjs-ext', 'fast-plural-rules', 'timezone-support' + 'dayjs-ext', 'fast-plural-rules', 'timezone-support/dist/lookup-convert', + 'timezone-support/dist/data', 'timezone-support/dist/data-1900-2050', + 'timezone-support/dist/data-2012-2022' ], plugins: [ babel({ @@ -23,7 +25,10 @@ module.exports = (config) => { globals: { 'dayjs-ext': 'dayjs', 'fast-plural-rules': 'fastPluralRules', - 'timezone-support': 'timezone-support' + 'timezone-support/dist/lookup-convert': 'timezone-support', + 'timezone-support/dist/data': 'timezone-data', + 'timezone-support/dist/data-1900-2050': 'timezone-data-1900-2050', + 'timezone-support/dist/data-2012-2022': 'timezone-data-2012-2022' }, sourcemap: true } diff --git a/docs/en/Plugin.md b/docs/en/Plugin.md index 37e41d59b..6cc47109d 100644 --- a/docs/en/Plugin.md +++ b/docs/en/Plugin.md @@ -277,6 +277,32 @@ Day.js uses an embedded `Date` object. This object supports only local time zone * The time zone parameter in the constructor is meant only for converting the parsed input string correctly to UTC. The embedded `Date` object will be initialised with UTC and offer the local time zone representation as usual. The original time zone offset will not be remembered. It is usually not important, because dates should be rendered consistently in user's time zone; not in various time zones, which their string sources referred to. * The time zone parameter in the `format` method will extract the date parts (year, month, ...) from the embedded `Date` object in UTC and convert them to the specified time zone, before producing the output string. +#### Package Size + +The plugin includes all available time zone data in the main module `dayjs-ext/plugin/timeZone`. If you can afford limit the support for recent years only, you can reduce the size of your package, if you bundle `dayjs-ext` with other sources using rollup or webpack: + +```txt +Full IANA TZ data: 923 KB minified, 33.3 KB gzipped +Data for 1900-2050: 200 KB minified, 23.3 KB gzipped +Data for 2012-2022: 27 KB minified, 6.5 KB gzipped +``` + +Modules with limited time zone data are exposed as `dayjs-ext/plugin/timeZone-1900-2050` and `dayjs-ext/plugin/timeZone-2012-2022`. A custom module with different time zone data can be used via `dayjs-ext/plugin/timeZone-custom`: + +```js +import dayjs from 'dayjs-ext' +import timeZonePlugin from 'dayjs-ext/plugin/timeZone-custom' + +import { populateTimeZones } from 'timezone-support/dist/lookup-convert' +import timeZoneData from './data-1970-2025' + +populateTimeZones(timeZoneData) + +dayjs.extend(timeZonePlugin) +``` + +When `dayjs-ext` is loaded in the browser as described below, custom data can be loaded by using the [interface of `timezone-support` for limited data loading](https://github.com/prantlf/timezone-support/blob/master/docs/usage.md#limit-the-loaded-time-zone-data). + #### Installation This plugin has a dependency on the [`timezone-support`](https://www.npmjs.com/package/timezone-support) NPM module. If you are going to use it on a web page directly, add its script to your section of `