Description
Hi, I would like to revisit the issue of removing the use of haste modules as described by issue #14196.
I have been able to get a bundle size reduction of react-native using webpack
with haul
. My approach relies on the modules not using haste so that webpack can properly identify dead code.
I've put together a code example using create-react-native-app
ejected you can see it here:
https://github.com/rdy/tree-shaking-example/tree/tree-shaking. I've chosen to use the android platform since I had the build tools on my machine already. You can build the bundles from the package.json scripts bundle:metro
and bundle:haul
.
The master branch contains a version where I've optimized the bundle size with haul slightly by tweaking the babel output instead of relying on react-native-preset
.
Metro bundle: 505K
Haul bundle: 538K
The tree-shaking
branch shows the changes from that where I've used my fork of react-native
that removes haste as well as haul that doesn't use haste anymore. I've provided a transform for babel-plugin-transform-imports
. I'm basically looking at the original main for react-native and supplying the correct import path, this allows webpack to be able to tree shake out the dead code.
Haul bundle with tree shaking: 254K
I've created a series of PDF using source-map-explorer
to illustrate that pieces of the bundle are actually being remove correctly by webpack.
metro.pdf
haul-pre-shake.pdf
haul-shaken.pdf
I believe that I could fix the problem with a babel-plugin to rewrite the haste modules to relative requires if necessary but it would be better to not have to maintain a fork or have to transform react-native
. Is there a compelling reason to keeping haste? The bundle savings show in my example are pretty compelling.
I was able to do everything with off the shelf components. The only changes I've needed to make was removing haste from react-native
and haul
so that webpack can trim the dead code.