AOT build produces applications unable to lazy load modules
angular-cli: 1.0.0-beta.16
node: 6.0.0
os: linux x64
reproduction repo can be found here
The log given by the failure.
main.cfeed71….bundle.js:547 EXCEPTION: Uncaught (in promise): Error: Cannot find module
'app/lazy/lazy.module.ngfactory'.
It seems that aot option can not build project that lazily loads modules. I would hope that one could use the patterns in the official documentation -- which I believe I'm doing -- and use the -- aot option to compile your app easily. I would appreciate any clarification as to whether this is an issue with the CLI or with the compiler. Thanks.
@achimha all I can say is that we're working as fast as possible to get it fixed. It's our top priority atm really.
@filipesilva can you please confirm, when fixed should it also help overcome the memory errors that we encounter when compiling large project without lazy loading? Such the one referenced here https://github.com/angular/angular/issues/12184
Thanks for the update @achimha. I'm sure this is no small issue to work on and look forward to being able to leverage both AoT and lazy loading in our production app
Is there a way to leverage lazy-load routes with AoT today? I'm using @ngtools/webpack and lazy routes are not recognized unless I also use angular2-router-loader. In this case, components are not precompiled anyway...
A big shout out to @hansl and @filipesilva for making this work in angular-cli 1.0.0-beta.20-4. I have our app lazy load with AOT now. You guys rock! I appreciate all the effort that goes into fixing the tooling so that angular devs can focus on building applications, not build infrastructure 👍
I'm still getting AppModule is not an NgModule in beta.20-4 using ng build -prod -aot
Me too (well, not AppModule but the lazy module name instead), dunno how to solve it
Yes, same thing here MyLazyLoadedModule is not and NgModule when trying ng build --prod --aot
Repo here: https://github.com/MrCroft/lazy - ignore all the "angular-cli beta.18" commits; package.json and angular-cli.json are updated accordingly (for angular-cli 1.0.0-beta.20-4, respectively Angular 2.2.1)
@angular: 2.2.1
angular-cli: 1.0.0-beta.20-4
node: 6.9.1
os: win32 x64
I can confirm Lazyloading with AoT (ng build --aot --prod) is working properly in latest release of Angular-Cli
Those having issues could try creating routing module and add to imports of AppModule.
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
@NgModule({
imports: [
RouterModule.forRoot([
{ path: '', loadChildren: 'app/home/home.module#HomeModule' },
{ path: 'lazyLoadedFeature', loadChildren: 'app/lazyLoadedFeature/lazy-loaded-feature.module#LazyLoadedFeatureModule' },
]
)
],
exports: [
RouterModule
]
})
export class AppRoutingModule { }
In Lazy loaded module define its routing as a separate module and import in the child module.
import { FeatureListComponent } from './feature-list.component';
import { FeatureDetailsComponent } from './feature-details.component';
@NgModule({
imports: [
RouterModule.forChild([
{
path: '', component: FeatureListComponent,
children: [
{ path: 'details/:id', component: FeatureDetailsComponent },
]
}
])
],
exports: [
RouterModule
]
})
export class LazyLoadedFeatureRoutingModule { }
@naveedahmed1
I've tried your suggestion on 2 apps: a bigger app, with multiple lazy loaded modules... and in the repo I gave the link to as an example ( https://github.com/MrCroft/lazy )
First case (the bigger app):
After having created routing modules, ng build --aot doesn't throw errors anymore during the build process.
But when browsing through the app, as soon as I try to access a link that needs to load a lazy module, I get this thrown in my browser's console:
EXCEPTION: Uncaught (in promise): Error: Cannot find module 'app/user/user.module.ngfactory'.
Error: Cannot find module 'app/user/user.module.ngfactory'.
Second case (the repo I gave the link to):
Very strange, I'm still getting the same error on build: LazyModule is not an NgModule... I've checked and double-checked, I can't find anything missing.
Make sure you are using latest version of cli. Can you try generating a new project using latest version of cli, then copy code in your src folder to the new project's src folder, compile and check if it works.
@naveedahmed1
My bad... I didn't think there was any difference between previous version and the latest (except only under the hood, but didn't think there would be any in the actual files generated initially). I did an ng init now, overwrote all the files and then put back from history whatever needed to have my old app back. The simple one (the repo) worked right away!
For the larger app - I also had to delete the entire node_modules folder, not just angular and angular-cli (like I did with the small test app). But it works now! Both work.
Thank you!
@MrCroft and @naveedahmed1, the key change was the
"baseUrl": "",
inside the tsconfig.json.
The packages updates + this line solve the LazyModule is not an NgModule error at build.
I can confirm that I've just upgraded to angular-cli 1.0.0-beta.21 and lazy loading is working with AOT <3 ! Big shout out for the team :) !!
I found a problem when running --prod without --aot : Lazy loaded modules are no longer lazy loaded. Am I the only one to have that problem ? https://github.com/angular/angular-cli/issues/3267
@maxime1992 my lazy loading works fine with ng serve --prod with beta.21. Unfortunately, I can only partially validate with --aot. It compiles and serves the first lazy route without issues :smile: Then I run into the first @Effect custom decorator which is stripped out due to #2799 :cry:
Theory of Constraints in action :wink:
Hi @tomwanzek ! Today is your lucky day :) I had exactly the same problem and I made a fork (like 5h ago) of angular-cli that doesn't strip out decorators! Just take a look at the end of #2799 ;) I tried it and it's working fine for me. I also added a bundle size comparison and it's way better than I could imagine. Only +7kb on main bundle. I thought keeping decorators would have a huge impact on bundle size but it's not that bad actually. Plus, I'll give a try to rollup now that AOT is working well and hopefully I'll get even better results!
HTH, I set up a demo repo to summarize how to make lazy routes work with @ngtools/webpack
https://github.com/unsafecode/ng2-aot
I am also getting the same error of:
Error: No module factory available for dependency type: ContextElementDependency
and also using lazy loaded modules... any ideas?
angular-cli: 1.0.0-beta.26
node: 6.9.4
os: win32 x64
@angular/common: 2.4.5
@angular/compiler: 2.4.5
@angular/core: 2.4.5
@angular/forms: 2.4.5
@angular/http: 2.4.5
@angular/language-service: 2.4.5
@angular/platform-browser: 2.4.5
@angular/platform-browser-dynamic: 2.4.5
@angular/router: 3.2.1
@angular/compiler-cli: 2.4.5
Regards,
Checkout the Ultimate Angular 2 Boorstrap App: @ http://ng2.javascriptninja.io Source@ https://github.com/born2net/ng2Boilerplate
@born2net I haven't seen that ContextElementDependency error in a few months now, I didn't think we had it anymore.
Can you open a new issue with a repo I can debug in?