2016-06-08 19:38:52 -04:00
|
|
|
import {CompileDirectiveMetadata, CompileTemplateMetadata, CompileTypeMetadata} from '@angular/compiler/src/compile_metadata';
|
|
|
|
import {CompilerConfig} from '@angular/compiler/src/config';
|
|
|
|
import {DirectiveNormalizer} from '@angular/compiler/src/directive_normalizer';
|
2016-04-28 20:50:03 -04:00
|
|
|
import {Lexer} from '@angular/compiler/src/expression_parser/lexer';
|
2016-06-08 19:38:52 -04:00
|
|
|
import {Parser} from '@angular/compiler/src/expression_parser/parser';
|
2016-04-28 20:50:03 -04:00
|
|
|
import {HtmlParser} from '@angular/compiler/src/html_parser';
|
2016-06-08 19:38:52 -04:00
|
|
|
import {NormalizedComponentWithViewDirectives, OfflineCompiler, SourceModule} from '@angular/compiler/src/offline_compiler';
|
|
|
|
import {OutputEmitter} from '@angular/compiler/src/output/abstract_emitter';
|
|
|
|
import {ImportGenerator} from '@angular/compiler/src/output/path_util';
|
2016-04-28 20:50:03 -04:00
|
|
|
import {StyleCompiler} from '@angular/compiler/src/style_compiler';
|
2016-06-08 19:38:52 -04:00
|
|
|
import {TemplateParser} from '@angular/compiler/src/template_parser';
|
2016-04-28 20:50:03 -04:00
|
|
|
import {createOfflineCompileUrlResolver} from '@angular/compiler/src/url_resolver';
|
|
|
|
import {MODULE_SUFFIX} from '@angular/compiler/src/util';
|
2016-06-08 19:38:52 -04:00
|
|
|
import {ViewCompiler} from '@angular/compiler/src/view_compiler/view_compiler';
|
2016-01-06 17:13:44 -05:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
import {Console} from '../core_private';
|
|
|
|
import {IS_DART, isPresent, print} from '../src/facade/lang';
|
|
|
|
import {MockSchemaRegistry} from '../testing/schema_registry_mock';
|
|
|
|
import {MockXHR} from '../testing/xhr_mock';
|
2016-04-28 20:50:03 -04:00
|
|
|
|
2016-01-06 17:13:44 -05:00
|
|
|
|
|
|
|
export class CompA { user: string; }
|
|
|
|
|
2016-05-02 12:38:46 -04:00
|
|
|
var THIS_MODULE_PATH = `asset:@angular/lib/compiler/test`;
|
2016-01-06 17:13:44 -05:00
|
|
|
var THIS_MODULE_URL = `${THIS_MODULE_PATH}/offline_compiler_util${MODULE_SUFFIX}`;
|
|
|
|
|
|
|
|
export var compAMetadata = CompileDirectiveMetadata.create({
|
|
|
|
isComponent: true,
|
|
|
|
selector: 'comp-a',
|
|
|
|
type: new CompileTypeMetadata(
|
|
|
|
{name: 'CompA', moduleUrl: THIS_MODULE_URL, runtime: CompA, diDeps: []}),
|
|
|
|
template: new CompileTemplateMetadata({
|
|
|
|
templateUrl: './offline_compiler_compa.html',
|
|
|
|
styles: ['.redStyle { color: red; }'],
|
2016-05-02 12:38:46 -04:00
|
|
|
styleUrls: ['./offline_compiler_compa.css']
|
2016-01-06 17:13:44 -05:00
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
function _createOfflineCompiler(xhr: MockXHR, emitter: OutputEmitter): OfflineCompiler {
|
|
|
|
var urlResolver = createOfflineCompileUrlResolver();
|
|
|
|
xhr.when(`${THIS_MODULE_PATH}/offline_compiler_compa.html`, 'Hello World {{user}}!');
|
|
|
|
var htmlParser = new HtmlParser();
|
2016-06-13 13:06:40 -04:00
|
|
|
var config = new CompilerConfig({genDebugInfo: true, useJit: true});
|
|
|
|
var normalizer = new DirectiveNormalizer(xhr, urlResolver, htmlParser, config);
|
2016-01-06 17:13:44 -05:00
|
|
|
return new OfflineCompiler(
|
2016-06-08 19:38:52 -04:00
|
|
|
normalizer,
|
|
|
|
new TemplateParser(
|
|
|
|
new Parser(new Lexer()), new MockSchemaRegistry({}, {}), htmlParser, new Console(), []),
|
2016-06-13 13:06:40 -04:00
|
|
|
new StyleCompiler(urlResolver), new ViewCompiler(config), emitter, xhr);
|
2016-01-06 17:13:44 -05:00
|
|
|
}
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
export function compileComp(
|
|
|
|
emitter: OutputEmitter, comp: CompileDirectiveMetadata): Promise<string> {
|
2016-01-06 17:13:44 -05:00
|
|
|
var xhr = new MockXHR();
|
|
|
|
var compiler = _createOfflineCompiler(xhr, emitter);
|
|
|
|
var result = compiler.normalizeDirectiveMetadata(comp).then((normComp) => {
|
|
|
|
return compiler.compileTemplates([new NormalizedComponentWithViewDirectives(normComp, [], [])])
|
|
|
|
.source;
|
|
|
|
});
|
|
|
|
xhr.flush();
|
|
|
|
return result;
|
|
|
|
}
|
2016-05-02 12:38:46 -04:00
|
|
|
|
|
|
|
export class SimpleJsImportGenerator implements ImportGenerator {
|
|
|
|
getImportPath(moduleUrlStr: string, importedUrlStr: string): string {
|
|
|
|
// var moduleAssetUrl = ImportGenerator.parseAssetUrl(moduleUrlStr);
|
|
|
|
var importedAssetUrl = ImportGenerator.parseAssetUrl(importedUrlStr);
|
|
|
|
if (isPresent(importedAssetUrl)) {
|
|
|
|
return `${importedAssetUrl.packageName}/${importedAssetUrl.modulePath}`;
|
|
|
|
} else {
|
|
|
|
return importedUrlStr;
|
|
|
|
}
|
|
|
|
}
|
2016-06-15 09:58:57 -04:00
|
|
|
}
|