date-fns-tz
date-fns-tz copied to clipboard
Bundling ESM exports using `vite` library mode doesn't work correctly
Hi! 👋
Firstly, thanks for your work on this project! 🙂
I was facing an issue on my UI library that uses date-fns-tz/esm/*
.
My library setup uses vite
with preserveModules
option set to true
.
The bundling was successful but during runtime the CJS libs that are imported within were throwing error.
Related:
- https://github.com/marnusw/date-fns-tz/issues/193
- https://github.com/marnusw/date-fns-tz/issues/200
After breaking my head for 2 days on this issue, today I used patch-package to patch [email protected]
for the project I'm working on.
I replaced the CJS imports with ESM imports under the date-fns-tz/esm
directory.
Here is the diff that solved my problem:
diff --git a/node_modules/date-fns-tz/esm/format/index.js b/node_modules/date-fns-tz/esm/format/index.js
index 440e5a9..e1b6a3c 100644
--- a/node_modules/date-fns-tz/esm/format/index.js
+++ b/node_modules/date-fns-tz/esm/format/index.js
@@ -1,4 +1,4 @@
-import dateFnsFormat from 'date-fns/format/index.js'
+import dateFnsFormat from 'date-fns/esm/format/index.js'
import formatters from './formatters/index.js'
import toDate from '../toDate/index.js'
diff --git a/node_modules/date-fns-tz/esm/formatInTimeZone/index.js b/node_modules/date-fns-tz/esm/formatInTimeZone/index.js
index bbb2c4e..6763dc1 100644
--- a/node_modules/date-fns-tz/esm/formatInTimeZone/index.js
+++ b/node_modules/date-fns-tz/esm/formatInTimeZone/index.js
@@ -1,4 +1,4 @@
-import cloneObject from 'date-fns/_lib/cloneObject/index.js'
+import cloneObject from 'date-fns/esm/_lib/cloneObject/index.js'
import format from '../format/index.js'
import utcToZonedTime from '../utcToZonedTime/index.js'
diff --git a/node_modules/date-fns-tz/esm/toDate/index.js b/node_modules/date-fns-tz/esm/toDate/index.js
index 6acea0e..d4340cc 100644
--- a/node_modules/date-fns-tz/esm/toDate/index.js
+++ b/node_modules/date-fns-tz/esm/toDate/index.js
@@ -1,5 +1,5 @@
-import toInteger from 'date-fns/_lib/toInteger/index.js'
-import getTimezoneOffsetInMilliseconds from 'date-fns/_lib/getTimezoneOffsetInMilliseconds/index.js'
+import toInteger from 'date-fns/esm/_lib/toInteger/index.js'
+import getTimezoneOffsetInMilliseconds from 'date-fns/esm/_lib/getTimezoneOffsetInMilliseconds/index.js'
import tzParseTimezone from '../_lib/tzParseTimezone/index.js'
import tzPattern from '../_lib/tzPattern/index.js'
diff --git a/node_modules/date-fns-tz/esm/zonedTimeToUtc/index.js b/node_modules/date-fns-tz/esm/zonedTimeToUtc/index.js
index 4451b67..4ad4f50 100644
--- a/node_modules/date-fns-tz/esm/zonedTimeToUtc/index.js
+++ b/node_modules/date-fns-tz/esm/zonedTimeToUtc/index.js
@@ -1,4 +1,4 @@
-import cloneObject from 'date-fns/_lib/cloneObject/index.js'
+import cloneObject from 'date-fns/esm/_lib/cloneObject/index.js'
import toDate from '../toDate/index.js'
import tzPattern from '../_lib/tzPattern/index.js'
import tzParseTimezone from '../_lib/tzParseTimezone/index.js'
This issue body was partially generated by patch-package.
Hope this workaround helps others.