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.

@rjsf/material-ui breaks tree shaking

See original GitHub issue

Prerequisites

Description

Importing @rjsf/material-ui into a project bundled with esbuild causes bundle size to increase a lot.

This seems to be because of https://github.com/rjsf-team/react-jsonschema-form/blob/master/packages/material-ui/src/Theme5/Mui5Context.tsx#L11, which causes the entire @mui/icons-material package to be included in the bundle.

Bundle size with @rjsf/material-ui included: 6,9M Bundle size without @rjsf/material-ui: 2,4M

Steps to Reproduce

  1. Include @rjsf/material-ui in a project
  2. Bundle it using a tree-shaking enabled bundler (like esbuild)
  3. Compare bundle sizes before and after @rjsf/material-ui inclusion

Expected behavior

The icons should be tree-shaken and only icons used by the application should be included in the bundle.

Actual behavior

All icons from the @mui/icons-material package are included in the bundle.

Version

        "@rjsf/core": "^4.0.1",
        "@rjsf/material-ui": "^4.0.1",

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:15 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
heath-freenomecommented, Mar 9, 2022

@rollercodester @mdarocha I may try to meld the two approaches when I have time this weekend… Get a better approach to type handling that is highlighted in the PR by @rollercodester along with trying the /*#__PURE__*/ directive to get tree-shaking

0reactions
rollercodestercommented, Mar 9, 2022

I spent a little more time on this after remembering that the MUI project is structured so that each component can be required on its own. This negates the need for code-splitting, rather each component just needs to be required individually.

I took a stab at another PR (#2754), making sure that all of the components are individually required and also properly typed (i.e. no more use of any). Additionally, I abstracted context usage into a hook so that the component consumption is much easier to maintain in the future.

I’ve verified that this results in a 68% (6.34mb) reduction in the resulting parsed bundle. The previous (yucky) PR has been closed in lieu of #2754.

@heath-freenome, feel free to ignore if you still want to dig in this weekend. I did this mainly to ease my own sanity as it was bugging me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Errors with version 4.0 and material-ui 4 · Issue #2721 - GitHub
In the above scenario, in spite of the warnings/errors, the form displays. But it breaks when I set `tagName="div". `import Form from '@rjsf/ ......
Read more >
How to get Tree Shaking to work in Webpack 4 for React ...
I'm using Webpack 4 with ts-loader for TypeScript . The library in question is material-ui . In their docs (see How to reduce...
Read more >
Tree Shaking - webpack
Tree Shaking. Tree shaking is a term commonly used in the JavaScript context for dead-code elimination. It relies on the static structure of...
Read more >
Themes - react-jsonschema-form documentation
To use a theme from a package, just import the <Form /> component from that package. For example, to use the material ui...
Read more >
Tree-Shaking Problems with Component Libraries - Medium
Tree -shaking isn't magic, and much like the garbage collector, ... this code in place, unsure if removing it will break runtime behaviour....
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