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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix broken tree shaking for legacy parse #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

marcodejongh
Copy link

Fixes #14

Copy link

@biniek-io biniek-io left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@marcodejongh
Copy link
Author

@dkozickis @kossnocorp @silvenon Could you do us a huge favour and review and merge this? It's a relatively low-risk change that has a big impact, without this change we're not able to move forward with our date-fns upgrade, unless we remove date-fns/upgrade as a dependnecy.

@marcodejongh
Copy link
Author

Needed to fix tsconfig module resolution, wonder if that is why the old style import was used to begin with. We're now using this version in prod published as date-fns-upgrade-treeshaking@1.0.8, so should be good to go now

@silvenon
Copy link
Contributor

silvenon commented Apr 20, 2021

Could you help reviewers understand what the problem is and how these changes fixes it? I think it would greatly increase the chances of merging.

@marcodejongh
Copy link
Author

import { isDate } from 'date-fns'

Imports the whole module and doesn't get tree-shaken by Webpack.
By changing this to:

import isDate from 'date-fns/isDate'

There is no more need to tree-shaken because we're now importing from a single dedicated file.

The changes to the config were necessary to facilitate those changes as TS is a bit fickle in its module resolution logic. date-fns exports isDate on the module.exports it loses its "default" key, by enabling esModuleInterop, TS protects against this scenario, resulting in the code working.

@silvenon
Copy link
Contributor

Adding an ES build of this library seems like a more natural solution for this problem, that way people won't end up with two copies of isDate (date-fns/isDate and date-fns/esm/isDate).

@marcodejongh
Copy link
Author

@silvenon Not sure I understand? date-fns currently doesn't have an ESM build: https://unpkg.com/browse/date-fns@2.21.1/isDate/
The recommended method of importing date-fns: https://date-fns.org/docs/Getting-Started#submodules

You're right that if:

import { isDate } from 'date-fns'

Was ESM, we would need to do this change, but the fact that date-fns didn't add an ESM build up until now, leads me to believe this is an intentional decision. Arguably, we'd still want to import by submodules as this is still more reliable than tree shaking.

Copy link
Contributor

@silvenon silvenon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how to get to it with unpkg's browse feature, but date-fns/esm/isDate/index.js appears to exist, but now I realized that date-fns/esm/isDate/package.json points ESM-enabled tools to date-fns/esm/isDate/index.js anyway, so there's no problem, my bad! 😃

@silvenon
Copy link
Contributor

Only now I realized I don't have merging rights. 🤦‍♂️ 🤦‍♂️ 🤦‍♂️ @dkozickis

@marcodejongh
Copy link
Author

marcodejongh commented Apr 21, 2021

Ah it does have ESM, missed that...

I just noticed that date-fns/upgrade didn't tree shake properly when bundle analysing, I'm not quite sure why it doesn't tree shake https://unpkg.com/date-fns@2.21.1/esm/index.js looks fine, but Webpack can be a bit fickle sometimes, it's better to just use the submodule import instead.

@silvenon
Copy link
Contributor

Because @date-fns/upgrade only has a CJS build, so that's where the tree-shaking potential stops. 😕 You'll need to edit Makefile to produce two builds. To create an ESM build you can probably just pass flags to tsc, and make sure to add module field to package.json. Let me know if you need additional help.

@marcodejongh
Copy link
Author

Adding ESM sounds like a good follow-up task on this project, but has significantly bigger scope and as a result a higher risk profile.
This PR is significantly less risky and fixes the more immediate problem of pulling in all of date-fns when trying to use this upgrade module.

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

Successfully merging this pull request may close these issues.

Bundle Size: not tree shakable, date-fns not peer dependency
3 participants