2017-08-07 17:30:35 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {isSyntaxError, syntaxError} from '@angular/compiler';
|
2017-08-02 14:20:07 -04:00
|
|
|
import {createBundleIndexHost} from '@angular/tsc-wrapped';
|
2017-08-07 17:30:35 -04:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
|
|
|
import * as ts from 'typescript';
|
2017-08-02 14:20:07 -04:00
|
|
|
|
2017-08-07 17:30:35 -04:00
|
|
|
import * as api from './transformers/api';
|
|
|
|
import * as ng from './transformers/entry_points';
|
|
|
|
|
|
|
|
const TS_EXT = /\.ts$/;
|
|
|
|
|
2017-08-09 16:45:45 -04:00
|
|
|
export type Diagnostics = Array<ts.Diagnostic|api.Diagnostic>;
|
2017-08-07 17:30:35 -04:00
|
|
|
|
2017-08-09 16:45:45 -04:00
|
|
|
function isTsDiagnostic(diagnostic: any): diagnostic is ts.Diagnostic {
|
2017-08-18 17:03:59 -04:00
|
|
|
return diagnostic && diagnostic.source != 'angular';
|
2017-08-07 17:30:35 -04:00
|
|
|
}
|
|
|
|
|
2017-08-09 16:45:45 -04:00
|
|
|
export function formatDiagnostics(options: api.CompilerOptions, diags: Diagnostics): string {
|
2017-08-07 17:30:35 -04:00
|
|
|
if (diags && diags.length) {
|
2017-08-09 16:45:45 -04:00
|
|
|
const tsFormatHost: ts.FormatDiagnosticsHost = {
|
|
|
|
getCurrentDirectory: () => options.basePath || process.cwd(),
|
|
|
|
getCanonicalFileName: fileName => fileName,
|
|
|
|
getNewLine: () => ts.sys.newLine
|
|
|
|
};
|
|
|
|
return diags
|
|
|
|
.map(d => {
|
|
|
|
if (isTsDiagnostic(d)) {
|
|
|
|
return ts.formatDiagnostics([d], tsFormatHost);
|
|
|
|
} else {
|
|
|
|
let res = ts.DiagnosticCategory[d.category];
|
2017-08-07 17:30:35 -04:00
|
|
|
if (d.span) {
|
|
|
|
res +=
|
|
|
|
` at ${d.span.start.file.url}(${d.span.start.line + 1},${d.span.start.col + 1})`;
|
|
|
|
}
|
|
|
|
if (d.span && d.span.details) {
|
2017-08-18 17:03:59 -04:00
|
|
|
res += `: ${d.span.details}, ${d.messageText}\n`;
|
2017-08-07 17:30:35 -04:00
|
|
|
} else {
|
2017-08-18 17:03:59 -04:00
|
|
|
res += `: ${d.messageText}\n`;
|
2017-08-07 17:30:35 -04:00
|
|
|
}
|
|
|
|
return res;
|
2017-08-09 16:45:45 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.join();
|
2017-08-07 17:30:35 -04:00
|
|
|
} else
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2017-08-09 16:45:45 -04:00
|
|
|
export interface ParsedConfiguration {
|
2017-08-18 17:03:59 -04:00
|
|
|
project: string;
|
2017-08-09 16:45:45 -04:00
|
|
|
options: api.CompilerOptions;
|
|
|
|
rootNames: string[];
|
|
|
|
errors: Diagnostics;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function calcProjectFileAndBasePath(project: string):
|
|
|
|
{projectFile: string, basePath: string} {
|
|
|
|
const projectIsDir = fs.lstatSync(project).isDirectory();
|
|
|
|
const projectFile = projectIsDir ? path.join(project, 'tsconfig.json') : project;
|
|
|
|
const projectDir = projectIsDir ? project : path.dirname(project);
|
|
|
|
const basePath = path.resolve(process.cwd(), projectDir);
|
|
|
|
return {projectFile, basePath};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createNgCompilerOptions(
|
|
|
|
basePath: string, config: any, tsOptions: ts.CompilerOptions): api.CompilerOptions {
|
|
|
|
return {...tsOptions, ...config.angularCompilerOptions, genDir: basePath, basePath};
|
2017-08-07 17:30:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function readConfiguration(
|
2017-08-09 16:45:45 -04:00
|
|
|
project: string, existingOptions?: ts.CompilerOptions): ParsedConfiguration {
|
|
|
|
try {
|
|
|
|
const {projectFile, basePath} = calcProjectFileAndBasePath(project);
|
|
|
|
|
|
|
|
let {config, error} = ts.readConfigFile(projectFile, ts.sys.readFile);
|
|
|
|
|
|
|
|
if (error) {
|
2017-08-18 17:03:59 -04:00
|
|
|
return {project, errors: [error], rootNames: [], options: {}};
|
2017-08-09 16:45:45 -04:00
|
|
|
}
|
|
|
|
const parseConfigHost = {
|
|
|
|
useCaseSensitiveFileNames: true,
|
|
|
|
fileExists: fs.existsSync,
|
|
|
|
readDirectory: ts.sys.readDirectory,
|
|
|
|
readFile: ts.sys.readFile
|
|
|
|
};
|
|
|
|
const parsed =
|
|
|
|
ts.parseJsonConfigFileContent(config, parseConfigHost, basePath, existingOptions);
|
|
|
|
const rootNames = parsed.fileNames.map(f => path.normalize(f));
|
|
|
|
|
|
|
|
const options = createNgCompilerOptions(basePath, config, parsed.options);
|
2017-08-18 17:03:59 -04:00
|
|
|
return {project: projectFile, rootNames, options, errors: parsed.errors};
|
2017-08-09 16:45:45 -04:00
|
|
|
} catch (e) {
|
|
|
|
const errors: Diagnostics = [{
|
|
|
|
category: ts.DiagnosticCategory.Error,
|
2017-08-18 17:03:59 -04:00
|
|
|
messageText: e.stack,
|
|
|
|
source: api.SOURCE,
|
|
|
|
code: api.UNKNOWN_ERROR_CODE
|
2017-08-09 16:45:45 -04:00
|
|
|
}];
|
2017-08-18 17:03:59 -04:00
|
|
|
return {project: '', errors, rootNames: [], options: {}};
|
2017-08-09 16:45:45 -04:00
|
|
|
}
|
2017-08-07 17:30:35 -04:00
|
|
|
}
|
|
|
|
|
2017-08-18 17:03:59 -04:00
|
|
|
export interface PerformCompilationResult {
|
|
|
|
diagnostics: Diagnostics;
|
|
|
|
program?: api.Program;
|
|
|
|
emitResult?: ts.EmitResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function exitCodeFromResult(result: PerformCompilationResult | undefined): number {
|
|
|
|
if (!result) {
|
|
|
|
// If we didn't get a result we should return failure.
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!result.diagnostics || result.diagnostics.length === 0) {
|
|
|
|
// If we have a result and didn't get any errors, we succeeded.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return 2 if any of the errors were unknown.
|
|
|
|
return result.diagnostics.some(d => d.source === 'angular' && d.code === api.UNKNOWN_ERROR_CODE) ?
|
|
|
|
2 :
|
|
|
|
1;
|
|
|
|
}
|
|
|
|
|
2017-08-07 17:30:35 -04:00
|
|
|
export function performCompilation(
|
2017-08-16 18:35:19 -04:00
|
|
|
{rootNames, options, host, oldProgram, emitCallback, customTransformers}: {
|
|
|
|
rootNames: string[],
|
|
|
|
options: api.CompilerOptions,
|
|
|
|
host?: api.CompilerHost,
|
|
|
|
oldProgram?: api.Program,
|
|
|
|
emitCallback?: api.TsEmitCallback,
|
|
|
|
customTransformers?: api.CustomTransformers
|
2017-08-18 17:03:59 -04:00
|
|
|
}): PerformCompilationResult {
|
2017-08-02 14:20:07 -04:00
|
|
|
const [major, minor] = ts.version.split('.');
|
|
|
|
|
2017-08-09 16:45:45 -04:00
|
|
|
if (Number(major) < 2 || (Number(major) === 2 && Number(minor) < 3)) {
|
2017-08-02 14:20:07 -04:00
|
|
|
throw new Error('Must use TypeScript > 2.3 to have transformer support');
|
|
|
|
}
|
|
|
|
|
2017-08-09 16:45:45 -04:00
|
|
|
const allDiagnostics: Diagnostics = [];
|
2017-08-07 17:30:35 -04:00
|
|
|
|
2017-08-09 16:45:45 -04:00
|
|
|
function checkDiagnostics(diags: Diagnostics | undefined) {
|
|
|
|
if (diags) {
|
|
|
|
allDiagnostics.push(...diags);
|
|
|
|
return diags.every(d => d.category !== ts.DiagnosticCategory.Error);
|
2017-08-07 17:30:35 -04:00
|
|
|
}
|
2017-08-09 16:45:45 -04:00
|
|
|
return true;
|
|
|
|
}
|
2017-08-07 17:30:35 -04:00
|
|
|
|
2017-08-09 16:45:45 -04:00
|
|
|
let program: api.Program|undefined;
|
2017-08-16 18:35:19 -04:00
|
|
|
let emitResult: ts.EmitResult|undefined;
|
2017-08-09 16:45:45 -04:00
|
|
|
try {
|
|
|
|
if (!host) {
|
2017-08-14 14:04:55 -04:00
|
|
|
host = ng.createCompilerHost({options});
|
2017-08-09 16:45:45 -04:00
|
|
|
}
|
2017-08-07 17:30:35 -04:00
|
|
|
|
2017-08-09 16:45:45 -04:00
|
|
|
program = ng.createProgram({rootNames, host, options, oldProgram});
|
2017-08-07 17:30:35 -04:00
|
|
|
|
2017-08-09 16:45:45 -04:00
|
|
|
let shouldEmit = true;
|
2017-08-07 17:30:35 -04:00
|
|
|
// Check parameter diagnostics
|
2017-08-09 16:45:45 -04:00
|
|
|
shouldEmit = shouldEmit && checkDiagnostics([
|
|
|
|
...program !.getTsOptionDiagnostics(), ...program !.getNgOptionDiagnostics()
|
|
|
|
]);
|
2017-08-07 17:30:35 -04:00
|
|
|
|
|
|
|
// Check syntactic diagnostics
|
2017-08-09 16:45:45 -04:00
|
|
|
shouldEmit = shouldEmit && checkDiagnostics(program !.getTsSyntacticDiagnostics());
|
2017-08-07 17:30:35 -04:00
|
|
|
|
|
|
|
// Check TypeScript semantic and Angular structure diagnostics
|
2017-08-09 16:45:45 -04:00
|
|
|
shouldEmit =
|
|
|
|
shouldEmit &&
|
|
|
|
checkDiagnostics(
|
|
|
|
[...program !.getTsSemanticDiagnostics(), ...program !.getNgStructuralDiagnostics()]);
|
2017-08-07 17:30:35 -04:00
|
|
|
|
|
|
|
// Check Angular semantic diagnostics
|
2017-08-09 16:45:45 -04:00
|
|
|
shouldEmit = shouldEmit && checkDiagnostics(program !.getNgSemanticDiagnostics());
|
2017-08-02 14:20:07 -04:00
|
|
|
|
2017-08-09 16:45:45 -04:00
|
|
|
if (shouldEmit) {
|
2017-08-16 18:35:19 -04:00
|
|
|
emitResult = program !.emit({
|
|
|
|
emitCallback,
|
|
|
|
customTransformers,
|
2017-08-09 16:45:45 -04:00
|
|
|
emitFlags: api.EmitFlags.Default |
|
|
|
|
((options.skipMetadataEmit || options.flatModuleOutFile) ? 0 : api.EmitFlags.Metadata)
|
|
|
|
});
|
|
|
|
allDiagnostics.push(...emitResult.diagnostics);
|
2017-08-18 17:03:59 -04:00
|
|
|
return {diagnostics: allDiagnostics, program, emitResult};
|
2017-08-09 16:45:45 -04:00
|
|
|
}
|
2017-08-18 17:03:59 -04:00
|
|
|
return {diagnostics: allDiagnostics, program};
|
2017-08-07 17:30:35 -04:00
|
|
|
} catch (e) {
|
2017-08-09 16:45:45 -04:00
|
|
|
let errMsg: string;
|
2017-08-18 17:03:59 -04:00
|
|
|
let code: number;
|
2017-08-07 17:30:35 -04:00
|
|
|
if (isSyntaxError(e)) {
|
2017-08-09 16:45:45 -04:00
|
|
|
// don't report the stack for syntax errors as they are well known errors.
|
|
|
|
errMsg = e.message;
|
2017-08-18 17:03:59 -04:00
|
|
|
code = api.DEFAULT_ERROR_CODE;
|
2017-08-09 16:45:45 -04:00
|
|
|
} else {
|
|
|
|
errMsg = e.stack;
|
2017-08-18 17:03:59 -04:00
|
|
|
// It is not a syntax error we might have a program with unknown state, discard it.
|
|
|
|
program = undefined;
|
|
|
|
code = api.UNKNOWN_ERROR_CODE;
|
2017-08-07 17:30:35 -04:00
|
|
|
}
|
2017-08-18 17:03:59 -04:00
|
|
|
allDiagnostics.push(
|
|
|
|
{category: ts.DiagnosticCategory.Error, messageText: errMsg, code, source: api.SOURCE});
|
|
|
|
return {diagnostics: allDiagnostics, program};
|
2017-08-07 17:30:35 -04:00
|
|
|
}
|
2017-08-02 14:20:07 -04:00
|
|
|
}
|