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.

Tree shaking issue in v2.28

See original GitHub issue

I use only a few of the functions and made sure I import them as recommended for tree-shaking in the documentation :

import { parse } from 'date-fns'
import { fr } from 'date-fns/locale';

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 :

image

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 :

import parse from "date-fns/fp/parse";
import fr from 'date-fns/locale/fr';

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.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:3
  • Comments:6

github_iconTop GitHub Comments

1reaction
guillaumeprevostcommented, May 31, 2022

Thanks for the replies !

⚠️⚠️ The following workaround works, but I still believe there should be some kind of fix coming from date-fns itself to avoid these messy fixes and allow simply importing ⚠️⚠️

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 date-fns-functions.js where I re-export the functions used in the project like this :

export { default as parseISO } from 'date-fns/parseISO';
export { default as formatDistanceToNow } from 'date-fns/formatDistanceToNow';
...

Then, adding an alias in the Webpack config :

	resolve: {
		alias: {
			'date-fns$': require.resolve('./date-fns-functions.js')
		}
	},

In my particular case, using Nuxt.js for Webpack bundling I added this in nuxt.config.js instead :

extend (config, ctx) {
   config.resolve.alias['date-fns$'] = require.resolve('./date-fns-functions.js');
   ...
}

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.

image

0reactions
devuxercommented, May 14, 2022

Aha, I think I found out what’s causing the problem for me: https://github.com/react-component/picker/issues/232

So, ultimately, this is probably nothing to do with date-fns itself.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Parcel.js Web Application Bundler: Enabling Tree Shaking for ...
One interesting feature I came across: Tree Shaking, which means stripping out code from your dependencies that you do not import.
Read more >
date-fns : tree-shaking doesn't seem to work - Stack Overflow
Here is a fix for tree-shaking the date-fns locales that worked for me, using a Webpack alias (found the solution in this Github...
Read more >
Typescript Tutorial - Basic Project Setup and Tree Shaking
In this video I introduce you to my Typescript Boiler Plate project, which is available via my Github. This Boiler Plate provides different ......
Read more >
Tree-Shaking: A Reference Guide - Smashing Magazine
Simply put, tree-shaking means removing unreachable code (also known as dead code) from a bundle. As Webpack version 3's documentation states: “ ...
Read more >
Pick the cherry or shake the tree to prune the dead code.
The problem statement that made me dig deep into this topic and write this article is tree shaking with rc-slider.
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