Project

General

Profile

1
"use strict";
2
/**
3
 * @license
4
 * Copyright Google Inc. All Rights Reserved.
5
 *
6
 * Use of this source code is governed by an MIT-style license that can be
7
 * found in the LICENSE file at https://angular.io/license
8
 */
9
Object.defineProperty(exports, "__esModule", { value: true });
10
/**
11
 * Transform template html and css into executable code.
12
 * Intended to be used in a build step.
13
 */
14
var compiler = require("@angular/compiler");
15
var core_1 = require("@angular/core");
16
var fs_1 = require("fs");
17
var compiler_host_1 = require("./compiler_host");
18
var path_mapped_compiler_host_1 = require("./path_mapped_compiler_host");
19
var GENERATED_META_FILES = /\.json$/;
20
var PREAMBLE = "/**\n * @fileoverview This file is generated by the Angular template compiler.\n * Do not edit.\n * @suppress {suspiciousCode,uselessCode,missingProperties,missingOverride}\n */\n /* tslint:disable */\n\n";
21
var CodeGenerator = (function () {
22
    function CodeGenerator(options, program, host, compiler, ngCompilerHost) {
23
        this.options = options;
24
        this.program = program;
25
        this.host = host;
26
        this.compiler = compiler;
27
        this.ngCompilerHost = ngCompilerHost;
28
    }
29
    CodeGenerator.prototype.codegen = function () {
30
        var _this = this;
31
        return this.compiler
32
            .analyzeModulesAsync(this.program.getSourceFiles().map(function (sf) { return _this.ngCompilerHost.getCanonicalFileName(sf.fileName); }))
33
            .then(function (analyzedModules) { return _this.emit(analyzedModules); });
34
    };
35
    CodeGenerator.prototype.codegenSync = function () {
36
        var _this = this;
37
        var analyzed = this.compiler.analyzeModulesSync(this.program.getSourceFiles().map(function (sf) { return _this.ngCompilerHost.getCanonicalFileName(sf.fileName); }));
38
        return this.emit(analyzed);
39
    };
40
    CodeGenerator.prototype.emit = function (analyzedModules) {
41
        var _this = this;
42
        var generatedModules = this.compiler.emitAllImpls(analyzedModules);
43
        return generatedModules.map(function (generatedModule) {
44
            var sourceFile = _this.program.getSourceFile(generatedModule.srcFileUrl);
45
            var emitPath = _this.ngCompilerHost.calculateEmitPath(generatedModule.genFileUrl);
46
            var source = generatedModule.source || compiler.toTypeScript(generatedModule, PREAMBLE);
47
            _this.host.writeFile(emitPath, source, false, function () { }, [sourceFile]);
48
            return emitPath;
49
        });
50
    };
51
    CodeGenerator.create = function (options, cliOptions, program, tsCompilerHost, compilerHostContext, ngCompilerHost) {
52
        if (!ngCompilerHost) {
53
            var usePathMapping = !!options.rootDirs && options.rootDirs.length > 0;
54
            var context = compilerHostContext || new compiler_host_1.ModuleResolutionHostAdapter(tsCompilerHost);
55
            ngCompilerHost = usePathMapping ? new path_mapped_compiler_host_1.PathMappedCompilerHost(program, options, context) :
56
                new compiler_host_1.CompilerHost(program, options, context);
57
        }
58
        var transContent = '';
59
        if (cliOptions.i18nFile) {
60
            if (!cliOptions.locale) {
61
                throw new Error("The translation file (" + cliOptions.i18nFile + ") locale must be provided. Use the --locale option.");
62
            }
63
            transContent = fs_1.readFileSync(cliOptions.i18nFile, 'utf8');
64
        }
65
        var missingTranslation = core_1.MissingTranslationStrategy.Warning;
66
        if (cliOptions.missingTranslation) {
67
            switch (cliOptions.missingTranslation) {
68
                case 'error':
69
                    missingTranslation = core_1.MissingTranslationStrategy.Error;
70
                    break;
71
                case 'warning':
72
                    missingTranslation = core_1.MissingTranslationStrategy.Warning;
73
                    break;
74
                case 'ignore':
75
                    missingTranslation = core_1.MissingTranslationStrategy.Ignore;
76
                    break;
77
                default:
78
                    throw new Error("Unknown option for missingTranslation (" + cliOptions.missingTranslation + "). Use either error, warning or ignore.");
79
            }
80
        }
81
        if (!transContent) {
82
            missingTranslation = core_1.MissingTranslationStrategy.Ignore;
83
        }
84
        var aotCompiler = compiler.createAotCompiler(ngCompilerHost, {
85
            translations: transContent,
86
            i18nFormat: cliOptions.i18nFormat,
87
            locale: cliOptions.locale, missingTranslation: missingTranslation,
88
            enableLegacyTemplate: options.enableLegacyTemplate !== false,
89
            enableSummariesForJit: options.enableSummariesForJit !== false,
90
            preserveWhitespaces: options.preserveWhitespaces,
91
        }).compiler;
92
        return new CodeGenerator(options, program, tsCompilerHost, aotCompiler, ngCompilerHost);
93
    };
94
    return CodeGenerator;
95
}());
96
exports.CodeGenerator = CodeGenerator;
97
//# sourceMappingURL=codegen.js.map
(2-2/30)