Skip to content

Commit

Permalink
feat: Support ESM properly
Browse files Browse the repository at this point in the history
* Build ESM output to dist
* Announce ESM exports in package.json
* Add exports for lookup-convert, parse-format, data and limited data
* Replace `commander` with a hand-coded command-line parsing

* Upgrade dev dependencies
* Replace npm with pnpm (worked slower)
* Replace cpy-cli with cp.js (started to create wrong paths)
* Replace eslint with denolint (worked slower)
* Replace coveralls with codecov (they cancel accounts)
* Replace travis with github actions (more work to maintain)

BREAKING CHANGE: The initial ESM support exposed the sources at paths like `src/index.js` and worked only if you used a bundler. **The proper ESM support** has been provided by files built to paths like `dist/index.mjs`, which are mapped to export modules in `package.json`. For example, if you imported the main module like this:

    import { getZonedTime } from ./node_modules/timezone-support/src/index.js

Change it to this:

    import { getZonedTime } from timezone-support

See also sections "Loading" and "Modules" in `API.md`.

**Replacing `commander` in `create-timezone-data`** with a hand-coded command-line parsing should not affect anybody, because the command-line format did not change, but there might be some undetected difference.

Declaring export modules in `package.json` **works reliably since Node.js 14.8**, but the rest of the source code and loading the files from the `dist` directory directly should work since Node.js 6. The script `create-timezone-data` needs the `fs/promises` implementation, which was introduced in Node.js 14.
  • Loading branch information
prantlf committed Dec 4, 2022
1 parent 986ddc4 commit 04afbba
Show file tree
Hide file tree
Showing 37 changed files with 5,015 additions and 12,213 deletions.
1 change: 0 additions & 1 deletion .coveralls.yml

This file was deleted.

5 changes: 5 additions & 0 deletions .denolint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"tags": ["recommended"]
}
}
1 change: 0 additions & 1 deletion .eslintrc.yml

This file was deleted.

46 changes: 46 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Test or Release

on:
- push
- pull_request

jobs:
test-or-release:
runs-on: ubuntu-latest
steps:
- name: Checkout Sources
uses: actions/checkout@v2
- name: Install Node
uses: actions/setup-node@v2
with:
node-version: 'lts/*'
registry-url: 'https://registry.npmjs.org'
- name: Install PNPM
uses: pnpm/action-setup@v2
with:
version: '>=6'
run_install: |
- args: [--frozen-lockfile, --no-verify-store-integrity]
- name: Test
run: npm test
- name: Examples
run: |
cd examples/nodejs
pnpm i --frozen-lockfile --no-verify-store-integrity
npm test
cd ../browser-separate
pnpm i --frozen-lockfile --no-verify-store-integrity
npm test
cd ../browser-bundled
pnpm i --frozen-lockfile --no-verify-store-integrity
npm test
- name: Coverage
uses: codecov/codecov-action@v2
- name: Publish
uses: cycjimmy/semantic-release-action@v2
with:
semantic_version: 18
branches: master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ src/lookup/data*.js
src/index-*.d.ts
src/lookup/data-*.d.ts
test/browser
test/typings.test.js
test/types.test.js
10 changes: 0 additions & 10 deletions .npmignore

This file was deleted.

19 changes: 0 additions & 19 deletions .travis.yml

This file was deleted.

