UNPKG

4.19 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright Google Inc. 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"use strict";
9/**
10 * Extract i18n messages from source code
11 */
12// Must be imported first, because angular2 decorators throws on load.
13require('reflect-metadata');
14var compiler = require('@angular/compiler');
15var core_1 = require('@angular/core');
16var codegen_1 = require('./codegen');
17var reflector_host_1 = require('./reflector_host');
18var static_reflection_capabilities_1 = require('./static_reflection_capabilities');
19var static_reflector_1 = require('./static_reflector');
20var Extractor = (function () {
21 function Extractor(options, program, host, staticReflector, messageBundle, reflectorHost, metadataResolver) {
22 this.options = options;
23 this.program = program;
24 this.host = host;
25 this.staticReflector = staticReflector;
26 this.messageBundle = messageBundle;
27 this.reflectorHost = reflectorHost;
28 this.metadataResolver = metadataResolver;
29 }
30 Extractor.prototype.extract = function () {
31 var _this = this;
32 var programSymbols = codegen_1.extractProgramSymbols(this.program, this.staticReflector, this.reflectorHost, this.options);
33 var _a = compiler.analyzeAndValidateNgModules(programSymbols, { transitiveModules: true }, this.metadataResolver), ngModules = _a.ngModules, files = _a.files;
34 return compiler.loadNgModuleDirectives(ngModules).then(function () {
35 var errors = [];
36 files.forEach(function (file) {
37 var compMetas = [];
38 file.directives.forEach(function (directiveType) {
39 var dirMeta = _this.metadataResolver.getDirectiveMetadata(directiveType);
40 if (dirMeta && dirMeta.isComponent) {
41 compMetas.push(dirMeta);
42 }
43 });
44 compMetas.forEach(function (compMeta) {
45 var html = compMeta.template.template;
46 var interpolationConfig = compiler.InterpolationConfig.fromArray(compMeta.template.interpolation);
47 errors.push.apply(errors, _this.messageBundle.updateFromTemplate(html, file.srcUrl, interpolationConfig));
48 });
49 });
50 if (errors.length) {
51 throw new Error(errors.map(function (e) { return e.toString(); }).join('\n'));
52 }
53 return _this.messageBundle;
54 });
55 };
56 Extractor.create = function (options, translationsFormat, program, compilerHost, resourceLoader, reflectorHost) {
57 var htmlParser = new compiler.I18NHtmlParser(new compiler.HtmlParser());
58 var urlResolver = compiler.createOfflineCompileUrlResolver();
59 if (!reflectorHost)
60 reflectorHost = new reflector_host_1.ReflectorHost(program, compilerHost, options);
61 var staticReflector = new static_reflector_1.StaticReflector(reflectorHost);
62 static_reflection_capabilities_1.StaticAndDynamicReflectionCapabilities.install(staticReflector);
63 var config = new compiler.CompilerConfig({
64 genDebugInfo: options.debug === true,
65 defaultEncapsulation: core_1.ViewEncapsulation.Emulated,
66 logBindingUpdate: false,
67 useJit: false
68 });
69 var normalizer = new compiler.DirectiveNormalizer(resourceLoader, urlResolver, htmlParser, config);
70 var elementSchemaRegistry = new compiler.DomElementSchemaRegistry();
71 var resolver = new compiler.CompileMetadataResolver(new compiler.NgModuleResolver(staticReflector), new compiler.DirectiveResolver(staticReflector), new compiler.PipeResolver(staticReflector), elementSchemaRegistry, normalizer, staticReflector);
72 // TODO(vicb): implicit tags & attributes
73 var messageBundle = new compiler.MessageBundle(htmlParser, [], {});
74 return new Extractor(options, program, compilerHost, staticReflector, messageBundle, reflectorHost, resolver);
75 };
76 return Extractor;
77}());
78exports.Extractor = Extractor;
79//# sourceMappingURL=extractor.js.map
\No newline at end of file