-
Notifications
You must be signed in to change notification settings - Fork 119
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
Rollup treeshaking and tslib #498
Conversation
Codecov Report
@@ Coverage Diff @@
## master #498 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 95 95
Lines 1214 1214
Branches 225 225
=====================================
Hits 1214 1214 Continue to review full report at Codecov.
|
Let me start by bowing to your willingness to dig through this stuff! Clearly, easiest thing would be to simply wait for upstream fixes to this. And while we're seeing reasonable percentage changes in package size, the packages are very small to begin with... Sounds like Stenci.js uses rollup, so we'd have to do this to help @jgravois make tiny-pretty-things. @jgravois can you try this and see if it works for you? |
@jgravois I have no idea but for absolutely no reason at all this just started working in Rollup. Can you pull down this PR and do the following:
and see if the output only has |
confirmed. 🚀 i've pushed up another commit with a little cleanup to ensure that the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i tested both your demo and in Stencil.js and confirmed that rollup's dead code elimination is kicking in and producing big savings.
thanks for all your help @patrickarlt!
In #490 @jgravois and I went deep into if it was worth creating another build in order to get tree shaking working for module aware bundlers like Parcel, webpack and Rollup. The main reason to get this working was to allow @jgravois to tree shake dependencies in Hub components which have complicated dependency trees like what he points out in #490 (comment)
In the end we got webpack and Parcel working due to their support of a
sideEffects
flag inpackage.json
. Rollup however has some issues stemming from our use oftslib
and theimportHelpers
option in TypeScript.While webpack and Parcel allow authors to mark their libraries an side-effect free with the
sideEffects
flag. Rollup take a more complicated algorithmic approach to figuring out what can be tree shaken and what cannot. Unfortunately that algorithm doesn't handle the code included intslib
very well which cases all code that userstslib
to be marked as having side effects and not tree shakable.As background TypeScript will automatically insert the
tslib
module when it needs common helpers likeassign
andextend
if theimportHelpers
option is true. This option exists to avoid duplicating helper code across multiple modules, for example when bothrequest
andUserSession
needassign
they both get the single instance fromtslib
rather then their own 150 byte (minified + gzip) function in each moduleThis issue is actually pretty well known and has a few potential fixes:
sideEffects
flag in Rollup Add support forsideEffects
hint inresolveId
rollup/rollup#2593 would let us mark our code as sife effect free so it can tree shaken.tslib
code.This PR removes our use of
tslib
allow Rollup to tree shake our modules. What follows is a brief overview of what this change would do.All modules would see a size increase in the esm modules. This is because almost every method uses the
assign
helper. This is ~150b per method after gzip. Some estimates are below:Also worth noting is that some methods still need
tslib
for some reason so those methods could never be tree shaken out.HOWEVER there are also some significant savings here:
If you use only
ApplicationSession
from auth you save ~584b. Using justsearchItems
from items saves ~1kb. So an application using justApplicationSession
andsearchItems
would save ~1.5kb but gain ~750b total (~450b fromrequest
+ 150b fromUserSession
and 150bsearchItems
) so there IS a net savings of ~750b.Almost all of our usage of
tslib
could and thus most drawbacks of this by converting code that looked like:to
This means TypeScript will no longer insert the code for its own
assign
helper. We could also add our own assign helper to the common utils and use it throughout to negate most of the tslib usage which would eliminate almost all of the penalties for taking this approach and allow us to better support Rollup users with tree shaking without waiting for a potential resolution in either rollup or tslib. If we implemented this across every method in ourUserSession
+searchItems
example above we could potentially still save the 1.5kb and only bloat modules by ~150b (request
still needs andextends
helper).@jgravois @tomwayson @dbouwman what do you think? Wait for this to be resolved upstream in either
Rollup
ortslib
or implement a fix here and rely less ontslib
?