Improve/fix tree-shaking
See original GitHub issueEven though we added sideEffects: false
to all packages, tree shaking still seems to be an issue.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Reduce JavaScript payloads with tree shaking - web.dev
To that end, there are techniques to improve JavaScript performance. Code splitting, is one such technique ... Tree shaking attempts to solve this...
Read more >Tree Shaking - webpack
Clarifying tree shaking and sideEffects ... The sideEffects and usedExports (more known as tree shaking) optimizations are two different things. sideEffects is ...
Read more >Tree-Shaking: A Reference Guide - Smashing Magazine
“Tree-shaking” is a must-have performance optimization when bundling JavaScript. In this article, we dive deeper on how exactly it works and ...
Read more >Improving Site Performance With Webpack Tree Shaking
In this blog post, we'll discuss our approach to improving site performance with ES6 modules and tree shaking.
Read more >Tree shaking and code splitting in webpack - LogRocket Blog
Tree shaking, also known as dead code elimination, is the practice of removing unused code in your production build. It's important to ship...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@chaance I just wasn’t sure how to add babel plugins to Parcel at the time and didn’t want to spend too much time on it but @benoitgrelard has figured out that adding
.babelrc
works fine 🙂I’ve done a couple of days of heavy digging on this…
Given we’ve all already (or about to) wind up for the end of the year, I thought I’d summarise where we’re at with this here, given that I’d rather pick it back up in Jan as it’s a decent task with lots of impacts on build tooling, etc
So here goes…
As far I can tell, we have 3 main culprits preventing us from having tree-shaking work for us:
Xxx.displayName = "Xxx";
statementsforwardRefWithAs
callsbuild setup issues
Firstly, our default build is a production build with minification turned on. This means that although, parcel adds
/*#__PURE__*/
annotations on react calls, etc (via babel), it also actually removes the comments using terser (because it minifies…)displayName
It seems static properties on functions are basically a no go for tree-shaking. This article sums it up nicely. It has to do with how optimizers like terser compress code with statics.
We can resolve this easily using a babel plugin to:
if(process.env.NODE_ENV !=== "production")
checksif (__DEV__)
checks and have a babel plugin to replace that to the above checkI have tested both approaches and they both work well.
forwardRefWithAs
The same way babel-plugin-transform-react-pure-annotations adds
/*#__PURE__*/
annotations to certain react calls (includingforwardRef
) we also need to do the same with our own customforwardRefWithAs
.Again, this is fairly easy to do with a custom babel plugin. I have done it and tested it and it works great.
Now, I also think we have other issues with our build which prevent us from having this all work nicely, it’s all a bit intermingled. For example parcel seems to eagerly replace
process.env.NODE_ENV
which means its value ends up being hardcoded in the dist files.That means this is actually forcing us to create development and production builds of each package, which we started looking into but brought a bunch of other surprises/issues.
I have pushed my current WIP to a branch called
tree-shaking-experimentation