marnusw / date-fns-tz

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

ESM date-fns-tz/format imports CJS date-fns/format #200

Open nskazki opened 1 year ago

nskazki commented 1 year ago

https://github.com/marnusw/date-fns-tz/blob/4ced7b8b0a5aad5b08fc3a44476242c82bc3272d/src/format/index.js#L1

For dateFnsFormat to be imported as EMS, not CJS, it should have been written like so

import { format as dateFnsFormat } from 'date-fns'
nskazki commented 1 year ago

Other ESM files importing CJS functions:

  1. date-fns-tz/esm/formatInTimeZone/index.js
  2. date-fns-tz/esm/fp/format/index.js
  3. date-fns-tz/esm/fp/formatInTimeZone/index.js
  4. date-fns-tz/esm/fp/formatInTimeZoneWithOptions/index.js
  5. date-fns-tz/esm/fp/formatWithOptions/index.js
  6. date-fns-tz/esm/fp/getTimezoneOffset/index.js
  7. date-fns-tz/esm/fp/toDate/index.js
  8. date-fns-tz/esm/fp/toDateWithOptions/index.js
  9. date-fns-tz/esm/fp/utcToZonedTime/index.js
  10. date-fns-tz/esm/fp/utcToZonedTimeWithOptions/index.js
  11. date-fns-tz/esm/fp/zonedTimeToUtc/index.js
  12. date-fns-tz/esm/fp/zonedTimeToUtcWithOptions/index.js
  13. date-fns-tz/esm/toDate/index.js
  14. date-fns-tz/esm/zonedTimeToUtc/index.js
tomstolarczuk commented 1 year ago

I have the same problem. Getting CommonJS or AMD dependencies can cause optimization bailouts. in Angular.

marnusw commented 1 year ago

@nskazki The problem is not so much the syntax for importing ESM, but the fact that the date-fns ESM section of the project doesn't specify type: module. That means when you try to build date-fns-tz importing ESM files files from the default type: commonjs project into a type: module project you also have problems.

Perhaps I'm wrong and I missed something. I can go back and take another look when I have time. But I don't think it's as simple to solve this as you suggest here.

If you have the time to see whether you can import ESM from date-fns and get this library to build please create a PR; I'm not sure I'll get round to it in the too near future.

nskazki commented 1 year ago

Well, it would have solved the problem for me with my Webpack config.

marnusw commented 1 year ago

I won't argue with you. Seeing as this is a big deal for you at the moment please submit the changes in a PR. Please just ensure you can run the build and package commands successfully beforehand. If that works, you will have solved a painpoint for many!

nskazki commented 1 year ago

Okay, will do. Thanks for being so open to suggestions!

A revoked suggestion About the `exports` field in `package.json`. It throws Webpack off, making it ignore the `module` field and export CJS files the `exports` specifies. As of recently, Node supports `type: 'module'`, https://nodejs.org/api/packages.html#type. When you have time, please check if you still need the `exports` to make this package compatible with the NodeJS version of ES imports.
nskazki commented 1 year ago

@marnusw, I haven't succeeded with https://github.com/marnusw/date-fns-tz/pull/204.

date-fns-tz that imports ES-only modules from date-fns only works with Webpack because, unlike NodeJS, Webpack recognizes the module field in package.json. I don't know how to make the patched date-fns-tz work with NodeJS and Webpack without patching date-fns.

marnusw commented 1 year ago

Thank you for trying @nskazki. That was my unfortunate finding as well. You can help by applying some pressure over at date-fns. 😄