2018-07-25 18:56:42 +00:00
|
|
|
// import all the stuff we need
|
|
|
|
import * as plugins from './tsbuild.plugins';
|
|
|
|
import { CompilerOptions } from 'typescript';
|
|
|
|
export { CompilerOptions, ScriptTarget, ModuleKind } from 'typescript';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the default typescript compilerOptions
|
|
|
|
*/
|
|
|
|
export const compilerOptionsDefault: CompilerOptions = {
|
|
|
|
declaration: true,
|
|
|
|
emitDecoratorMetadata: true,
|
|
|
|
experimentalDecorators: true,
|
|
|
|
inlineSourceMap: true,
|
2018-07-25 21:38:30 +00:00
|
|
|
noEmitOnError: true,
|
2018-07-25 18:56:42 +00:00
|
|
|
outDir: 'dist/',
|
|
|
|
module: plugins.typescript.ModuleKind.CommonJS,
|
2018-07-25 21:38:30 +00:00
|
|
|
lib: ['lib.es2016.d.ts', 'lib.es2017.d.ts'],
|
2018-07-25 18:56:42 +00:00
|
|
|
noImplicitAny: false,
|
|
|
|
target: plugins.typescript.ScriptTarget.ES2015
|
|
|
|
};
|
|
|
|
|
2018-12-05 22:29:01 +00:00
|
|
|
export const compilerOptionsWebDefault: CompilerOptions = {
|
|
|
|
...compilerOptionsDefault,
|
|
|
|
lib: [...compilerOptionsDefault.lib, 'dom']
|
|
|
|
};
|
|
|
|
|
2018-07-25 18:56:42 +00:00
|
|
|
/**
|
|
|
|
* merges compilerOptions with the default compiler options
|
|
|
|
*/
|
2018-12-05 22:29:01 +00:00
|
|
|
export const mergeCompilerOptions = (customTsOptions: CompilerOptions, argvArg?: any): CompilerOptions => {
|
|
|
|
const defaultOptionsToMerge = (() => {
|
|
|
|
if (argvArg && argvArg.web) {
|
|
|
|
return compilerOptionsWebDefault;
|
|
|
|
} else {
|
|
|
|
return compilerOptionsDefault;
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
2018-07-25 18:56:42 +00:00
|
|
|
// create merged options
|
|
|
|
let mergedOptions: CompilerOptions = {
|
2018-12-05 22:29:01 +00:00
|
|
|
...defaultOptionsToMerge,
|
2018-07-25 18:56:42 +00:00
|
|
|
...customTsOptions
|
|
|
|
};
|
|
|
|
|
|
|
|
return mergedOptions;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the internal main compiler function that compiles the files
|
|
|
|
*/
|
|
|
|
export const compiler = (
|
|
|
|
fileNames: string[],
|
2018-12-05 22:29:01 +00:00
|
|
|
options: plugins.typescript.CompilerOptions,
|
|
|
|
argvArg?: any,
|
2018-07-25 18:56:42 +00:00
|
|
|
): Promise<any[]> => {
|
2018-07-25 21:38:30 +00:00
|
|
|
console.log(`Compiling ${fileNames.length} files...`);
|
2018-07-25 18:56:42 +00:00
|
|
|
let done = plugins.smartpromise.defer<any[]>();
|
|
|
|
let program = plugins.typescript.createProgram(fileNames, options);
|
|
|
|
let emitResult = program.emit();
|
|
|
|
|
|
|
|
// implement check only
|
|
|
|
/*let emitResult = program.emit(undefined,(args) => {
|
|
|
|
console.log(args)
|
|
|
|
});*/
|
|
|
|
|
|
|
|
let allDiagnostics = plugins.typescript
|
|
|
|
.getPreEmitDiagnostics(program)
|
|
|
|
.concat(emitResult.diagnostics);
|
2018-07-25 21:38:30 +00:00
|
|
|
allDiagnostics.forEach(diagnostic => {
|
|
|
|
if (diagnostic.file) {
|
|
|
|
let { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start!);
|
2018-07-25 18:56:42 +00:00
|
|
|
let message = plugins.typescript.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
|
|
|
|
console.log(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`);
|
2018-07-25 21:38:30 +00:00
|
|
|
} else {
|
|
|
|
console.log(
|
|
|
|
`${plugins.typescript.flattenDiagnosticMessageText(diagnostic.messageText, '\n')}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
2018-07-25 18:56:42 +00:00
|
|
|
|
|
|
|
let exitCode = emitResult.emitSkipped ? 1 : 0;
|
|
|
|
if (exitCode === 0) {
|
|
|
|
console.log('TypeScript emit succeeded!');
|
|
|
|
done.resolve(emitResult.emittedFiles);
|
|
|
|
} else {
|
|
|
|
console.error('TypeScript emit failed. Please investigate!');
|
|
|
|
process.exit(exitCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
return done.promise;
|
|
|
|
};
|