Slim down the React build
See original GitHub issueI created a brand new React-TypeScript addin using the yo office generator. The sample project, without any changes results in almost 3 MB of JS output. This seems like an important problem since I assume a lot of people will be starting with this generator, and we might see big addins that might slow down Office apps.
Here is what npm run build displays:
Asset Size Chunks Chunk Names
app.84c970d255d072636045.js 1.19 MiB 0 [emitted] [big] app
app.84c970d255d072636045.js.map 3.87 MiB 0 [emitted] app
assets/icon-16.png 1.56 KiB [emitted]
assets/icon-32.png 2.33 KiB [emitted]
assets/icon-80.png 4.72 KiB [emitted]
assets/logo-filled.png 11.6 KiB [emitted]
function-file.84c970d255d072636045.js 1.12 KiB 1 [emitted] function-file
function-file.84c970d255d072636045.js.map 4.92 KiB 1 [emitted] function-file
function-file/function-file.html 830 bytes [emitted]
index.html 841 bytes [emitted]
vendor.84c970d255d072636045.js 1.06 MiB 2 [emitted] [big] vendor
vendor.84c970d255d072636045.js.map 3.87 MiB 2 [emitted] vendor
Summary of Problems:
- The current React template loads office-fabric and ui-fabric twice, both in app.js and in vendor.js. It is ignoring that it has been loaded into vendor.js and it is being included in app.js as well.
- No tree shaking for office-fabric, even if you use a button, it seems to load the whole library.
I enabled the webpack-bundle-analyzer https://www.npmjs.com/package/webpack-bundle-analyzer and here is the report it generated on the production build:

Some suggestions for improvement:
- splitChunks’s chunks property is “async”, changing this to “all” helps a little.
- entry point is an array that includes react-hot-loader, having an array seems to disable some of webpack’s optimizations. See https://github.com/OfficeDev/office-ui-fabric-react/issues/7581#issuecomment-467585503
- Importing from the internals of the library helps. If I change import { Button, ButtonType } from “office-ui-fabric-react”; to import { Button, ButtonType } from “office-ui-fabric-react/lib/Button”; it seems to just import the Button. I think webpack should be handling this, but there is something lost in translation, so this optimization is not done automatically for some reason.
- Also see https://github.com/OfficeDev/office-ui-fabric-react/issues/7581 which discusses similar issues.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:4
- Comments:5 (1 by maintainers)
Top Results From Across the Web
10 Tips & Tricks for smaller bundles in React apps - ITNEXT
In this article I'll attempt to share a few tips & optimization tricks that you should consider when aiming to minimize the footprint...
Read more >How to Slim Down Your Bundle Size
Building a sample bundle with Webpack. Let's start by setting up a simple Webpack project. We are going to use the basic packages...
Read more >Creating slim docker images for react apps - Blog - Manish Raj
Creating slim docker images for react apps. In this blog post, we will create a slim, production ready docker image for a react...
Read more >Easy Optimization Of Your React Docker Image. Down To 22MB!
How to build and run a lean Docker image for your React web application and why it is important.
Read more >Optimizing Performance - React
This will create a production build of your app in the build/ folder of your project. ... so React had to go down...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Selective imports worked and shrank everything way down for me. Strangely enough, this is how Microsoft recommends using the fabric ui library in some guides. You just need to be careful to do it everywhere in your project. Also, you don’t need to remove the whole
vendor
part fromwebpack.config.js
, deleting theoffice-ui-fabric-react
should be enough. I had to remove thefabric.min.css
from the taskpane code as well, freeing up 800kb.This way I got from:
down to:
@ustun Thanks for opening this issue. We realize this is a problem with the React template and will open a work item on our end to address this issue in the near future
Thanks,
Courtney