question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improve/fix tree-shaking

See original GitHub issue

Even though we added sideEffects: false to all packages, tree shaking still seems to be an issue.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
jjenzzcommented, Dec 22, 2020

@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 🙂

1reaction
benoitgrelardcommented, Dec 21, 2020

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:

  • the way our builds are setup
  • Xxx.displayName = "Xxx"; statements
  • forwardRefWithAs calls

build 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:

  • wrap our displayName statements in if(process.env.NODE_ENV !=== "production") checks
  • or manually wrap them in if (__DEV__) checks and have a babel plugin to replace that to the above check

I 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 (including forwardRef) we also need to do the same with our own custom forwardRefWithAs.

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.


NOTE: I have tested all of the above only with @radix-ui/react-avatar package so it was easier to identify the issues. I don’t think we’re guaranteed that these are the only issues. I am pretty sure for examples at least a few other calls will need to be annotated but at this point then it’s pretty simple to do once we have the whole infra in place (ie. the custom babel plugin working, etc).

I have pushed my current WIP to a branch called tree-shaking-experimentation

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found