Skip to content
This repository has been archived by the owner on Nov 22, 2024. It is now read-only.

Commit

Permalink
feat(ng-module-map-ngfactory-loader): introduce ng-module-map-ngfacto…
Browse files Browse the repository at this point in the history
…ry-loader
  • Loading branch information
FrozenPandaz committed Jul 25, 2017
1 parent 4e5b3c4 commit 51d07af
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 0 deletions.
5 changes: 5 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ npm run build:ng-aspnetcore-engine

cp modules/ng-aspnetcore-engine/package.json dist/ng-aspnetcore-engine/package.json
cp modules/ng-aspnetcore-engine/README.md dist/ng-aspnetcore-engine/README.md

npm run build:ng-module-map-ngfactory-loader

cp modules/ng-module-map-ngfactory-loader/package.json dist/ng-module-map-ngfactory-loader/package.json
cp modules/ng-module-map-ngfactory-loader/README.md dist/ng-module-map-ngfactory-loader/README.md
34 changes: 34 additions & 0 deletions modules/ng-module-map-ngfactory-loader/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Module Map NgFactory Loader

This is a NgFactory Loader which uses a map of modules instead of resolving modules lazily.

This is useful when executing in node because lazy loading serves no purpose

## Usage

`npm install @nguniversal/module-map-ngfactory-loader --save`

To use it, import the module and provide a module map as well

```ts
@NgModule({
imports: [ModuleMapLoaderModule.withMap(LAZY_MODULE_MAP]
})
```
## Usage with `@angular/cli`
`@angular/cli` will generate this map in its main output bundle if you put app.platform = 'server'.
```ts
const { provideModuleMap } = require('@nguniversal/module-map-ngfactory-loader');
const { AppModuleNgFactory, LAZY_MODULE_MAP } = require('main.bundle.js');

renderModuleFactory(AppModuleNgFactory, {
document: '<app-root></app-root>',
url: '/',
extraProviders: [
provideModuleMap(LAZY_ROUTE_MAP)
]
})
```
1 change: 1 addition & 0 deletions modules/ng-module-map-ngfactory-loader/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './src';
22 changes: 22 additions & 0 deletions modules/ng-module-map-ngfactory-loader/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "@nguniversal/module-map-ngfactory-loader",
"main": "index.js",
"types": "index.d.ts",
"version": "1.0.0-beta.0",
"description": "NgFactoryLoader which uses a Map to load ngfactories without lazy loading",
"homepage": "https://github.com/angular/universal",
"license": "MIT",
"contributors": [
"FrozenPandaz"
],
"repository": {
"type": "git",
"url": "https://github.com/angular/universal"
},
"bugs": {
"url": "https://github.com/angular/universal/issues"
},
"peerDependencies": {
"@angular/core": "^4.0.0"
}
}
2 changes: 2 additions & 0 deletions modules/ng-module-map-ngfactory-loader/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './module-map-loader.module';
export * from './module-map-ngfactory-loader';
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { NgModule, NgModuleFactoryLoader, ModuleWithProviders, Provider } from '@angular/core';

import { ModuleMapNgFactoryLoader, ModuleMap, MODULE_MAP } from './module-map-ngfactory-loader';

/**
* Helper function for getting the providers object for the MODULE_MAP
*
* @param {ModuleMap} moduleMap Map to use as a value for MODULE_MAP
*/
export function provideModuleMap(moduleMap: ModuleMap): Provider {
return {
provide: MODULE_MAP,
useValue: moduleMap
};
}

/**
* Module for using a NgModuleFactoryLoader which does not lazy load
*/
@NgModule({
providers: [
{
provide: NgModuleFactoryLoader,
useClass: ModuleMapNgFactoryLoader
}
]
})
export class ModuleMapLoaderModule {
/**
* Returns a ModuleMapLoaderModule along with a MODULE_MAP
*
* @param {ModuleMap} moduleMap Map to use as a value for MODULE_MAP
*/
static withMap(moduleMap: ModuleMap): ModuleWithProviders {
return {
ngModule: ModuleMapLoaderModule,
providers: [
{
provide: MODULE_MAP,
useValue: moduleMap
}
]
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Injectable, NgModuleFactoryLoader, InjectionToken, NgModuleFactory, Inject, Type, Compiler } from '@angular/core';

/**
* A map key'd by loadChildren strings and Modules or NgModuleFactories as vaules
*/
export type ModuleMap = {
[key: string]: Type<any> | NgModuleFactory<any>;
};

/**
* Token used by the ModuleMapNgFactoryLoader to load modules
*/
export const MODULE_MAP: InjectionToken<ModuleMap> = new InjectionToken('MODULE_MAP');

/**
* NgModuleFactoryLoader which does not lazy load
*/
@Injectable()
export class ModuleMapNgFactoryLoader implements NgModuleFactoryLoader {
constructor(private compiler: Compiler, @Inject(MODULE_MAP) private moduleMap: ModuleMap) { }

load(loadChildrenString: string): Promise<NgModuleFactory<any>> {
const offlineMode = this.compiler instanceof Compiler;
const type = this.moduleMap[loadChildrenString];

if (!type) {
throw new Error(`${loadChildrenString} did not exist in the MODULE_MAP`);
}

return offlineMode ? this.loadFactory(<NgModuleFactory<any>> type) : this.loadAndCompile(<Type<any>> type);
}

private loadFactory(factory: NgModuleFactory<any>): Promise<NgModuleFactory<any>> {
return new Promise(resolve => resolve(factory));
}

private loadAndCompile(type: Type<any>): Promise<NgModuleFactory<any>> {
return this.compiler.compileModuleAsync(type);
}
}
12 changes: 12 additions & 0 deletions modules/ng-module-map-ngfactory-loader/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/ng-module-map-ngfactory-loader"
},
"angularCompilerOptions": {
"genDir": "ngfactory"
},
"files": [
"index.ts"
]
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"jasmine": "jasmine",
"build:ng-express-engine": "ngc -p modules/ng-express-engine/tsconfig.json",
"build:ng-aspnetcore-engine": "ngc -p modules/ng-aspnetcore-engine/tsconfig.json",
"build:ng-module-map-ngfactory-loader": "ngc -p modules/ng-module-map-ngfactory-loader/tsconfig.json",
"build": "./build.sh",
"test": "exit 0"
},
Expand Down

0 comments on commit 51d07af

Please sign in to comment.