javascript icon indicating copy to clipboard operation
javascript copied to clipboard

Improve class exports for better tree-shaking

Open pedrovsp opened this issue 6 years ago • 3 comments

On the issue #117 , @hyanmandian suggested a change on the way we import our functions in order to improve the tree-shaking.

Today we import something like that: import { isValidCPF } from" @ brazilian-utils/brazilian-utils" in a tree shakable way we import something like this: import isValidCPF from" @brazilian-utils/brazilian-utils/is-valid-cpf ";, so the babel plugin will help just replacing @brazilian-utils / brazilian-utils to @brazilian-utils/brazilian-utils. You can see something like that in date-fns or lodash libraries.`

I opened this issue so we can better discuss how to proceed with this feature.

pedrovsp avatar Oct 01 '20 15:10 pedrovsp

The statement about a "better tree-shakable way to export" is not really true, with modern bundlers (since webpack 4.0, that as released in fev 2018) we can safely use named exports from main package file

The sideEffects: false in bazilian-utils package.json inform bundlers that it is safe to remove unsed named exports

https://developers.google.com/web/fundamentals/performance/optimizing-javascript/tree-shaking#what_is_tree_shaking

See the "Exports Analysis" from date-fns in bundlephia, the named exports have a variable size

Screenshot_20201031_235307

The current @brazilian-utils/brazilian-utils package works with Tree Shaking, with a blank project that I created with just webpack@4 and @brazilian-utils/brazilian-utils installed, the Tree Shaking works

Importing isValidCPF add only 1kb to the production bundle Screenshot_20201101_001137

getCities

hexetia avatar Nov 01 '20 03:11 hexetia

About Exports Analysis of @brazilian-utils/[email protected] in bundlephobia, they erroneously shows that using a single named export result in full bundle import, that is because they look for "multi entry" package, date-fns as example send the files unbundled ("multi entry"), which offers a better analysis of the bundle, just for analysis

Selection_019

the brazilian-utils package export just the files bundled, that works with three-shaking because of sideEffects: false in package.json

https://github.com/formium/tsdx/issues/365

hexetia avatar Nov 01 '20 04:11 hexetia

WOWWW nice job @saculbr . This info that you share here, makes me think that we don't need to do anything in our project related to tree shaking. I will close this issue. Thanks @saculbr !

hyanmandian avatar Nov 03 '20 14:11 hyanmandian