Webpack Bundle Analyzer: Master Angular Optimization and Reduce Build Size by 55%

Webpack Bundle Analyzer: Master Angular Optimization and Reduce Build Size by 55%

Aย sizableย JavaScriptย bundleย canย significantlyย affectย userย experienceย byย prolongingย downloadย andย loadย times.ย Thisย issueย isย particularlyย concerningย forย sizableย projects thatย incorporateย variousย third-partyย librariesย andย tools,ย potentiallyย leadingย toย aย considerableย increaseย inย bundleย size

Luckily,ย theย webpackย bundleย analyzerย offersย aย simpleย methodย toย assessย andย enhanceย theย buildย sizeย ofย your Angularย project. Thisย manualย willย showย youย how toย combineย webpack bundleย analyzer withย Angular,ย visualizeย theย structureย ofย your projectโ€™sย bundle,ย andย implementย methodsย toย decreaseย itsย size.


Setting Up an Angular Project for Analysis

Beforeย weย beginย optimization, letโ€™sย createย aย sampleย Angularย projectย andย addย aย third-partyย library toย see howย itย affectsย the bundleย size.

Step 1: Create a New Angular Project

# Create a new Angular project  
$ ng new DemoProject

Step 2: Add a Third-Party Library

$ cd DemoProject  
$ npm i devextreme@20.1.7 devextreme-angular@20.1.7 --save

Updateย theย app.module.tsย fileย toย addย theย DxDataGridModuleย module.ย Hereย isย theย revisedย codeย snippet:

import { DxDataGridModule } from 'devextreme-angular';  

@NgModule({
imports: [
BrowserModule,
DxDataGridModule
],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule {}

Step 3: Generate a Production Build

Executeย theย subsequentย command toย generateย a productionย build:

$ npm run build  

Uponย completionย ofย theย execution,ย youย willย observeย aย distย folderย thatย holdsย the build,ย roughly 45ย MB in size.


Integrating Webpack Bundle Analyzer

It’sย time toย utilizeย webpackย bundleย analyzerย toย comprehendย andย enhanceย the bundleย layout.

Step 1: Install Webpack Bundle Analyzer

Install the plugin as a dev dependency:

$ npm i webpack-bundle-analyzer --save-dev  

Step 2: Generate Stats File

Theย Angular CLIย allowsย forย theย creationย ofย aย stats.jsonย file.ย Includeย anย additionalย scriptย inย your package.jsonย file:

"scripts": {  
"build:stats": "ng build --stats-json"
}

Run the command below to generate the stats file:

$ npm run build:stats  

Theย stats.jsonย fileย willย nowย beย foundย withinย theย dist folder.

Step 3: Run Webpack Bundle Analyzer

Addย anย additionalย scriptย inย yourย package.jsonย toย evaluateย theย statistics

"scripts": {  
"analyze": "webpack-bundle-analyzer dist/DemoProject/stats.json"
}

Run the following command to launch the analyzer:

$ npm run analyze  

Theย analyzer willย launchย atย localhost:8888ย inย yourย browser, showcasingย aย comprehensiveย visualization of yourย projectโ€™sย bundle.


Recognizingย andย Addressingย Bundleย Problems

Fromย theย examination,ย youย mayย discoverย thatย certainย third-party librariesย bringย inย superfluousย modules. Forย example,ย inย ourย project,ย weย importedย theย devextremeย andย devextreme-angular librariesย entirely, which includedย modulesย thatย wereย unnecessary,ย likeย forms andย routers.

Optimization: Import Only the Required Modules

Ratherย thanย importing theย wholeย library,ย modifyย theย importย statementย toย include justย theย essential modulesย (forย instance,ย the gridย module).

Step 1: Update the Imports

Modify your app.module.ts to import only the grid module:

import { DxGridModule } from 'devextreme-angular';  

Step 2: Generate a New Production Build

Run the following command again:

$ npm run build  

Check the size of the updated dist folderโ€”it has now reduced to 20 MB, a significant improvement from the original 45 MB.


Outcomesย andย Summary

Throughย theย examinationย ofย the bundleย withย webpackย bundleย analyzerย andย byย importing onlyย essential modules, weย attainedย a 55%ย decreaseย inย bundleย size.ย Improvingย theย sizeย ofย the Angular buildย increases performanceย andย alsoย boostsย userย experienceย byย decreasingย loadย times.

Beginย incorporatingย webpackย bundleย analyzerย intoย yourย projectsย todayย toย enhanceย yourย understandingย ofย yourย buildย andย optimizeย itย efficiently!

More Articles for you

115 thoughts on “Webpack Bundle Analyzer: Master Angular Optimization and Reduce Build Size by 55%

  1. hey there and thank you for your info โ€“ I’ve definitely picked up anything neew
    from right here. I did however expertise several technical
    points usung this web site, ince I experienced to reload the website many
    times previous to I could get it to load properly. I had been wohdering
    if your web hosting is OK? Not that I’m complaining, but sluggish loading instances times will sometimes affect your placement in google and ould damage
    your high-quality score if ads and marketing with Adwords.
    Anyway I’m adding this RSS tto my e-mail and can look out for much morfe of your respective exciting content.
    Make sure you update this again ver soon. http://boyarka-Inform.com/

  2. I loved as much as you will obtain performed right here. The cartoon is tasteful, your authored material stylish. nonetheless, you command get got an impatience over that you would like be delivering the following. in poor health without a doubt come more previously once more as exactly the similar nearly very regularly inside of case you protect this hike.

  3. demais este conteúdo. Gostei bastante. Aproveitem e vejam este site. informações, novidades e muito mais. Não deixem de acessar para se informar mais. Obrigado a todos e até a próxima. ๐Ÿ™‚

  4. Today, while I was at work, my cousin stole my iPad and tested to see if it can survive a twenty five foot drop, just so she can be a youtube sensation. My iPad is now destroyed and she has 83 views. I know this is totally off topic but I had to share it with someone!

  5. Normally I do not learn article on blogs, but I wish to say that this write-up very forced me to take a look at and do so! Your writing style has been surprised me. Thanks, quite nice article.

Leave a Reply

Your email address will not be published. Required fields are marked *