project-slippi / slippi-js

Parse slp files and compute stats
GNU Lesser General Public License v3.0
148 stars 79 forks source link

Enable tree-shaking in ESM #89

Closed vinceau closed 2 years ago

vinceau commented 2 years ago

Description

This PR enables tree-shaking so that people importing the library won't have to import the entire thing.

We achieve this by doing the following:

Bundle Size Comparison

Testing was done by compiling my Slippi Stats project and using the yarn run analyze command.

Before

The original bundle size was around 2MB all up. image

After

image

image

Source Map Comparison

Before

image

After

image

Conclusion

As you can see, we can reduce Stats App bundle size from 2MB down to around 1MB just by enabling tree shaking. The biggest difference is that the enet library (around 725KB) no longer gets imported at all since it's not used. This makes a huge difference for all web based projects that are installing slippi-js.

By explicitly importing the required lodash functions and not using _.chain, we can further reduce the size of the slippi-js bundle from around 249KB down to 141KB. This is all while preserving the existing library API, mostly just by changing the way we do imports and bundling.