Stop Using Shared Material Module

Santosh Yadav
Angular In Depth
Published in
5 min readMar 27, 2020
Image By www.96five.com

AngularInDepth is moving away from Medium. This article, its updates and more recent articles are hosted on the new platform inDepth.dev

In this blog post, I will talk about a mistake I have made and have seen many developers doing it as well, which is a shared Material Module. I am sure if you used Angular Material, you still have a SharedMaterial Module in your project, time to remove it.

What is my Motivation

I am working on a new project which has many modules. It has more than 20k Lines of Code. I was working on making some modules as Lazy Loaded modules. I realized we are using the SharedMaterial module in all modules. First, I thought it’s ok, but I tried to do an experiment which proved its a bad idea.

Time To Prove Ourselves Wrong

In my last project where we had our custom components like Grid, Tables, Forms and I did ended up using a big Shared Material Module, as all the components were written on top of Angular Material Component. And now it’s time to prove myself wrong that it was not a good approach.

Let’s create a new App, and we will cover what’s wrong with this approach and how it increases the bundle size. We will be using webpack-bundle-analyzer to check the bundle size. Run the below command to create a new App. Make sure you are using the latest Angular CLI

ng new demoapp

next install webpack-bundle-analyzer using the below command

npm i webpack-bundle-analyzer -D

now add the given script in package.json

"analyze": "ng build --prod --stats-json && webpack-bundle-analyzer ./dist/demoapp/stats-es2015.json"

now run the below command to view the stats.

npm run analyze
Bundle Analyzer 1.1

The above command opens a page, as shown in the image above.

Now, let’s add Angular Material using the below command:

ng add @angular/material

Run the npm run analyze command again and see the main bundle size. It's already increased by around 70 KB without using even a single component from Angular Material.

After Adding Angular Material 1.2

Now let’s add 2 modules employee and department, respectively, using the below command. We will not lazy load them for the first example.

ng g m employee --routing --module app
ng g c employee --export true
ng g m department --routing --module app
ng g c department --export true

Open app.component.html and replace the default template data with

<app-employee></app-employee><app-department></app-department>

If we analyze the bundle again at this point as we hardly have any code, there is a very small change in size, and it has reduced as the default template contains lots of markups and CSS.

After adding modules 1.3

Now let’s add some code in both the component, we will copy some code from Material documentation.

Add the below code into department and employee component respectively

department.component.html
employee.component.html

One more change is needed in department.component.ts add the below property :

panelOpenState = false;

Now, we need to add some Material module into our employee, and department module as well to avoid the build error, and this is where most of us decide to create a SharedMaterial module like one below.

ng g m shared/material --flat true

and add the below code into the new module:

material.module.ts

Now include the newly create MaterialModule into employee and department module.

imports: [
CommonModule,
MaterialModule
]

Now run the analyzer again, you can see 216 KB has increased.

After using Angular Components 1.4

Now to optimize the app, the next approach we take is lazy load the modules. Let’s convert the employee and department module to a lazy loaded module.

Remove the EmployeeModule and DepartmentModule from app.module.ts remove the import statement and from the import array as well.

This is the code after removing both modules

app.module.ts

Next, configure the employee and department as a lazy-loaded module. Add the below code to app-routing.module.ts

app-routing.module.ts

Next, add the below code in employee-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { EmployeeComponent } from './employee.component';
const routes: Routes = [
{ path: '' , component : EmployeeComponent }
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class EmployeeRoutingModule { }

And similar changes in department-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { DepartmentComponent } from './department.component';
const routes: Routes = [
{ path: '', component: DepartmentComponent }
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class DepartmentRoutingModule { }

Let’s change app.component.html to use routerLink to lazy-load these 2 modules.

<a [routerLink]="['/employee']" routerLinkActive="router-link-active">Employee</a><a [routerLink]="['/department']" routerLinkActive="router-link-active">Department</a><router-outlet></router-outlet>

Now run the analyzer again and check the bundle size. After lazy loading, the bundle size should be reduced, but it increased by around 70KB.

After lazy loading 1.5

Now, let’s remove the Shared Material module and import only the modules which are needed. In employee.module.ts import MatFormFieldModule and MatSelectModule and in department.module.ts import MatExpansionModule and MatFormFieldModule delete the shared module and run the command to analyze the bundle size. In this example, we save around 40KB.

CONCLUSION

I did a similar experiment in my current project and the bundle size was reduced by around 200 KB. Remember when it comes to the web every single KB matters. So, I would suggest and try in your app refactor and share your experience.

GitHub Repo: https://github.com/santoshyadav198613/sharedmodule-demo

--

--

Santosh Yadav
Angular In Depth

I am a GDE for Angular, GitHub Star, and an Auth0 Ambassador, he loves contributing to Angular and NgRx. I work as a software consultant.