question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

generatePicker imports all locales, with huge impact on bundle size

See original GitHub issue

When 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 😃

image

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:5
  • Comments:13 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
ekeijlcommented, Aug 26, 2021

Webpack alias to the rescue!

Create ./date-fns-locale.js next to your Webpack config. Here you re-export all the locales you need:

export { default as enGB } from 'date-fns/locale/en-GB';
export { default as enUS } from 'date-fns/locale/en-US';

export { default as fr } from 'date-fns/locale/fr';
// ...etc

Here is the full list.

Add an alias to your webpack.config.js:

	resolve: {
		alias: {
			'date-fns/locale$': require.resolve('./date-fns-locales.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 you import x from "date-fns/locale" will also be aliased to date-fns-locales.js.

The resulting bundle: image


Edit: if you try to use a locale that is not exported from your date-fns-locales.js, you will get the error Cannot read property 'localize' of undefined.

To use en-GB you also need to include en-US as it extends from the US locale! (I fixed this in the code above)

1reaction
devuxercommented, Jun 9, 2022

@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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found