You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I actually find this super interesting. What status is your PR in? How far along is the implementation and can you show some differences in bundle sizes?
I actually find this super interesting. What status is your PR in? How far along is the implementation and can you show some differences in bundle sizes?
actually it used in my project now, but no test case. the differences of bundle sizes dependes on what npm package you used, I will give some cases and this pr has example project that you can get some real differences bundle sizes by running
I actually find this super interesting. What status is your PR in? How far along is the implementation and can you show some differences in bundle sizes?
Reacted by yuhang, Hamed Soheili, Jaemin Cheun, aldewiz, Fedir Ushakov, Rajendran Nadar, Hanno J. Gödecke, damon.chen, wwwikies, José Morais Jr and 1 more
Reacted by Jules Sam. Randolph, Rajendran Nadar, Mayank Chauhan, Jesse Katsumata, Roy, William Lauzé, Maxim Chouinard, Andrew Goodale, Lorenzo Sciandra, damon.chen and 13 more
Reacted by Jules Sam. Randolph, Rajendran Nadar, Mayank Chauhan, William Lauzé, yuhang, Hamed Soheili, Jaemin Cheun, Fedir Ushakov, Shubham Kumar, Hanno J. Gödecke and 1 more
Did the metro team decide on not supporting it at all?
Has there been some official correspondence between the metro team and the rnx-kit team, where the result is, that metro won't take care of it and if devs want tree shaking they should look into rnx-kit?
Hi @hannojg, this issue was closed by the author, no PR to Metro was ever opened. I guess they didn't have time to finish it, but the work (on their fork: chang-ke#1) looked interesting.
Tree shaking isn't a priority within Meta right now but we're open to contributions - as this would be quite a complex feature, it'd be a good idea to open a discussion about approaches first.
Wasn't a big reason why Hermes was created was to reduce app start time? Rather than creating an entirely new JavaScript engine using the example above treeshaking would cut 1/3 off the bundle size (and maybe about 1/3 off the start time too without Hermes - not sure if it would change anything WITH Hermes)
I think doing this should be considered. I'm looking at my bundle with npx react-native-bundle-visualizer and seeing all sorts of things that really shouldn't be in there.
Reacted by Vodopyanov Egor, Rubén Sospedra, José Morais Jr, Erick Riva and Vladimir Timofeev
@iketw we've explored it with interest! Because Metro already only bundles explicitly imported files/modules rather than packages, the gains from tree shaking (=identifying unused imports exports and eliminating them) are less than some bundlers, and as far as we've seen, not a big win in practice, especially under Hermes where the impact is only on a small fraction of download size vs a binary, with no runtime benefit. It would be a bigger win on web, but that hasn't been Metro's focus.
We're absolutely open to explorations in this area though - I'd love to be able to say "Metro does tree shaking", but the fact is folks who've experimented with it on complex, real-world apps haven't yet found significant gains.
For what's worth, at Microsoft we have made a plugin for Metro to do treeshaking via esbuild (here) and it had led to good results in our complex real world apps (Office and Xbox to name a couple) for over two years (link to the first release). We have a whole guide around bundling enhanced by rnx-kit here.
For what's worth, at Microsoft we have made a plugin for Metro to do treeshaking via esbuild (here) and it had led to good results in our complex real world apps (Office and Xbox to name a couple) for over two years (link to the first release). We have a whole guide around bundling enhanced by rnx-kit here.
@kelset thanks for your reply. Based on @robhogan's answer, it seems that the only thing missing for full tree-shaking is unused imports, is that what the MS's plugin completes in the cycle?
Also, I'm building a RN SDK using native modules and JS code. If I use the plugin in my SDK's metro config, if the hosting app does not include the plugin, will it still tree-shake the dependencies inside my SDK?
what our plugin for Metro does is leveraging esbuild's built-in treeshaker; you can find a more detailed explanation on how it works in this discussion.
because our tool is a plugin for Metro, the treeshaking happens at the Metro bundle step level: which means that it's used when generating the jsbundle consumed by an app. So unless you use Metro to bundle your SDK that you then gets consumed by hosting apps, you won't get any treeshaking. So probably you'd want your hosting apps to add the plugin; to say it in another way, whoever does the bundling needs to add treeshaking.
we've explored it with interest! Because Metro already only bundles explicitly imported files/modules rather than packages, the gains from tree shaking (=identifying unused imports exports and eliminating them) are less than some bundlers, and as far as we've seen, not a big win in practice
I am late to the discussion, but if you are using an icon library like Font Awesome, with imports like import { faShareSquare } from "@fortawesome/pro-solid-svg-icons", then tree-shaking is a really big win to app size.
Activity
chang-ke commentedon Feb 19, 2021
@cpojer Does metro interested in it?(then review it)
[-][Feature]: support module tree shaking[/-][+]Implement module tree shaking[/+]cpojer commentedon Feb 19, 2021
I actually find this super interesting. What status is your PR in? How far along is the implementation and can you show some differences in bundle sizes?
chang-ke commentedon Feb 19, 2021
actually it used in my project now, but no test case. the differences of bundle sizes dependes on what npm package you used, I will give some cases and this pr has example project that you can get some real differences bundle sizes by running
jsamr commentedon Feb 26, 2021
Just a side note: I propose that tree-shaking for metro bundler follows webpack
sideEffectspackage.json field, see https://webpack.js.org/guides/tree-shaking/#mark-the-file-as-side-effect-freechang-ke commentedon Mar 1, 2021
got it
chang-ke commentedon Mar 4, 2021
metro config
common bundle (only react-native@0.64-rc2)
index.js:
bundle size: 658KB
common bundle with react-native-svg
index.js:
App.js:
no tree-shaking bundle size: 1191KB
tree-shaking bundle size: 700KB
common bundle with lodash-es
index.js:
App.js:
no tree-shaking bundle size: 940KB
tree-shaking bundle size: 661KB
wilau2 commentedon Mar 25, 2021
What is the next step here?
I have to say I am very interested in this work.
We are now at
7.10 mBin our project.@chang-ke could it be useful that we try your fix in our project?
If you can give me the basics on how to use it in my project I could give another example of tree-shaking for our huge app.
chang-ke commentedon Mar 31, 2021
you can clone this repo and run it by #632 (comment) 's metro config or https://github.com/chang-ke/metro/tree/feature/tree-shaking/examples.
hannojg commentedon Aug 31, 2022
May I ask why this has been closed?
Thanks in advance for your response!
robhogan commentedon Aug 31, 2022
Hi @hannojg, this issue was closed by the author, no PR to Metro was ever opened. I guess they didn't have time to finish it, but the work (on their fork: chang-ke#1) looked interesting.
Tree shaking isn't a priority within Meta right now but we're open to contributions - as this would be quite a complex feature, it'd be a good idea to open a discussion about approaches first.
johnculviner commentedon Nov 23, 2022
Bump
Wasn't a big reason why Hermes was created was to reduce app start time? Rather than creating an entirely new JavaScript engine using the example above treeshaking would cut 1/3 off the bundle size (and maybe about 1/3 off the start time too without Hermes - not sure if it would change anything WITH Hermes)
I think doing this should be considered. I'm looking at my bundle with
npx react-native-bundle-visualizerand seeing all sorts of things that really shouldn't be in there.russeg commentedon Feb 6, 2023
@johnculviner, the contributor said it was not a facebook priority. their needs comes first not the community.
iketw commentedon Mar 12, 2024
@robhogan has FB reconsidered implementing tree-shaking support out of the box?
robhogan commentedon Mar 12, 2024
@iketw we've explored it with interest! Because Metro already only bundles explicitly imported files/modules rather than packages, the gains from tree shaking (=identifying unused
importsexports and eliminating them) are less than some bundlers, and as far as we've seen, not a big win in practice, especially under Hermes where the impact is only on a small fraction of download size vs a binary, with no runtime benefit. It would be a bigger win on web, but that hasn't been Metro's focus.We're absolutely open to explorations in this area though - I'd love to be able to say "Metro does tree shaking", but the fact is folks who've experimented with it on complex, real-world apps haven't yet found significant gains.
kelset commentedon Mar 13, 2024
For what's worth, at Microsoft we have made a plugin for Metro to do treeshaking via esbuild (here) and it had led to good results in our complex real world apps (Office and Xbox to name a couple) for over two years (link to the first release). We have a whole guide around bundling enhanced by rnx-kit here.
iketw commentedon Mar 13, 2024
@kelset thanks for your reply. Based on @robhogan's answer, it seems that the only thing missing for full tree-shaking is unused imports, is that what the MS's plugin completes in the cycle?
Also, I'm building a RN SDK using native modules and JS code. If I use the plugin in my SDK's metro config, if the hosting app does not include the plugin, will it still tree-shake the dependencies inside my SDK?
kelset commentedon Mar 14, 2024
@iketw :
renchap commentedon Mar 26, 2025
I am late to the discussion, but if you are using an icon library like Font Awesome, with imports like
import { faShareSquare } from "@fortawesome/pro-solid-svg-icons", then tree-shaking is a really big win to app size.