|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | +import {ResourceLoader} from '@angular/compiler'; |
| 9 | +import {Compiler, Type, NgModuleFactory, CompilerFactory, StaticProvider} from '@angular/core'; |
| 10 | +import {INITIAL_CONFIG, renderModuleFactory, platformDynamicServer} from '@angular/platform-server'; |
| 11 | +import * as fs from 'fs'; |
| 12 | + |
| 13 | +import {FileLoader} from './file-loader'; |
| 14 | +import {RenderOptions} from './interfaces'; |
| 15 | + |
| 16 | +/** |
| 17 | + * A common rendering engine utility. This abstracts the logic |
| 18 | + * for handling the platformServer compiler, the module cache, and |
| 19 | + * the document loader |
| 20 | + */ |
| 21 | +export class CommonEngine { |
| 22 | + |
| 23 | + /** Return an instance of the platformServer compiler */ |
| 24 | + getCompiler(): Compiler { |
| 25 | + const compilerFactory: CompilerFactory = platformDynamicServer().injector.get(CompilerFactory); |
| 26 | + return compilerFactory.createCompiler([ |
| 27 | + {providers: [{provide: ResourceLoader, useClass: FileLoader, deps: []}]} |
| 28 | + ]); |
| 29 | + } |
| 30 | + |
| 31 | + private factoryCacheMap = new Map<Type<{}>, NgModuleFactory<{}>>(); |
| 32 | + private templateCache: {[key: string]: string} = {}; |
| 33 | + |
| 34 | + constructor(private moduleOrFactory: Type<{}> | NgModuleFactory<{}>, |
| 35 | + private providers: StaticProvider[] = []) {} |
| 36 | + |
| 37 | + /** |
| 38 | + * Render an HTML document for a specific URL with specified |
| 39 | + * render options |
| 40 | + */ |
| 41 | + render(filePath: string, opts: RenderOptions): Promise<string> { |
| 42 | + const extraProviders = [ |
| 43 | + ...(opts.providers || []), |
| 44 | + ...(this.providers || []), |
| 45 | + [ |
| 46 | + { |
| 47 | + provide: INITIAL_CONFIG, |
| 48 | + useValue: { |
| 49 | + document: opts.document || this.getDocument(filePath), |
| 50 | + url: opts.url |
| 51 | + } |
| 52 | + } |
| 53 | + ] |
| 54 | + ]; |
| 55 | + |
| 56 | + return this.getFactory() |
| 57 | + .then(factory => renderModuleFactory(factory, {extraProviders})); |
| 58 | + } |
| 59 | + |
| 60 | + /** Return the factory for a given engine instance */ |
| 61 | + getFactory(): Promise<NgModuleFactory<{}>> { |
| 62 | + // If module has been compiled AoT |
| 63 | + const moduleOrFactory = this.moduleOrFactory; |
| 64 | + if (moduleOrFactory instanceof NgModuleFactory) { |
| 65 | + return Promise.resolve(moduleOrFactory); |
| 66 | + } else { |
| 67 | + // we're in JIT mode |
| 68 | + let moduleFactory = this.factoryCacheMap.get(moduleOrFactory); |
| 69 | + |
| 70 | + // If module factory is cached |
| 71 | + if (moduleFactory) { |
| 72 | + return Promise.resolve(moduleFactory); |
| 73 | + } |
| 74 | + |
| 75 | + // Compile the module and cache it |
| 76 | + return this.getCompiler().compileModuleAsync(moduleOrFactory) |
| 77 | + .then((factory) => { |
| 78 | + this.factoryCacheMap.set(moduleOrFactory, factory); |
| 79 | + return factory; |
| 80 | + }); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** Retrieve the document from the cache or the filesystem */ |
| 85 | + private getDocument(filePath: string): Promise<string> { |
| 86 | + const doc = this.templateCache[filePath] = this.templateCache[filePath] || |
| 87 | + fs.readFileSync(filePath).toString(); |
| 88 | + |
| 89 | + // As promise so we can change the API later without breaking |
| 90 | + return Promise.resolve(doc); |
| 91 | + } |
| 92 | +} |
0 commit comments