I have an angular module for demo purposes (DevShowcaseModule). This module should not be included in the production build. In order to hide this demos from the endusers and prevent demo code errors in the production.
Environment:
This is my app-routing.module.ts
{
path: APP_NAV_ITEMS.DEV_SHOWCASE,
canActivate: [ AuthGuard ],
loadChildren: './_dev-showcase/dev-showcase.module#DevShowcaseModule',
}
I have tried to exclude the module folder from the tsconfig.json. But it doesnt work, i can still call the route and the demo module is loaded.
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"importHelpers": true,
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"module": "es2015",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"target": "es5",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2018",
"dom"
]
},
"exclude": [
"app/_dev-showcase/*"
]
}
Any idea how to do it properly?
Thanks!
define where we want to load our component in the template with the ng-template tag, define its view query through ViewChild decorator, which gives us access to the DOM and defines the container to which the component will be added, finally, dynamic import the component and add it to the container.
You can check to see that a module is indeed being lazy loaded with the Chrome developer tools. In Chrome, open the dev tools by pressing Cmd+Option+i on a Mac or Ctrl+Alt+i on a PC and go to the Network Tab. NOTE: Another important check is to make sure that a module loaded lazily is not loaded again.
Lazy loading is an important Angular feature that helps to reduce the initial load time since it loads only the necessary files first. Other required modules are loaded on demand when you navigate to their particular route. Now, you can take advantage of this feature to improve your app's load time.
I think you can leverage CLI fileReplacements feature like:
angular.json
"configurations": {
"production": {
"fileReplacements": [
...
{
"replace": "src/app/demo.routes.ts",
"with": "src/app/demo.routes.prod.ts"
}
],
demo.routes.ts
import { Routes } from '@angular/router';
export const demoRoutes: Routes = [
{
path: 'demo',
loadChildren: './demo/demo.module#DemoModule'
}
];
demo.routes.prod.ts
export const demoRoutes = [];
The root router configuration should look like:
import { demoRoutes } from './demo.routes';
RouterModule.forRoot([
{
path: '',
component: HomeComponent
},
...demoRoutes
])
Using this method the cli will only bundle the DemoModule
in dev mode.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With