Toggle navigation
P3X GitList Snapshot
GitHub
Repo
Changelog
To do
Releases
Themes
Change log
Loading change log ...
To do ...
Loading todo ...
browsing:
f35b332db486fef55ab0b6d0ad1c79f5541a74a0
Branches
master
Tags
1.1.129-287
1.1.113-149
1.1.108-143
1.1.95-138
1.1.92-119
1.0.35-18
1.0.13-14
Files
Commits
Log
Graph
Stats
angular-compile.git
src
CompileService.ts
RSS
Git
Fetch origin
Download
ZIP
TAR
Clone
Raw
View
History
Clone
SSH
HTTPS
Blames found: 29
Mode: application/typescript
Binary: false
Hang on, we reloading big blames...
f46e4d96
import { Component, NgModule, Injectable, Compiler, ViewContainerRef, ModuleWithProviders, Type,
69db3407
Optional
f46e4d96
} from '@angular/core';
69db3407
import { CommonModule } from '@angular/common'; import { BrowserModule } from '@angular/platform-browser';
f46e4d96
db3e80f5
import { cloneDeep } from 'lodash';
f46e4d96
export interface CompileOptions { template: string; container: ViewContainerRef; imports?: Array<Type<any> | ModuleWithProviders | any[]>; context?: any, onCompiled?: Function, onError?: Function;
69db3407
module?: NgModule;
f46e4d96
} const cache : any = {};
69db3407
export class CompileServiceConfig { module: NgModule } let SingletonDefaultModule: NgModule;
f46e4d96
@Injectable() export class CompileService { constructor( private compiler: Compiler,
69db3407
@Optional() config: CompileServiceConfig,
f46e4d96
) {
52297b81
if (config !== undefined && config !== null) { if (config.module !== undefined && config.module !== null) { SingletonDefaultModule = config.module; }
69db3407
}
f46e4d96
} public async compile(opts: CompileOptions) { try { const factory = await this.createFactory(opts); opts.container.clear(); const cmp : any = opts.container.createComponent(factory); cmp.instance.context = opts.context; } catch (e) { if (opts.onError) { opts.onError(e) } else { console.error(e); } } } private async createFactory(opts: CompileOptions) { const cacheKey = opts.template; if (Object.keys(cache).indexOf(cacheKey) > -1) { return cache[cacheKey]; }
52297b81
8e7c9bcf
cache[cacheKey] = (async() => {
69db3407
8e7c9bcf
try { @Component({ template: opts.template }) class TemplateComponent { context: any }
69db3407
8e7c9bcf
let module : NgModule = {}; if (opts.module !== undefined) {
db3e80f5
module = cloneDeep(opts.module);
8e7c9bcf
} else if (SingletonDefaultModule !== undefined && SingletonDefaultModule !== null) {
db3e80f5
module = cloneDeep(SingletonDefaultModule);
8e7c9bcf
} module.imports = module.imports || []; module.imports.push( CommonModule ); module.imports.push( BrowserModule ); if (opts.imports !== undefined) { module.imports = module.imports.concat(opts.imports) } if (module.declarations === undefined) { module.declarations = [ TemplateComponent ]; } else { module.declarations.push(TemplateComponent); } @NgModule(module) class TemplateModule { } const component = await this.compiler.compileModuleAndAllComponentsAsync(TemplateModule); const factory = component.componentFactories.find((comp) => comp.componentType === TemplateComponent ); cache[cacheKey] = factory; if (opts.onCompiled) { opts.onCompiled(component); } return factory; } catch (e) { delete cache[cacheKey]; throw e;
f46e4d96
}
8e7c9bcf
})();
f46e4d96
return cache[cacheKey]; } }