1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"search.exclude": {
"**/src/lookup/data*.js": true,
"**/dist/**": true,
"**/out/**": true,
"**/test/browser/**": true,
"**/test/typings.test.js": true
}
Expand Down
51 changes: 19 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
# Time Zone Support
[![NPM version](https://badge.fury.io/js/timezone-support.png)](http://badge.fury.io/js/timezone-support)
[![Build Status](https://travis-ci.org/prantlf/timezone-support.png)](https://travis-ci.org/prantlf/timezone-support)
[![Coverage Status](https://coveralls.io/repos/github/prantlf/timezone-support/badge.svg?branch=master)](https://coveralls.io/github/prantlf/timezone-support?branch=master)

[![Latest version](https://img.shields.io/npm/v/timezone-support)
![Dependency status](https://img.shields.io/librariesio/release/npm/timezone-support)
](https://www.npmjs.com/package/timezone-support)
[![Coverage](https://codecov.io/gh/prantlf/timezone-support/branch/master/graph/badge.svg)](https://codecov.io/gh/prantlf/timezone-support)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/9f1034029c0747a980cd49f64f16338b)](https://www.codacy.com/app/prantlf/timezone-support?utm_source=github.com&utm_medium=referral&utm_content=prantlf/timezone-support&utm_campaign=Badge_Grade)
[![Dependency Status](https://david-dm.org/prantlf/timezone-support.svg)](https://david-dm.org/prantlf/timezone-support)
[![devDependency Status](https://david-dm.org/prantlf/timezone-support/dev-status.svg)](https://david-dm.org/prantlf/timezone-support#info=devDependencies)
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

[![NPM Downloads](https://nodei.co/npm/timezone-support.png?downloads=true&stars=true)](https://www.npmjs.com/package/timezone-support)

Lightweight time zone listing and date converting. Intended for adding time zone support to high-level date libraries, but also for direct application usage.

* Tiny code base - 4.6 KB minified, 1.7 KB gzipped. Do not pack unnecessary weight in your application.
* Packed time zone data - 924 KB minified, 33.6 KB gzipped. Single time zones are unpacked on demand.
* Smaller bundles of code with limited data - 1900-2050 (206 kB minified, 25.4 kB gzipped), 1970-2038 (141 kB minified, 15.8 kB gzipped) and 2012-2022 (31.3 KB minified, 8.25 kB gzipped).
* Generated from the official time zone database version 2019a. Canonical time zone names, aliases, UTC offsets, and daylight-saving time changes.
* Generated from the official time zone database version 2022f. Canonical time zone names, aliases, UTC offsets, and daylight-saving time changes.
* ESM, UMD and CJS module formats provided.
* Minimal interface for time zone lookup and conversions. Parsing, formatting and manipulating dates is usually the task for a higher-level date library.

**Attention**: exported identifiers in vanilla browser modules changed in the version 2.0.0. See the [migration guide] for more information.
Expand Down Expand Up @@ -56,13 +55,11 @@ const nativeDate = new Date(getUnixTime(berlinTime, berlin))

## Installation and Getting Started

This module can be installed in your project using [NPM] or [Yarn]. Make sure, that you use [Node.js] version 6 or newer.

```sh
$ npm i timezone-support --save
```
This module can be installed in your project using [NPM], [PNPM] or [Yarn]. Make sure, that you use [Node.js] version 14.8 or newer.

```sh
$ npm i timezone-support
$ pnpm i timezone-support
$ yarn add timezone-support
```

Expand All @@ -72,7 +69,7 @@ Functions are exposed as named exports from the package modules, for example:
const { findTimeZone, getZonedTime } = require('timezone-support')
```

You can read more about the [module loading](./docs/API.md#loading) in other environments, like with ES6 or in web browsers. [Usage scenarios](./docs/usage.md#usage-scenarios) demonstrate applications of this library in typical real-world scenarios. [Design concepts](./docs/design.md#design-concepts) explain the approach to time zone handling taken by tni library and types of values used ion the interface. [Generating custom time zone data](./docs/usage.md#generate-custom-time-zone-data) will allow you to save the overall package size by limiting the supported year span. Finally, the [API reference](./docs/API.md#api-reference) lists all functions with a description of their functionality.
You can read more about the [module loading] in other environments, like with ES6 or in web browsers. [Usage scenarios] demonstrate applications of this library in typical real-world scenarios. [Design concepts] explain the approach to time zone handling taken by tni library and types of values used ion the interface. [Generating custom time zone data] will allow you to save the overall package size by limiting the supported year span. Finally, the [API reference] lists all functions with a description of their functionality.

You can see [complete sample applications] too, which can help you start with integration of this library.

Expand All @@ -85,34 +82,24 @@ You can see [complete sample applications] too, which can help you start with in

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

## Release History

* 2018-06-11 v2.0.2 Upgrade the time zone database to the version 2019a.
* 2018-06-11 v2.0.1 Default to midnight, if the time part of a date is missing.
* 2018-06-10 v2.0.0 Use proper identifiers in vanilla browser modules.
* 2018-11-17 v1.8.0 Include time zone data for years 1970-2038.
* 2018-11-17 v1.7.0 Include full time zone data separately loadable.
* 2018-11-06 v1.6.0 Upgrade the time zone database to the version 2018g.
* 2018-10-08 v1.5.5 Fix compatibility with IE. Thanks, [Andrii](https://github.com/AndriiDidkivsky)!
* 2018-10-06 v1.5.0 Add TypeScript export declarations.
* 2018-09-30 v1.4.0 Add limited data for just the current decade - years 2012-2022.
* 2018-09-18 v1.3.0 Maintain the property dayOfWeek in the time object.
* 2018-09-16 v1.2.0 Add a new getUTCOffset method for more lightweight integrations.
* 2018-09-03 v1.1.0 Set the property epoch to the time object.
* 2018-09-02 v1.0.0 Initial release

## License

Copyright (c) 2018-2019 Ferdinand Prantl
Copyright (c) 2018-2022 Ferdinand Prantl

Licensed under the MIT license.

[Node.js]: http://nodejs.org/
[NPM]: https://www.npmjs.com/
[RequireJS]: https://requirejs.org/
[PNPM]: https://pnpm.io/
[Yarn]: https://yarnpkg.com/
[Day.js]: https://github.com/iamkun/dayjs
[date-fns]: https://github.com/date-fns/date-fns
[timeZone plugin]: https://github.com/prantlf/dayjs/blob/combined/docs/en/Plugin.md#timezone
[date-fns-timezone]: https://github.com/prantlf/date-fns-timezone
[migration guide]: docs/migration.md#migration-from-1x-to-2x
[complete sample applications]: examples#readme
[module loading]: ./docs/API.md#loading
[Usage scenarios]: ./docs/usage.md#usage-scenarios
[Design concepts]: ./docs/design.md#design-concepts
[Generating custom time zone data]: ./docs/usage.md#generate-custom-time-zone-data
[API reference]: ./docs/API.md#api-reference
58 changes: 0 additions & 58 deletions bin/create-timezone-data

This file was deleted.

102 changes: 102 additions & 0 deletions bin/create-timezone-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/usr/bin/env node

const { createTimeZoneData } = require('../util/data-creator')

function help() {
console.log(`Generates time zone data for a selected year range.
Usage: create-timezone-data [options] <first year> <last year>
Options:
-a|--all-years includes all available years
-c|--as-cjs-module format the time zone data as a CommonJS module
-d|--as-amd-module format the time zone data as an AMD module
-m|--as-module format the time zone data as a JavaScript module
-n|--umd-name <name> UMD global export name, if not "timezoneData"
-o|--output-file <file> write the time zone data to a file
-u|--as-umd-module format the time zone data as an UMD module
-V|--version print version number
-h|--help print usage instructions
Time zone data are printed on the standard output as JSON by default.
Examples:
$ create-timezone-data 2012 2022
$ create-timezone-data -m -o custom-data.js 1970 2038`)
process.exit(0)
}

const { argv } = process
const args = []
let allYears, asModule, asCjsModule, asAmdModule, asUmdModule, umdName, outputFile

for (let i = 2, l = argv.length; i < l; ++i) {
const arg = argv[i]
const match = /^(-|--)(no-)?([a-zA-Z][-a-zA-Z]*)(?:=(.*))?$/.exec(arg)
if (match) {
const parseArg = (arg, flag) => {
switch (arg) {
case 'a': case 'all-years':
allYears = flag
return
case 'c': case 'as-cjs-module':
asCjsModule = flag
return
case 'd': case 'as-amd-module':
asAmdModule = flag
return
case 'm': case 'as-module':
asModule = flag
return
case 'n': case 'umd-name':
umdName = match[4] || argv[++i]
return
case 'o': case 'output-file':
outputFile = match[4] || argv[++i]
return
case 'u': case 'as-umd-module':
recursive = flag
return
case 'V': case 'version':
console.log(require('../package.json').version)
process.exit(0)
break
case 'h': case 'help':
help()
}
console.error(`unknown option: "${arg}"`)
process.exit(1)
}
if (match[1] === '-') {
const flags = match[3].split('')
for (const flag of flags) parseArg(flag, true)
} else {
parseArg(match[3], match[2] !== 'no-')
}
continue
}
args.push(arg)
}

const [ firstYear, lastYear ] = args
if (!(firstYear && lastYear) && !allYears) help()

createTimeZoneData({
firstYear,
lastYear,
asModule,
asCjsModule,
asAmdModule,
asUmdModule,
umdName,
outputFile
})
.then(timeZoneData => {
if (!outputFile) {
console.log(timeZoneData)
}
})
.catch(error => {
console.error(error.message)
process.exitCode = 1
})
Loading

0 comments on commit 04afbba

Please sign in to comment.