1

I'm building an app that need to have lazy loaded module. Everything is working fine on dev mode. When I try to prepare prod dist I have problems with lazy loaded module.

Deployment to prod:

  1. I'm trying to use this angular-seed: https://github.com/mgechev/angular-seed.
  2. Change APP_BASE in file seed.config.ts: APP_BASE = argv['base'] || '/angular-seed-master';
  3. Build command: npm run build.prod.aot (similar problem with build.prod)
  4. copy files to apache-tomcat-8.5.4\webapps\angular-seed-master\

App is starting fine, but when I try to open lazy loaded module I get following error:

__zone_symbol__error
:
Error: Uncaught (in promise): TypeError: System.import is not a function TypeError: System.import is not a function at t.loadFactory (http://127.0.0.1:8080/angular-seed-master/js/app.js?1487588806074:15:566) at t.load (http://127.0.0.1:8080/angular-seed-master/js/app.js?1487588806074:15:151) at t.loadModuleFactory (http://127.0.0.1:8080/angular-seed-master/js/app.js?1487588806074:14:8371) at t.load (http://127.0.0.1:8080/angular-seed-master/js/app.js?1487588806074:14:8103) at e.project (http://127.0.0.1:8080/angular-seed-master/js/app.js?1487588806074:12:21324)

I was able to reproduce this problem just by converting about.module from the seed into lazy loaded module. After doing changes listed below about.module is lazy loaded in dev mode:

app-routing.module.ts (add lazy load path)

@NgModule({
  imports: [
    RouterModule.forRoot([
      { path: 'about', loadChildren: 'app/about/about.module#AboutModule' },
      /* define app module routes here, e.g., to lazily load a module
         (do not place feature module routes here, use an own -routing.module.ts in the feature instead)
       */
    ])
  ],
  exports: [RouterModule]
})
export class AppRoutingModule { }

about-routing.module.ts (change path from 'about' to '' because it is relative to the root router now)

import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { AboutComponent } from './about.component';

@NgModule({
  imports: [
    RouterModule.forChild([
      { path: '', component: AboutComponent }
    ])
  ],
  exports: [RouterModule]
})
export class AboutRoutingModule { }

app.module.ts (remove import of AboutModule)

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { APP_BASE_HREF } from '@angular/common';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';

import { HomeModule } from './home/home.module';
import { SharedModule } from './shared/shared.module';

@NgModule({
  imports: [BrowserModule, HttpModule, AppRoutingModule, HomeModule, SharedModule.forRoot()],
  declarations: [AppComponent],
  providers: [{
    provide: APP_BASE_HREF,
    useValue: '<%= APP_BASE %>'
  }],
  bootstrap: [AppComponent]

})
export class AppModule { }

What alterations need to be done to the seed in order to have lazy loading working ?

mrh
  • 917
  • 1
  • 9
  • 28

1 Answers1

0

Angular-seed production build is bundling the application as a commonjs module now. Currently the production build doesn't support lazy loading but they say it's in the roadmap.

You can see a post related to this here: https://github.com/mgechev/angular-seed/issues/864#issue-154456682

I encountered the same issue with my application. Actually it's a pity and I hope they will add this support soon.

  • I tried "lazy" branch it is still not working, so I was forced to use webpack seed: https://github.com/AngularClass/angular2-webpack-starter – mrh Feb 26 '17 at 11:48
  • I also downloaded the webpack seed and it seems good. Didn't have a chance to port my previous application to it yet. – Jecky Bershteyn Feb 27 '17 at 13:59