Home

Large bundle size of Office Fabric UI React web parts in SPFx

%3CLINGO-SUB%20id%3D%22lingo-sub-698964%22%20slang%3D%22en-US%22%3ELarge%20bundle%20size%20of%20Office%20Fabric%20UI%20React%20web%20parts%20in%20SPFx%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-698964%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20creating%20web%20parts%20that%20will%20go%20on%20a%20same%20page.%20The%20web%20part%20count%20can%20be%20from%2010-15.%20I%20am%20using%20SPFx%20with%20React%20and%20have%20added%20Office%20Fabric%20UI%20React%20to%20the%20web%20parts%20as%20recommended%20in%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsharepoint%2Fdev%2Fspfx%2Foffice-ui-fabric-integration%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EMSDN%3C%2FA%3E.%20Now%20when%20I%20bundle%20the%20web%20parts%20the%20size%20of%20the%20bundled%20files%20are%20large.%20For%20example%2C%20the%20smallest%20file%20is%20of%20104%20KB.%20So%20now%20if%20add%2010-15%20web%20parts%20on%20the%20same%20page%2C%20my%20page%20will%20be%20very%20slow.%3C%2FP%3E%3CP%3EI%20need%20to%20reduce%20the%20bundle%20size%20of%20the%20files.%20Hence%20I%20followed%20%3CA%20href%3D%22https%3A%2F%2Fwww.techmikael.com%2F2017%2F08%2Fhow-to-reduce-bundle-size-when-using.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ethis%3C%2FA%3E%20link%20to%20use%20office%20fabric%20react%20an%20external%20link%20to%20the%20web%20parts.%20I%20added%20the%20following%20code%20in%20the%20externals%20in%20config.json%3A%3C%2FP%3E%3CP%3E%22office-ui-fabric-react%22%3A%20%7B%3CBR%20%2F%3E%22path%22%3A%20%22%3CA%20href%3D%22https%3A%2F%2Funpkg.com%2Foffice-ui-fabric-react%406.143.0%2Fdist%2Foffice-ui-fabric-react.min.js%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Funpkg.com%2Foffice-ui-fabric-react%406.143.0%2Fdist%2Foffice-ui-fabric-react.min.js%3C%2FA%3E%22%2C%3CBR%20%2F%3E%22globalName%22%3A%20%22Fabric%22%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3ENow%20when%20the%20files%20are%20bundled%2C%20the%20size%20reduces%20from%20104%20KB%20to%2045%20KB.%20This%20was%20a%20good%20reduction.%3C%2FP%3E%3CP%3EI%20deployed%20the%20web%20parts%20to%20SharePoint%20but%20the%20office%20fabric%20did%20not%20load.%20The%20web%20parts%20were%20throwing%20following%20error%3A%26nbsp%3B%3C%2FP%3E%3CBLOCKQUOTE%3E%3CP%3E%26nbsp%3BUncaught%20ReferenceError%3A%20React%20is%20not%20defined%3C%2FP%3E%3C%2FBLOCKQUOTE%3E%3CP%3ESo%20in%20order%20to%20remove%20the%20error%20I%20thought%20of%20adding%20the%20react%20CDN%20path%20to%20the%20page.%20I%20did%20this%20on%20classic%20site%20using%20Script%20Editor%20web%20part%20and%20the%20web%20part%20worked!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETo%20add%20the%20same%20CDN%20path%20to%20the%20Modern%20UI%20we%20created%20a%20SPFx%20extension%20and%20installed%20it%20to%20SharePoint.%20But%20when%20I%20added%20the%20web%20parts%20to%20the%20page%20they%20threw%20the%20same%20error%3A%20%22React%20is%20not%20defined%22.%20I%20tried%20adding%20the%20CDN%20paths%20first%20in%20the%20page%20head%20so%20that%20the%20CDN%20files%20load%20before%20the%20fabric%20file.%20But%20this%20did%20not%20succeed.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20then%20added%20the%20CDN%20path%20in%20the%20external%20property%20of%20config.json%20so%20that%20they%20may%20load%20in%20sequence.%20But%20on%20doing%20so%20the%20bundling%20process%20failed%20and%20the%20console%20threw%20the%20following%20error%3A%3C%2FP%3E%3CP%3EError%20-%20%5Bcopy-assets%5D%20External%20%22react%22%20is%20specified%20both%20as%20a%20linked%20package%20in%20the%20node_modules%20dependency%20tree%20and%20as%20an%20external%20in%20in%20the%20config.json%20file.%20Remove%20one%20of%20the%20external%20references.%20Only%20one%20external%20reference%20type%20is%20allowed%20per%3CBR%20%2F%3Eexternal%20package.%3C%2FP%3E%3CP%3ESo%20now%20my%20challenge%20is%20to%20reduce%20the%20bundle%20size%20of%20the%20Office%20Fabric%20UI%20React%20Web%20Part%20files.%20Has%20anyone%20faced%20the%20same%20issue%3F%20Is%20there%20any%20way%20to%20reduce%20the%20bundle%20size%20of%20the%20web%20parts%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERegards%2C%3C%2FP%3E%3CP%3ENishant%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-698964%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20Fabric%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESharePoint%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESPFx%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Deleted
Not applicable

Hi,

 

I am creating web parts that will go on a same page. The web part count can be from 10-15. I am using SPFx with React and have added Office Fabric UI React to the web parts as recommended in MSDN. Now when I bundle the web parts the size of the bundled files are large. For example, the smallest file is of 104 KB. So now if add 10-15 web parts on the same page, my page will be very slow.

I need to reduce the bundle size of the files. Hence I followed this link to use office fabric react an external link to the web parts. I added the following code in the externals in config.json:

"office-ui-fabric-react": {
"path": "https://unpkg.com/office-ui-fabric-react@6.143.0/dist/office-ui-fabric-react.min.js",
"globalName": "Fabric"
}

Now when the files are bundled, the size reduces from 104 KB to 45 KB. This was a good reduction.

I deployed the web parts to SharePoint but the office fabric did not load. The web parts were throwing following error: 

 Uncaught ReferenceError: React is not defined

So in order to remove the error I thought of adding the react CDN path to the page. I did this on classic site using Script Editor web part and the web part worked!

 

To add the same CDN path to the Modern UI we created a SPFx extension and installed it to SharePoint. But when I added the web parts to the page they threw the same error: "React is not defined". I tried adding the CDN paths first in the page head so that the CDN files load before the fabric file. But this did not succeed.

 

I then added the CDN path in the external property of config.json so that they may load in sequence. But on doing so the bundling process failed and the console threw the following error:

Error - [copy-assets] External "react" is specified both as a linked package in the node_modules dependency tree and as an external in in the config.json file. Remove one of the external references. Only one external reference type is allowed per
external package.

So now my challenge is to reduce the bundle size of the Office Fabric UI React Web Part files. Has anyone faced the same issue? Is there any way to reduce the bundle size of the web parts?

 

Regards,

Nishant

Related Conversations
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
Tabs and Dark Mode
cjc2112 in Discussions on
35 Replies
Extentions Synchronization
ChirmyRam in Discussions on
3 Replies
flashing a white screen while open new tab
Deleted in Discussions on
14 Replies