Monday, 29 April 2019

Angular: lazy loading module only for dev (environment.production === false)

I have a lazy loaded module only for development purpose and i want don't deploy it into the production build.

With a guard i have denied the activation and the loading:

const routes: Routes = [
  {
    path: 'dev',
    loadChildren: './features/dev/dev.module#DevModule',
    canActivate: [DevelopmentGuard],
    canLoad: [DevelopmentGuard]
  },
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

the guard:

@Injectable({
  providedIn: 'root'
})
export class DevelopmentGuard implements CanActivate, CanLoad {

  constructor() {}

  canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
    return this.can();
  }

  canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
    return this.can();
  }

  canLoad(route: Route, segments: UrlSegment[]): boolean {
    return this.can();
  }

  private can(): boolean {
    return (environment.production === false);
  }
}

it works, my dev module works only in dev, but the scripts of the dev module are in the build.

There is a way for totally remove the script from the build of the prod version?



from Angular: lazy loading module only for dev (environment.production === false)

No comments:

Post a Comment