UNPKG

8.89 kBJavaScriptView Raw
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 */
9Object.defineProperty(exports, "__esModule", { value: true });
10var fs = require("fs");
11var path = require("path");
12var tsickle = require("tsickle");
13var ts = require("typescript");
14var bundler_1 = require("./bundler");
15var cli_options_1 = require("./cli_options");
16var compiler_host_1 = require("./compiler_host");
17var index_writer_1 = require("./index_writer");
18var tsc_1 = require("./tsc");
19var vinyl_file_1 = require("./vinyl_file");
20var tsc_2 = require("./tsc");
21exports.UserError = tsc_2.UserError;
22var DTS = /\.d\.ts$/;
23var JS_EXT = /(\.js|)$/;
24var TS_EXT = /\.ts$/;
25function createBundleIndexHost(ngOptions, rootFiles, host) {
26 var files = rootFiles.filter(function (f) { return !DTS.test(f); });
27 if (files.length != 1) {
28 return {
29 host: host,
30 errors: [{
31 file: null,
32 start: null,
33 length: null,
34 messageText: 'Angular compiler option "flatModuleIndex" requires one and only one .ts file in the "files" field.',
35 category: ts.DiagnosticCategory.Error,
36 code: 0
37 }]
38 };
39 }
40 var file = files[0];
41 var indexModule = file.replace(/\.ts$/, '');
42 var bundler = new bundler_1.MetadataBundler(indexModule, ngOptions.flatModuleId, new bundler_1.CompilerHostAdapter(host));
43 var metadataBundle = bundler.getMetadataBundle();
44 var metadata = JSON.stringify(metadataBundle.metadata);
45 var name = path.join(path.dirname(indexModule), ngOptions.flatModuleOutFile.replace(JS_EXT, '.ts'));
46 var libraryIndex = "./" + path.basename(indexModule);
47 var content = index_writer_1.privateEntriesToIndex(libraryIndex, metadataBundle.privates);
48 host = new compiler_host_1.SyntheticIndexHost(host, { name: name, content: content, metadata: metadata });
49 return { host: host, indexName: name };
50}
51exports.createBundleIndexHost = createBundleIndexHost;
52function main(project, cliOptions, codegen, options) {
53 try {
54 var projectDir = project;
55 // project is vinyl like file object
56 if (vinyl_file_1.isVinylFile(project)) {
57 projectDir = path.dirname(project.path);
58 }
59 else if (fs.lstatSync(project).isFile()) {
60 projectDir = path.dirname(project);
61 }
62 // file names in tsconfig are resolved relative to this absolute path
63 var basePath_1 = path.resolve(process.cwd(), cliOptions.basePath || projectDir);
64 // read the configuration options from wherever you store them
65 var _a = tsc_1.tsc.readConfiguration(project, basePath_1, options), parsed_1 = _a.parsed, ngOptions_1 = _a.ngOptions;
66 ngOptions_1.basePath = basePath_1;
67 var rootFileNames_1 = parsed_1.fileNames.slice(0);
68 var createProgram_1 = function (host, oldProgram) {
69 return ts.createProgram(rootFileNames_1.slice(0), parsed_1.options, host, oldProgram);
70 };
71 var addGeneratedFileName_1 = function (genFileName) {
72 if (genFileName.startsWith(basePath_1) && TS_EXT.exec(genFileName)) {
73 rootFileNames_1.push(genFileName);
74 }
75 };
76 var diagnostics_1 = parsed_1.options.diagnostics;
77 if (diagnostics_1)
78 ts.performance.enable();
79 var host_1 = ts.createCompilerHost(parsed_1.options, true);
80 // If the compilation is a flat module index then produce the flat module index
81 // metadata and the synthetic flat module index.
82 if (ngOptions_1.flatModuleOutFile && !ngOptions_1.skipMetadataEmit) {
83 var _b = createBundleIndexHost(ngOptions_1, rootFileNames_1, host_1), bundleHost = _b.host, indexName = _b.indexName, errors_1 = _b.errors;
84 if (errors_1)
85 tsc_1.check(errors_1);
86 if (indexName)
87 addGeneratedFileName_1(indexName);
88 host_1 = bundleHost;
89 }
90 var tsickleCompilerHostOptions = { googmodule: false, untyped: true, convertIndexImportShorthand: false };
91 var tsickleHost = {
92 shouldSkipTsickleProcessing: function (fileName) { return /\.d\.ts$/.test(fileName); },
93 pathToModuleName: function (context, importPath) { return ''; },
94 shouldIgnoreWarningsForPath: function (filePath) { return false; },
95 fileNameToModuleId: function (fileName) { return fileName; },
96 };
97 var tsickleCompilerHost_1 = new tsickle.TsickleCompilerHost(host_1, ngOptions_1, tsickleCompilerHostOptions, tsickleHost);
98 var program_1 = createProgram_1(tsickleCompilerHost_1);
99 var errors = program_1.getOptionsDiagnostics();
100 tsc_1.check(errors);
101 if (ngOptions_1.skipTemplateCodegen || !codegen) {
102 codegen = function () { return Promise.resolve([]); };
103 }
104 if (diagnostics_1)
105 console.time('NG codegen');
106 return codegen(ngOptions_1, cliOptions, program_1, host_1).then(function (genFiles) {
107 if (diagnostics_1)
108 console.timeEnd('NG codegen');
109 // Add the generated files to the configuration so they will become part of the program.
110 if (ngOptions_1.alwaysCompileGeneratedCode) {
111 genFiles.forEach(function (genFileName) { return addGeneratedFileName_1(genFileName); });
112 }
113 var definitionsHost = tsickleCompilerHost_1;
114 if (!ngOptions_1.skipMetadataEmit) {
115 // if tsickle is not not used for emitting, but we do use the MetadataWriterHost,
116 // it also needs to emit the js files.
117 var emitJsFiles = ngOptions_1.annotationsAs === 'decorators' && !ngOptions_1.annotateForClosureCompiler;
118 definitionsHost = new compiler_host_1.MetadataWriterHost(tsickleCompilerHost_1, ngOptions_1, emitJsFiles);
119 }
120 // Create a new program since codegen files were created after making the old program
121 var programWithCodegen = createProgram_1(definitionsHost, program_1);
122 tsc_1.tsc.typeCheck(host_1, programWithCodegen);
123 var programForJsEmit = programWithCodegen;
124 if (ngOptions_1.annotationsAs !== 'decorators') {
125 if (diagnostics_1)
126 console.time('NG downlevel');
127 tsickleCompilerHost_1.reconfigureForRun(programForJsEmit, tsickle.Pass.DECORATOR_DOWNLEVEL);
128 // A program can be re-used only once; save the programWithCodegen to be reused by
129 // metadataWriter
130 programForJsEmit = createProgram_1(tsickleCompilerHost_1);
131 tsc_1.check(tsickleCompilerHost_1.diagnostics);
132 if (diagnostics_1)
133 console.timeEnd('NG downlevel');
134 }
135 if (ngOptions_1.annotateForClosureCompiler) {
136 if (diagnostics_1)
137 console.time('NG JSDoc');
138 tsickleCompilerHost_1.reconfigureForRun(programForJsEmit, tsickle.Pass.CLOSURIZE);
139 programForJsEmit = createProgram_1(tsickleCompilerHost_1);
140 tsc_1.check(tsickleCompilerHost_1.diagnostics);
141 if (diagnostics_1)
142 console.timeEnd('NG JSDoc');
143 }
144 // Emit *.js and *.js.map
145 tsc_1.tsc.emit(programForJsEmit);
146 // Emit *.d.ts and maybe *.metadata.json
147 // Not in the same emit pass with above, because tsickle erases
148 // decorators which we want to read or document.
149 // Do this emit second since TypeScript will create missing directories for us
150 // in the standard emit.
151 tsc_1.tsc.emit(programWithCodegen);
152 if (diagnostics_1) {
153 ts.performance.forEachMeasure(function (name, duration) { console.error("TS " + name + ": " + duration + "ms"); });
154 }
155 });
156 }
157 catch (e) {
158 return Promise.reject(e);
159 }
160}
161exports.main = main;
162// CLI entry point
163if (require.main === module) {
164 var args_1 = process.argv.slice(2);
165 var _a = ts.parseCommandLine(args_1), options = _a.options, errors = _a.errors;
166 tsc_1.check(errors);
167 var project = options.project || '.';
168 // TODO(alexeagle): command line should be TSC-compatible, remove "CliOptions" here
169 var cliOptions = new cli_options_1.CliOptions(require('minimist')(args_1));
170 main(project, cliOptions, null, options)
171 .then(function (exitCode) { return process.exit(exitCode); })
172 .catch(function (e) {
173 console.error(e.stack);
174 console.error('Compilation failed');
175 process.exit(1);
176 });
177}
178//# sourceMappingURL=main.js.map
\No newline at end of file