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

Export functions instead of static methods #22

Open
dawidgarus opened this issue Jun 14, 2019 · 5 comments
Open

Export functions instead of static methods #22

dawidgarus opened this issue Jun 14, 2019 · 5 comments

Comments

@dawidgarus
Copy link

The library is quite big and it cannot be tree shaken because all of the operators are static methods.
It would be nice to have them exported from module, so unused operators could be removed from final bundle, after all we all want faster web.
Chances are you won't be using many operators like bitwise operations.
In my case, I would like to use BigInt for operations on money and I really need only basic operations.

@mathiasbynens
Copy link
Member

It's definitely possible to tree-shake static methods in theory. Is the issue that tooling doesn't support that today? Which tool are you using to perform the tree-shaking?

@dawidgarus
Copy link
Author

dawidgarus commented Jun 17, 2019

It's definitely possible to tree-shake static methods in theory.

Is it though? The shaking of static methods would come with a lot of caveats and implicit assumptions about the code that I'm not comfortable with. Despite the name, static methods are very dynamic by the nature of JS. You can extend class in inherit static methods, use the class in mixin, assign it to variable, inject the class using DI.

Which tool are you using to perform the tree-shaking?

Rollup and webpack. Neither supports tree-shaking of static methods. Not by default anyways, if there is a switch to toggle this feature I would gladly be corrected.

@ashi009
Copy link

ashi009 commented Sep 30, 2019

I think this approach is necessary to make babel transform plugin to work?

The Closure compiler will do the right thing -- though it will be a challenge to make it work for most of the random npm packages around.

I just found it awkward to use in typescript, as the type is actually JSBI instead of jsbi.BigInt and all other methods are on JSBI. And this practice seems against Google's javascript style guide.

@pauldraper
Copy link

It's definitely possible to tree-shake static methods in theory.

Only through a lot of introspection or making non-general assumptions about the code (a la Closure Compiler; though last time I used it had bugs around ES static).

E.g. class Me extends JSBI {} and now the static methods are even harder to follow.

Generally tree shaking works only at the level of export.

@danieltroger
Copy link

danieltroger commented Oct 27, 2021

Also got bitten by this today! In the end I made a function returning the whole JSBI class so that at least the import of JSBI can be tree-shaken away if it's never used. But if you'd fix it so that it doesn't have side-effects that would be great. I'm using parcel v2 and terser, google closure compiler breaks my code. (Also I think google closure compiler doesn't support es2021 input and es2021 output IIRC?)

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

No branches or pull requests

5 participants