How to exclude the modules from angular build?

1.5k views Asked by At

In my angular application, there are bunch of modules.

I want to exclude few modules from angular build.

ex: I have a one repository in which i do have entire code with all the angular modules and it's working fine. some of modules are chat module, dashboard module, products module, devices module

  1. scenario 1: I want to give only chat module & product module to one client
  2. scenario 2: I want to give only dashboard module and chart module to another client.

How can i create a angular build so that it uses the repository but creating a build based on client module requirement and exclude the un-necessary modules from build ?

2

There are 2 answers

0
Divek John On
//app-module.ts
   import { NgModule, Optional } from '@angular/core';
    import { BrowserModule } from '@angular/platform-browser';
    
    import { AppComponent } from './app.component';
    
    @NgModule({}) class MyModule {}
    
    // you can also put this config client-specific config
    const client1 = true;
    
    @NgModule({
      imports:      [ BrowserModule, client1 ? MyModule : []],
      declarations: [ AppComponent ],
      bootstrap:    [ AppComponent ]
    })
    export class AppModule {
      constructor(@Optional() module: MyModule) {
        console.log(module);
      }
    }

In case of lazy loading include routing modules based on condition(Not Tested)

//app-module.ts
    const client1 = true;
    let GlobalRoutingModule

    if (client1) {
      import('./app-routing-client1.module')
        .then((routingModule) => {
          GlobalRoutingModule = routingModule
        });
    } else {
      import('./app-routing-client2.module')
        .then((routingModule) => {
          GlobalRoutingModule = routingModule
        });
    }
0
jaibalaji On

You can maintain separate branch for different client and exclude the modules from app module and disable the router's and related setting of those modules which is not required and build & deliver the code to the client.