Skip to content

Instantly share code, notes, and snippets.

@brandonroberts
Last active April 26, 2019 12:40
Show Gist options
  • Star 31 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save brandonroberts/02cc07face25886fe142c4dbd8da1340 to your computer and use it in GitHub Desktop.
Save brandonroberts/02cc07face25886fe142c4dbd8da1340 to your computer and use it in GitHub Desktop.
Webpack Async NgModule Loader
import {Injectable, NgModuleFactory, NgModuleFactoryLoader, Compiler, Type} from '@angular/core';
class LoaderCallback {
constructor(public callback) {}
}
export let load: Type = (callback: Function) => {
return new LoaderCallback(callback);
};
/**
* NgModuleFactoryLoader that uses Promise to load NgModule type and then compiles them.
* @experimental
*/
@Injectable()
export class AsyncNgModuleLoader implements NgModuleFactoryLoader {
constructor(private compiler: Compiler) {}
load(modulePath: string|LoaderCallback): Promise<NgModuleFactory<any>> {
if (modulePath instanceof LoaderCallback) {
let loader = (modulePath as LoaderCallback).callback();
return Promise
.resolve(loader)
.then((type: any) => checkNotEmpty(type, '', ''))
.then((type: any) => this.compiler.compileModuleAsync(type));
}
return Promise.resolve(null);
}
}
function checkNotEmpty(value: any, modulePath: string, exportName: string): any {
if (!value) {
throw new Error(`Cannot find '${exportName}' in '${modulePath}'`);
}
return value;
}
import {NgModuleFactoryLoader} from '@angular/core';
import {AsyncNgModuleLoader} from './async-ng-module-loader';

// Add to main providers
{provide: NgModuleFactoryLoader, useClass: AsyncNgModuleLoader}
import {load} from './async-ng-module-loader';

{
  path: 'lazy',
  loadChildren: load(() => new Promise(resolve => {
      (require as any).ensure([], require => {
        resolve(require('./lazy-module').LazyModule);
      })
    }))
},
@mineofcode
Copy link

mineofcode commented Nov 5, 2016

export let load: Type = (callback: Function) => {
return new LoaderCallback(callback);
};

Generic type 'Type' requires 1 type argument(s)

Getting above error while compile with ng serve

node -v 7.0.0
npm -v 3.10.8
angular-cli: 1.0.0-beta.19-3
editor : Visual Studio Code

please help me...

"dependencies": {
"@angular/common": "~2.1.0",
"@angular/compiler": "~2.1.0",
"@angular/core": "~2.1.0",
"@angular/forms": "~2.1.0",
"@angular/http": "~2.1.0",
"@angular/platform-browser": "~2.1.0",
"@angular/platform-browser-dynamic": "~2.1.0",
"@angular/router": "~3.1.0",
"bootstrap": "^3.3.7",
"core-js": "^2.4.1",
"jquery": "^3.1.1",
"rxjs": "5.0.0-beta.12",
"ts-helpers": "^1.1.1",
"zone.js": "^0.6.23"
},

Copy link

ghost commented Mar 22, 2017

Same error than @masagatech trying to fix it right now.

@haisaco
Copy link

haisaco commented Apr 11, 2017

same error @masagatech trying to fix it right now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment