-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
@fluentui/react-northstar: importing a single component adds >1MB to bundle size #12953
Comments
@astegmaier that for creating the issue. We already started working on cutting the bundle size. The icons are already separated in their own package, which reduced the app bundle size for about 34%, you can see more info on that on this blog post. We started with removing some component statics that were referenced from the theme, but there are still some problems. We are doing an investigation around this now, so we are going to create a plan and share it by the end of next week. |
Thanks for the details! In the original issue, I was seeing the all the icons in the bundle (i.e. the optimization you mentioned in the blog post) wasn't being reflected in either parcel or webpack. I managed to tweak webpack to do better tree-shaking by using |
Currently we are tracking the bundle size only for webpack. Let me take a look on the issue on the parcel repo and will get back to you tomorrow. Maybe it would be good to add bundle size testsfor more bundlers. |
It looks like parcel2 currently doesn't tree-shake re-exports quite as well as webpack - if you comment out line 301 from
...the bundle size of my sample app in parcel2 shrinks from 1.1MB to 777KiB (compared to a roughly consistant 660 KiB in webpack). Hopefully, the parcel team will be able to address this before the beta release (its tracked in #4565). Having webpack as the main standard for now seems fine to me, but since the JavaScript world changes quickly, and tree-shaking/scope-hoisting is more of an art than a science, you might get better results if you considered multiple bundlers from the outset. I'm an occasional contributor to parcel as well, and we could always use the feedback 😄 Also, you might want to look at this comment for some ideas on how make bundle size even better going forward. |
Thanks for the input @astegmaier and the links to the comments. I cloned your repo and I am waiting on the new release to test again it, as we removed static I wouldn't remove the re-export for the icons from We are removing slowly the Will update you once I try the new release and will post any findings here. |
@astegmaier update from my side. I upgraded locally to v0.49 andI hoped we will see better results, but it was similar. Fortunately I found the issue. In each file we are defining constants for the className of the component and it's slots, and we are exporting these in the following manner:
Just moving these contants in a dedicated file is solving the issue surprisingly.. This worked and was tested with webpack, on both your repo ( This looks really strange for me, but will do some final reading and testing tomorrow, and if I don't find better reason of why this is happening, I will start with creating the files for these classes for all components, which will solve the problem with dragging all components when importing the theme. |
@mnajdova That's great news! Thanks for investigating. |
Here is the PR for reference: #13145 I am still surprised by this to be honest, so will ask other people for review and opinion too. |
Just to clarify: the issue with remaining components in bundles is related to side-effects that we have. One of them are static properties: https://dev.to/layershifter/how-to-kill-tree-shaking-in-webpack-with-static-properties-p72 I went through all cases to summarize required actions:
|
@layershifter @mnajdova would you please give us users status update on this issue. Thank you. |
@latonita the work that was done in #13985 & #14007 decreased bundle size significantly. Now it should be around 450kb on minimal component and theme imports (we started with 1.2mb). Still not good, but it's definitely better. The next huge item is a theme object which is not treeshakable 😨 However, this requires huge changes in the current design of Northstar components. |
@layershifter thanks for update! any ideas how can I reduce icons package at least? 465Kb for the pack, but i only use 5 icons. |
@latonita it should not be a case if you're using 0.51.0 or later. Result below is for two icons, they are completely treeshakable: I created a sample repo to check: https://github.com/layershifter/sample-fluentui |
@layershifter I'm also facing the same issue, a single component imported from @fluentui/react-northstar is taking the bundle size to 1.2mb. I'm using the latest 0.51.2 version. |
@syedjahangirpeeran can you please share a minimal repro repository? For example you can fork https://github.com/layershifter/sample-fluentui. |
@layershifter just checked - i'm on 0.49. i will upgrade to 51+ today and let you know |
@layershifter Slightly related to this issue. The giant bundle size is causing webpack to take seconds to rebuild. You can start any new webpack project with the default development config. When I only include an This slow iteration problem also happens to all projects generated with https://github.com/pnp/generator-teams. I think it's a very bad experience for a ms recommended teams bootstrap project to take 7sec to rebuild on every single change. |
@layershifter i finally made it to 0.51.2 today, had to fix some theme (updateTheme stopped working, btw, had to write my own updateTheme) and some grid changes. any suggestions? shall I import differently? |
@latonita can you please create and share a small repo that I can run to check? |
@layershifter okay. here it is https://github.com/latonita/test-teams-fluentui |
@latonita Thanks for sharing, that's something 👍 There is an issue with ModuleConcatenation that prevents tree-shaking:
It's not related to Fluent UI itself, it's related to third party dependency called 😿 A line with It also visible by public tools:
Once I manually complied it to ESM I got 652KB vs 1.4 MiB previously: |
hm.. i see its a part of teams generator. @layershifter any recomendation how to fix? config changes? source code fixes? meanwhile i will try to remove that package To be honest, i dont see anything very bad there. Small class, here is the header with import statements
I have just removed that package - just copied that class to the app. Again, nothing changed :) |
@latonita oops, I missed the second important change:
|
@latonita I created two PRs and described them to explain the issue: wictorwilen/msteams-react-base-component#5
Please check wictorwilen/msteams-react-base-component#5, there are no issues with source itself there is an issue missing ESM dist. |
@layershifter i think thats only part of the issue. |
@latonita I pulled your latest There is no icons included. We can't do anything with red area now: you're correct all themes are big objects that are not tree-shakable, so will you will anyway pay for themes if they are included. However, actual components will not be included to the bundle, This can be verified by using my Webpack's config for debugging: https://github.com/layershifter/sample-fluentui/blob/a5ffea15b696f0c0d2df1d694d20bfab1418cc3d/webpack.config.js#L14-L38. With it you can build partially minified bundle and ensure that components are not included even if |
@layershifter thanks for the help! 👍 |
As there is no work planned or any possible improvements - closing this for housekeeping 🏡 |
Environment Information
"@fluentui/react-northstar": "0.48.0"
Please provide a reproduction of the bug in a codepen:
Create a simple app with the following files:
index.tsx
components/App.tsx
Build it with webpack (or Parcel2) in production mode (which enables tree-shaking and minification), and you'll see a 1.3-1.4MB increase in bundle size over a simple
Hello World
react app that doesn't use@fluentui/react-northstar
.Here's a visualization of the minified, tree-shaken bundle from
webpack
, usingsource-map-explorer
:You can see a repro for the issue in the
fluentui-button
branch of this repository. Runyarn build:webpack
(to see webpack bundle output),yarn build:parcel
(to see parcel2 bundle output).This is related to issue #2113, #2324, and #2324 on the legacy repo. Those are still marked as "open", and I don't see any issues or PRs addressing it in the new repo.
Actual behavior:
The bundle size increase of using a single button component is really, really big (1.3-1.4 MB). It looks like it's importing just about every component and icon into the bundle, even though the app uses no icons and only a single
button
component.Expected behavior:
The bundle size increase should be "pay-to-play" - the more components/icons that I leverage, the more space it takes, and it should start small.
Priorities and help requested:
Are you willing to submit a PR to fix? Happy to help brainstorm but I don't think I have the time to fashion a full PR.
Requested priority: Medium (before a v1 release, would think it's important to get this under control).
The text was updated successfully, but these errors were encountered: