Khosro
Khosro

Reputation: 116

Angular-cli webpack lazy loading not working

I'm trying to make asynchronous routing with latest angular-cli (master branch) with angular2 RC6. But I'm stuck...

Here's the code :

app/app.routing.ts

import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { AuthGuardService } from './shared';

const routes: Routes = [
  {
    path: '',
    loadChildren: () => require('es6-promise!./+dashboard/dashboard.module')('DashboardModule'),
    canActivate: [AuthGuardService],
    pathMatch: 'full'
  },
  {
    path: 'login',
    loadChildren: () => require('es6-promise!./+login/login.module')('LoginModule'),
  }
];

export const routing: ModuleWithProviders = RouterModule.forRoot(routes);

app/+dashboard/dashboard.routing.ts

import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { DashboardComponent } from './';

const routes: Routes = [
  {
    path: '',
    component: DashboardComponent
  }
];

export const dashboardRouting: ModuleWithProviders = RouterModule.forChild(routes);

app/+login/login.routing.ts

import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { LoginComponent } from './';

const routes: Routes = [
  {
    path: '',
    component: LoginComponent
  }
];

export const loginRouting: ModuleWithProviders = RouterModule.forChild(routes);

app/+dashboard/dashboard.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { DashboardComponent, dashboardRouting } from './';

console.log('`Dashboard` bundle loaded asynchronously');

@NgModule({
  imports: [
    CommonModule,
    dashboardRouting
  ],
  exports: [
    DashboardComponent
  ],
  declarations: [DashboardComponent]
})
export class DashboardModule { }

app/+login/login.module.ts

import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { CommonModule } from '@angular/common';

import { LoginComponent, loginRouting } from './';
import { MdModule } from '../shared';

console.log('`Login` bundle loaded asynchronously');

@NgModule({
  imports: [
    CommonModule,
    loginRouting,
    FormsModule,
    ReactiveFormsModule,
    MdModule.forRoot()
  ],
  exports: [
    LoginComponent
  ],
  declarations: [LoginComponent]
})
export class LoginModule { }

app/+dashboard/dashboard.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'my-dashboard',
  templateUrl: './dashboard.component.html',
  styleUrls: ['./dashboard.component.scss']
})
export class DashboardComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

}

app/+login/login.component.ts

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';

import { UserService } from '../shared';

@Component({
  selector: 'my-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {

  private loginForm: FormGroup;
  private usernameCtrl: FormControl;
  private passwordCtrl: FormControl;

  constructor(private fb: FormBuilder, private userService: UserService, private router: Router) {
    this.usernameCtrl = fb.control('', Validators.required);
    this.passwordCtrl = fb.control('', Validators.required);

    this.loginForm = fb.group({
      username: this.usernameCtrl,
      password: this.passwordCtrl
    });
  }

  ngOnInit() {
    if (this.userService.isAuthenticated()) {
      this.router.navigate(['/']);
    }
  }

  authenticate() {
    this.userService.authenticate(this.usernameCtrl.value, this.passwordCtrl.value)
      .then(() => this.router.navigate(['/']));
  }

}

There is no error on compilation nor runtime. But the async components are not loaded.

On '' path, in console I have: "Dashboard bundle loaded asynchronously". But no content from dashboard component (The constructor and ngOnInit are not called).

On 'login' path, I have: "Login bundle loaded asynchronously". But no content from login component (The constructor and ngOnInit are not called).

Upvotes: 1

Views: 1144

Answers (2)

Rex
Rex

Reputation: 678

With the latest angular-cli as of today, beta.21, we can use both absolute path and relative path for lazy load modules like this:

{path: 'lazy-module', loadChildren: 'app/lazy-module/lazy.module#LazyModule'} or {path: 'lazy-module', loadChildren: './lazy-module/lazy.module#LazyModule'} assuming the relative path relative to the router configuration file.

There is a gotcha we need to aware of for now:

Every time we edit lazy module route configurations, we need to manually restart webpack by stopping the current npm start and re-run npm start.

Angular-cli does some code manipulation for lazy load modules on fresh start, but it does not do this on webpack's auto-recompilations on changes due to performance issues.

It may not be the case one day. I look forward to it.

Happy coding with Angular!

Upvotes: 4

Khosro
Khosro

Reputation: 116

Resolved by removing barrels usage...

Upvotes: 2

Related Questions