Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Large bundle size) Tree shaking is currently not possible on @tonic-ui/react #832

Closed
ZachLegros opened this issue Feb 28, 2024 · 4 comments

Comments

@ZachLegros
Copy link
Contributor

ZachLegros commented Feb 28, 2024

The current esm build cannot be tree shaken by bundlers such as webpack and rollup. Here is why:

Side effect optimization not enabled with sideEffects flag in package.json

sideEffects: skip over modules that do not have any used exports and that are side effects free

The library marks code as having side effects by default.

The sideEffects flag is especially important for libraries that export their API through an index file that itself exports functions or variables from internal files. Without the side effects optimization, our bundlers would have to parse all the files where our exported variables are defined.

We would need to mark this library as "side effect free" in order to enable side effect optimization by bundlers.

Bundle in a single file

The esm library is bundled in a single file. From a tree shaking perspective, this creates one problem: The side effects optimization is non existent as no modules can be skipped. We can preserve the library's module tree and split the code in small modules to fully benefit from the sideEffects optimization. For example, with Rollup, we can use preserveModules: true in the output config to achieve this.

Here is a good article showing how to build tree shakeable libraries: https://blog.theodo.com/2021/04/library-tree-shaking/

@cheton
Copy link
Member

cheton commented Mar 9, 2024

Further reading: https://mui.com/material-ui/guides/minimizing-bundle-size

@cheton cheton mentioned this issue Mar 10, 2024
21 tasks
@ZachLegros
Copy link
Contributor Author

Further reading: https://mui.com/material-ui/guides/minimizing-bundle-size

Interesting. I will look into how MUI achieved this and perhaps they have a better solution.

@ZachLegros
Copy link
Contributor Author

It looks like MUI is also using side effects optimization: https://github.com/mui/material-ui/blob/master/packages/mui-material/package.json#L106

@ZachLegros
Copy link
Contributor Author

I also noticed that MUI is using UMD modules instead of splitting the build for cjs and esm. There are many other interesting build settings in the rollup config that I want to investigate. See: https://github.com/mui/material-ui/blob/master/packages/mui-material/scripts/rollup.config.mjs

@cheton cheton closed this as completed Jun 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants