Lazy loading module from library package located in node_modules fails to build.
Bug Report or Feature Request (mark with an x)
- [x] bug report -> please search issues before submitting
- [ ] feature request
Versions.
@angular/cli: 1.0.4
node: 6.10.2
os: win32 x64
@angular/cli: 1.0.4
@angular/common: 4.1.3
@angular/compiler: 4.1.3
@angular/core: 4.1.3
@angular/forms: 4.1.3
@angular/http: 4.1.3
@angular/platform-browser: 4.1.3
@angular/platform-browser-dynamic: 4.1.3
@angular/router: 4.1.3
@angular/compiler-cli: 4.1.3
Repro steps.
I've modified the quick start library from @filipesilva and copied the output folder to my node_modules/quickstart-lib directory.
https://github.com/filipesilva/angular-quickstart-lib
Modification includes a routing module to add a child route.
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { LibComponent } from './component/lib.component';
const childRoutes: Routes = [
{
path: '',
component: LibComponent
}
];
@NgModule({
imports: [
RouterModule.forChild(childRoutes)
],
exports: [
RouterModule
]
})
export class LibRoutingModule { }
which is then imported into the main LibModule file.
import { NgModule } from '@angular/core';
import { LibRoutingModule } from './lib-routing.module';
import { LibComponent } from './component/lib.component';
import { LibService } from './service/lib.service';
@NgModule({
declarations: [LibComponent],
imports: [LibRoutingModule],
providers: [LibService],
exports: [LibComponent]
})
export class LibModule { }
In my host application, I created a simple route to lazy load the module from the library
const routes: Routes = [
{
path: 'quickstart',
loadChildren: 'quickstart-lib#LibModule'
},
{
path: 'preferences',
loadChildren: 'app/preferences#PreferencesModule'
},
...HomeRoutes
];
The log given by the failure.
PS C:\Workspace\scratch\Ng4Starter\host> ng serve
** NG Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200 **
11% building modules 14/23 modules 9 active ...gular\platform-browser-dynamic.es5.js(node:19124) DeprecationWarning: loaderUtils.parseQuery() received a non-string value which can be problematic,
see https://github.com/webpack/loader-utils/issues/56
parseQuery() will be replaced with getOptions() in the next major version of loader-utils.
11% building modules 15/23 modules 8 active ...gular\platform-browser-dynamic.es5.js(node:19124) DeprecationWarning: loaderUtils.parseQuery() received a non-string value which can be problematic,
see https://github.com/webpack/loader-utils/issues/56
parseQuery() will be replaced with getOptions() in the next major version of loader-utils.
Hash: c46342c7aafb5ce0f53e
Time: 14214ms
chunk {0} 0.chunk.js, 0.chunk.js.map 20.5 kB {3} [rendered]
chunk {1} 1.chunk.js, 1.chunk.js.map 793 bytes {0} {3} [rendered]
chunk {2} polyfills.bundle.js, polyfills.bundle.js.map (polyfills) 157 kB {7} [initial] [rendered]
chunk {3} main.bundle.js, main.bundle.js.map (main) 72.5 kB {6} [initial] [rendered]
chunk {4} styles.bundle.js, styles.bundle.js.map (styles) 229 kB {7} [initial] [rendered]
chunk {5} scripts.bundle.js, scripts.bundle.js.map (scripts) 162 kB {7} [initial] [rendered]
chunk {6} vendor.bundle.js, vendor.bundle.js.map (vendor) 3.36 MB [initial] [rendered]
chunk {7} inline.bundle.js, inline.bundle.js.map (inline) 0 bytes [entry] [rendered]
ERROR in ./~/quickstart-lib/quickstart-lib.d.ts
Module build failed: Error: Debug Failure. False expression: Output generation failed:=>C:/Workspace/scratch/Ng4Starter/host/node_modules/quickstart-lib/quickstart-lib.d.ts
at Object.assert (C:\Workspace\scratch\Ng4Starter\host\node_modules\typescript\lib\typescript.js:3259:23)
at Object.transpileModule (C:\Workspace\scratch\Ng4Starter\host\node_modules\typescript\lib\typescript.js:76181:18)
at TypeScriptFileRefactor.transpile (C:\Workspace\scratch\Ng4Starter\host\node_modules\@ngtools\webpack\src\refactor.js:161:27)
at Promise.resolve.then.then.then.then (C:\Workspace\scratch\Ng4Starter\host\node_modules\@ngtools\webpack\src\loader.js:361:37)
at process._tickCallback (internal/process/next_tick.js:109:7)
@ ./src async
@ ./~/@angular/core/@angular/core.es5.js
@ ./src/main.ts
@ multi webpack-dev-server/client?http://localhost:4200 ./src/main.ts
webpack: Failed to compile.
Desired functionality.
I would like to distribute my code as an NPM library and have the CLI support lazy loading the module that is located in the 'node_modules' directory.
Modules located in the same application directory are lazy loaded just fine.
Mention any other details that might be useful.
https://github.com/filipesilva/angular-quickstart-lib
I'm having the same issue with version:
@angular/cli: 1.0.4
@angular: 4.0.0
Hopefully this can be fixed soon, I'll continue by using --no-aot for the moment.
@mdewaelebe doesn't the CLI default to JIT? This error happens for me even if I use --aot false. I don't see a --no-aot option.
@thekhoi I tried the approach described in #5153. It seems you're right, the --no-aot option is not found anywhere, but it does the trick for me.
ng build --prod : fails
ng build --prod --no-aot : succeeds
I did no further investigation on why or how this works.
@mdewaelebe I think that is a different issue. I get the same error for a lazy route that happens to be local to my project directory or not.
ERROR in ./src/$$_gendir async
Module not found: Error: Can't resolve 'C:\Workspace\scratch\Ng4Starter\host\src\$$_gendir\app\foo\index.ngfactory.ts' in 'C:\Workspace\scratch\Ng4Starter\host\src\$$_gendir'
@ ./src/$$_gendir async
@ ./~/@angular/core/@angular/core.es5.js
@ ./src/main.ts
@ multi webpack-dev-server/client?http://localhost:4200 ./src/main.ts
webpack: Failed to compile.
I was able to track down the issue. It appears that the AoT plugin discovers lazy routes and passes them to the angular compiler language service to resolve the file name of a given route string. Assuming your library is following the angular package format, this resolves to a definition file, e.g. c:...\node_modules\lib-name\lib-name.d.ts.
The file @ngtools\webpack\src\loader.js then uses this path to transpile the source code to es2015 which fails. I modified this file to check for the *.d.ts extension. If there is a match I just use the contents of the pre-compiled .js and .map files for the library. This lets me get past the compilation error in the CLI and when watching the network traffic, I can see the module is loaded on demand.
However, this is another hickup, the library is already bundled in the vendor.js file since the webpack configuration simply bundles everything in the node_modules directory into a the vendors file.
The only way to get around this, is to eject out of the CLI to exclude the library from the common vendor chunk.
All these fixes seem to be a hack, hopefully the cli team can come up with something more elegant.
https://github.com/angular/angular-cli/issues/6755 contains a nice repro of this issue.
Hello @filipesilva, I have the same issue. Is there any news about a workaround?
Thanks,
ERROR in ./~/@project/myLib/lib/index.d.ts
Module build failed: Error: Debug Failure. False expression: Output generation failed
at Object.assert (<path>\node_modules\typescript\lib\typescript.js:3329:23)
at Object.transpileModule (<path>\node_modules\typescript\lib\typescript.js:80449:18)
at TypeScriptFileRefactor.transpile (<path>\node_modules\@ngtools\webpack\src\refactor.js:161:27)
at Promise.resolve.then.then.then.then (<path>\node_modules\@ngtools\webpack\src\loader.js:378:37)
at process._tickCallback (internal/process/next_tick.js:103:7)
@ ./src async
@ ./~/@angular/core/@angular/core.es5.js
@ ./src/main.ts
@ multi ./src/main.ts
@angular/[email protected] typescript@~2.3.3
const routes: Routes = [
{
path: 'myRoute', component: MyComponent,
children: [
{ path: "user", loadChildren: "@project/myLib#MyModule" }
]
}
];
I've got work around for this which works with uirouter/angular-hybrid (in AOT mode too) but might work with angular router too.
So, let's say I've got a library based on angular-quickstart-lib (let's call it quickstart-lib) which has LibModule that I want to lazy load. To do so, I added wrapper around library which lives in Angular CLI based host project.
So, in app.module.ts of host, instead of:
@NgModule({
imports: [
UIRouterModule.forChild({
states: [
{
name: 'quickstart.**',
url: 'quickstart',
loadChildren: 'quickstart-lib#LibModule'
}
]}),
]
})
export class AppModule
I've got:
libModuleLazyLoader.ts
import { NgModule } from "@angular/core";
import { LibModule } from 'quickstart-lib';
@NgModule({
imports: [
LibModule
]
})
export class QuickstartLibLazyLoader {}
and then, in app.module.ts, I've got:
@NgModule({
imports: [
UIRouterModule.forChild({
states: [
{
name: 'quickstart.**',
url: 'quickstart',
loadChildren: './libModuleLazyLoader#QuickstartLibLazyLoader'
}
]}),
]
})
export class AppModule
Downside is that you'd need wrapper per library with this work around.
hope this helps someone :)
@adamlubek it works! Thanks!
Using a different router isn't helpful and I didn't think the CLI supported anything other than the default router. What is the solution to this problem?
@babeal yes, instead of default Angular router you can use https://github.com/ui-router/angular for pure Angular project or https://github.com/ui-router/angular-hybrid for hybrid AngularJs/Angular project
That said, it looks like @Chabane is using default router and got it working. @Chabane , is that correct?
@adamlubek yes perfect!
Related to https://github.com/angular/angular-cli/issues/5986.
The key to work around from @adamlubek is the 'local' module, QuickstartLibLazyLoader, imports the installed 'LibModule'.
This mostly works however it breaks if you try to do a production build.
ng build --prod
Turning off AOT will suppress the issue
ng build --prod --aot false
@thekhoi , that's strange as my workaround works for me in production build with AOT. Does your library work with AOT when it's not lazy loaded using my work around?
Maybe you have non AOT compliant code somewhere in your library?
@adamlubek , you're right. It looks like there was an issue with the node package I created for testing. My package name was scoped @foo/bar but the "importAs" attribute in my bar.metadata.json file was set to "bar".
I have the same issue. @adamlubek s workaround worked for me. Is this issue going to get resolved?
Is it going to be fixed?
I had similar problem. Solution can be found in this stackoverflow answer:
https://stackoverflow.com/questions/46774625/aot-compiler-include-lazy-loaded-external-module
And in my Angular boilerplate repository:
https://github.com/maciejtreder/angular-universal-pwa
Is this issue going to be fixed?