Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bundling ESM exports using vite library mode doesn't work correctly #219

Open
sasivarnan opened this issue Dec 20, 2022 · 0 comments
Open

Comments

@sasivarnan
Copy link

sasivarnan commented Dec 20, 2022

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:

  1. node_modules\date-fns-tz\esm\formatInTimeZone\index.js depends on 'date-fns/_lib/cloneObject/index.js'. CommonJS or AMD dependencies can cause optimization bailouts in angular project聽#193
  2. ESM date-fns-tz/format imports CJS date-fns/format聽#200

After breaking my head for 2 days on this issue, today I used patch-package to patch date-fns-tz@1.3.7 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant