angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Lazy loading module from library package located in node_modules fails to build.

Open thekhoi opened this issue 9 years ago • 80 comments

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

thekhoi avatar May 18 '17 18:05 thekhoi

@maciejtreder

your solution https://github.com/maciejtreder/angular-universal-serverless doesn't use angular-cli

I tried your workaround with angular-cli.It didn't work. I got the same error "Module build failed: Error: Debug Failure. False expression: Output generation failed"

Can you tell us how can we make it work with angular-cli

Web2Integrators avatar Oct 22 '17 16:10 Web2Integrators

Any news?

ghost avatar Nov 14 '17 18:11 ghost

Any new guys?

rhinoandre avatar Dec 09 '17 14:12 rhinoandre

@adamlubek For my situation the workaround is not working. I have a local module which imports another module from the node_modules folder. The chunking works however my chunk only contains the import. The code of the imported module ends up in the vendor.bundle.js.

Am I doing something wrong?

appienvanveen avatar Jan 09 '18 14:01 appienvanveen

@appienvanveen The modules show up in the vendor bundle if it's referenced in more than one place. Make sure your imported module isn't referenced outside elsewhere.

-Khoi

thekhoi avatar Jan 09 '18 15:01 thekhoi

@thekhoi Thanks for your reply. When I remove the routing/loading of the lazy module, the module does not get loaded in de vendor.bundle.js. This would mean it isn't referenced elsewhere, does it?

appienvanveen avatar Jan 10 '18 06:01 appienvanveen

@appienvanveen sorry but I don't follow your example.

Generally though, in order for lazy loading to work properly, you can't have code (that you want lazy loaded) referenced anywhere in your code base through import statement (as it would 'tell' cli to eagerly load the code). You have to have your module referenced ONLY using config object passed into router module setup. So, in my earlier comment here (https://github.com/angular/angular-cli/issues/6373#issuecomment-319116889), the crucial bit is:

loadChildren: './libModuleLazyLoader#QuickstartLibLazyLoader'

adamlubek avatar Jan 10 '18 14:01 adamlubek

@adamlubek Thanks for replying. I'm pretty sure my module isn't referenced elsewhere. Hopefully the code below will clear things up!

app-routing.module.ts <-- My application routing

import {Route, RouterModule} from '@angular/router';

import {ModuleWithProviders} from '@angular/core';
import {HomeComponent} from './home/home.component';

export const APP_ROUTES: Route[] = [
  { path: 'pas', loadChildren: './pas-lazy.module#PasLazyModule' },
  {
    path: '',
    component: HomeComponent
  }
];

pas-lazy.module.ts <-- This is my 'wrapper' module; this module isn't referenced elsewhere.

import { NgModule } from '@angular/core';
import {PasModule} from '@xyz/pas';

@NgModule({
  imports: [
    PasModule   ///<-- this module is imported from node_modules which I want to be lazy loaded
  ],
  declarations: []
})
export class PasLazyModule { }

appienvanveen avatar Jan 11 '18 10:01 appienvanveen

@appienvanveen how do you pass your APP_ROUTES to RouterModule? Are you using:

RouterModule.forChild(APP_ROUTES);

?

adamlubek avatar Jan 11 '18 13:01 adamlubek

Ah, I noticed your using UIroutermodule, which I do not. It looks like it does not work without UIroutermodule.

appienvanveen avatar Jan 19 '18 09:01 appienvanveen

@Chabane here confirmed that he got it working with default angular router too (see https://github.com/angular/angular-cli/issues/6373#issuecomment-322430684)

also just to double check, are you using RouterModule.forChild(APP_ROUTES); ?

adamlubek avatar Jan 19 '18 09:01 adamlubek

In my main app.module i'm using RouterModule.forRoot(APP_ROUTES),

If I change this in forChild I get the error: No provider for ChildrenOutletContexts!

appienvanveen avatar Jan 19 '18 10:01 appienvanveen

I think this is where your problem is. Official documentation and other resources (e.g. rangle.io) recommend using forChild for all feature modules:

https://angular.io/guide/lazy-loading-ngmodules#forroot-and-forchild

The CLI also adds RouterModule.forChild(routes) to feature routing modules. This way, Angular knows that the route list is only responsible for providing additional routes and is intended for feature modules.

https://angular-2-training-book.rangle.io/handout/modules/lazy-loading-module.html

Notice that we use the method call forChild instead of forRoot to create the routing object. We should always do that when creating a routing object for a feature module, no matter if the module is supposed to be eagerly or lazily loaded.

So, I'd try using forChild and figure out how to solve your No provider for ChildrenOutletContexts! error

adamlubek avatar Jan 19 '18 10:01 adamlubek

@Chabane here confirmed that he got it working with default angular router too (see #6373 (comment)) also just to double check, are you using RouterModule.forChild(APP_ROUTES); ?

@adamlubek .. I can confirm that this is working by creating a wrapper module and with RouterModule.forChild

But whats the thought process towards accepting a direct link from node_modules in loadChildren? I feel this seems to be more friendly..

snarun avatar Jan 27 '18 22:01 snarun

@hansl Can #7659 be merged? This looks like the missing piece to fix this issue.

admosity avatar Jan 31 '18 15:01 admosity

Can confirm that @adamlubek's workaround works. I can also confirm that the loaded package appears both in the vendor chunk and the lazy loaded chunk as mentioned by @thekhoi.

admosity avatar Jan 31 '18 18:01 admosity

We are still facing the issue, using another router-lib is not a solution. Any traction on this issue??

pratikthecook avatar Feb 14 '18 04:02 pratikthecook

@adamlubek's solution is pretty elegant but,... as @Web2Integrators said, not use angular-cli; MY situation is more or less the same as @appienvanveen ; (application with an external feature module that via loadchildren try to load an external feature module);

tsc compiler with SystemJs builer is, ...so far, mine state of art;

waiting for do the same even with angular-cli;

82antu avatar Feb 14 '18 12:02 82antu

Is there any solution for this yet from angular-cli?

khanakifalam002 avatar Feb 16 '18 07:02 khanakifalam002

Hello, I believe Angular applications are going to be bigger and bigger. So this issue becomes more and more important. Is there any solution for this yet from angular-cli?

avin-shum avatar Feb 23 '18 09:02 avin-shum