generatePicker imports all locales, with huge impact on bundle size
See original GitHub issueWhen swapping out moment.js with either day.js or date-fns, the generatePicker
function imports all of the locales.
This results in a huge bundle and totally not needed in a lot of use cases. dateFns.ts#L26
Is there a way to get around this? It seems a little silly to have pre-loaded all locales when you only need one 😃
Issue Analytics
- State:
- Created 3 years ago
- Reactions:5
- Comments:13 (1 by maintainers)
Top Results From Across the Web
reactjs - ant design - huge imports - Stack Overflow
At the moment, a huge part of antd dist is SVG icons. ... I reduced my bundle size by 500KB by editing config-override.js...
Read more >How to put your Webpack bundle on a diet - Contentful
The bundle now takes up an enormous 744.4 KiB. This is because Webpack imports every Moment locale, regardless of whether it's used, by...
Read more >DatePicker - Ant Design
The input box comes in three sizes. middle will be used if size is omitted. expand code ... import locale from 'antd/es/date-picker/locale/zh_CN';.
Read more >warning: [antd: form.item] `name` is only used for validate ... - You.com
You can generate Picker components for custom date libraries using the generate ... We import all the icon file into antd which makes...
Read more >Ant Design Component Customization and Bundle Optimization
You will also learn how to significantly decrease bundle size, ... the massive replace tool of some IDE, we change every import path...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Webpack alias to the rescue!
Create
./date-fns-locale.js
next to your Webpack config. Here you re-export all the locales you need:Here is the full list.
Add an alias to your webpack.config.js:
Mind the
$
in the name! As explained in the Webpack alias documentation: this will only match imports ending in"date-fns/locale"
and ensures that we can still import from"date-fns/locale/en-GB"
. Beware that other places youimport x from "date-fns/locale"
will also be aliased todate-fns-locales.js
.The resulting bundle:
Edit: if you try to use a locale that is not exported from your
date-fns-locales.js
, you will get the errorCannot read property 'localize' of undefined
.To use
en-GB
you also need to includeen-US
as it extends from the US locale! (I fixed this in the code above)@matheusgrieger ,
That’s amazing!
But it made me realize that even though I’m using Antd/rc-picker, the locales seem to be getting loaded by date-fns itself.
Any thoughts on what could be causing this? I’m hoping I can use a similar hack to fix that, but I’m not sure where to look.
EDIT No, I’m wrong. I forgot to do a build before analyzing. Applying your patch reduces my bundle size by almost 1 MB! Thanks for this solution!