Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude some of the lazy modules from the build (Angular5)

I am working on a project, which runs at multiple customers. There are many lazy loaded modules, and most of them is used by all of the customers. But there are some modules, which are only needed in a few places, so I would like to exclude them from the build everywhere else.

Is it possible?

like image 414
Iter Ator Avatar asked May 24 '18 14:05

Iter Ator


1 Answers

How about something like this: (you could then use if statements, etc, to load different modules)

export class AppComponent implements AfterViewInit {

  @ViewChild('testOutlet', {read: ViewContainerRef}) testOutlet: ViewContainerRef;
  constructor(
    private loader: NgModuleFactoryLoader,
    private injector: Injector) {
    }

    ngAfterViewInit(): void {
    const path = 'src/app/lazy/lazy.module#LazyModule';
    this.loader.load(path).then((moduleFactory: NgModuleFactory<any>) => {
      const entryComponent = (<any>moduleFactory.moduleType).entry;
      const moduleRef = moduleFactory.create(this.injector);

      const compFactory = moduleRef.componentFactoryResolver.resolveComponentFactory(entryComponent);
      this.testOutlet.createComponent(compFactory);
  });
  }
}
like image 115
JBoothUA Avatar answered Oct 13 '22 01:10

JBoothUA