This repository has been archived by the owner on Nov 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ng-module-map-ngfactory-loader): introduce ng-module-map-ngfacto…
…ry-loader
- Loading branch information
1 parent
4e5b3c4
commit 6720233
Showing
9 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# 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 with `@angular/cli` | ||
|
||
`npm install @nguniversal/module-map-ngfactory-loader --save` | ||
|
||
`@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) | ||
] | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './src'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
45 changes: 45 additions & 0 deletions
45
modules/ng-module-map-ngfactory-loader/src/module-map-loader.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
] | ||
}; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
modules/ng-module-map-ngfactory-loader/src/module-map-ngfactory-loader.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters