marnusw / date-fns-tz

Complementary library for date-fns v2 adding IANA time zone support
MIT License
1.06k stars 116 forks source link

Feature request: Export import and require from 'date-fns-tz' #224

Closed mcshaz closed 1 year ago

mcshaz commented 1 year ago

Having both the common JS and EcmaScript modules is great. A problem is that when consuming libraries (which are libraries themselves) wish to export both formats with a transpiling workflow, transpiled code will have been forced to selected either commonJS or ES modules. That is:

pre transpile: import { utcToZonedTime } from 'date-fns-tz/esm' // this will be the ESM code & doesn't need transpiling post transpile to CJS var utcToZonedTime = require('date-fns-tz/esm') // Error here - not CJS

The solution would seem to be replacing the package.json line

"exports": {
    ".": "./index.js",

with

"exports": {
    ".": {
      "import": "./index.js",
      "require": "./esm/index.js"
    }

by leaving all the other exports, ~I do not believe this would be a breaking change~ - it will also require something like "engines":{"node":">=14.0.0"} - I am not sure of the lowest version of Node that began supported conditional exports, but it is around 14. Note Node 13 is no longer supported, so this should not be a huge change.

marnusw commented 1 year ago

Fixed via #212