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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tree shaking issue in v2.28 #3004
Comments
I'm running into this exact problem. I started a discussion before stumbling upon this issue. |
Same issue, would be great to see some feedback on this. Thank you! |
Solution for us was to use import enUS from 'date-fns/locale/en-US/index.js';
import enCA from 'date-fns/locale/en-CA/index.js'; But we use ncc, which might be smarter than others. |
@simlu , Alas, does not work with Create-React-App. I only need en-US, which is the default according to the docs, so I was previously not importing any locales. I tried specifically importing en-US anyway, just to see if that made a difference, but it did not. I'm still looking at about 900K for the date-fns portion of the bundle. |
Aha, I think I found out what's causing the problem for me: So, ultimately, this is probably nothing to do with date-fns itself. |
Thanks for the replies !
Regarding the locales, the workaround given here seems to work for me. It solved part of the problem for me and greatly reduced the bundle size. I've tried to use the same approach but haven't worked out how to achieve the same result for the actual date-fns functions. ⛔ Following did not work for the functions Created a file called
Then, adding an alias in the Webpack config :
In my particular case, using Nuxt.js for Webpack bundling I added this in nuxt.config.js instead :
This seemed to work fine in the bundle generation (I can see only the functions I import) BUT at runtime I get errors saying the functions are not defined. |
I use esbuild, problem is still here date-fns@2.20 is okay |
date-fns@2.27.0 works as well |
Can confirm that 2.27.0 is properly tree-shaked, later versions aren't. |
I use only a few of the functions and made sure I import them as recommended for tree-shaking in the documentation :
The code itself works fine, but when I look at the resulting JS bundle, the entire library (date-fns/esm) along with all of the locales are bundled, resulting in ~1,49mb > parsed 607kb > Gzipped 118kb. I use NuxtJS (bundling with webpack) and the latest date-fns (2.28.0 as of today).
Here is a picture of the bundle generated :
I initially posted my issue on StackOverflow here for the issue and tried several solutions from these older issues #2207 , #2589 and from these SO answers
Specifically, this is not working :
Unfortunately, no matter how I import the functions, the bundle keeps including all functions and all locales, so I am starting to suspect it may come from the date-fns library itself.
The text was updated successfully, but these errors were encountered: