jsbi icon indicating copy to clipboard operation
jsbi copied to clipboard

Export functions instead of static methods

Open dawidgarus opened this issue 5 years ago • 5 comments

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.

dawidgarus avatar Jun 14 '19 12:06 dawidgarus

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?

mathiasbynens avatar Jun 14 '19 13:06 mathiasbynens

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.

dawidgarus avatar Jun 17 '19 08:06 dawidgarus

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.

ashi009 avatar Sep 30 '19 09:09 ashi009

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.

pauldraper avatar Nov 15 '19 03:11 pauldraper

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?)

danieltroger avatar Oct 27 '21 17:10 danieltroger