tsbuild/ts/tsbuild.classes.compiler.ts

110 lines
3.3 KiB
TypeScript
Raw Permalink Normal View History

2018-07-25 18:56:42 +00:00
// import all the stuff we need
2024-05-10 13:55:25 +00:00
import * as plugins from './plugins.js';
2023-06-03 14:45:40 +00:00
import type { CompilerOptions, ScriptTarget, ModuleKind } from './tsbuild.exports.js';
2022-03-12 18:05:46 +00:00
2018-07-25 18:56:42 +00:00
/**
* the default typescript compilerOptions
*/
export const compilerOptionsDefault: CompilerOptions = {
declaration: true,
emitDecoratorMetadata: true,
experimentalDecorators: true,
inlineSourceMap: true,
noEmitOnError: true,
2020-03-13 19:23:01 +00:00
outDir: 'dist_ts/',
2023-08-26 11:27:51 +00:00
module: plugins.typescript.ModuleKind.NodeNext,
target: plugins.typescript.ScriptTarget.ESNext,
2022-03-18 21:12:27 +00:00
moduleResolution: plugins.typescript.ModuleResolutionKind.NodeNext,
2024-05-17 17:19:55 +00:00
lib: ['lib.dom.d.ts', 'lib.es2022.d.ts'],
2022-03-14 15:04:07 +00:00
noImplicitAny: true,
2019-04-30 11:27:42 +00:00
esModuleInterop: true,
2022-03-24 17:24:05 +00:00
useDefineForClassFields: false,
2023-06-03 14:45:40 +00:00
verbatimModuleSyntax: true,
2018-07-25 18:56:42 +00:00
};
/**
* merges compilerOptions with the default compiler options
*/
2019-08-26 14:28:03 +00:00
export const mergeCompilerOptions = (
customTsOptions: CompilerOptions,
argvArg?: any
): CompilerOptions => {
2018-07-25 18:56:42 +00:00
// create merged options
2020-03-13 19:23:01 +00:00
const mergedOptions: CompilerOptions = {
2022-03-15 19:06:48 +00:00
...compilerOptionsDefault,
2022-01-19 16:59:07 +00:00
...customTsOptions,
2022-03-18 13:32:35 +00:00
...(argvArg && argvArg.skiplibcheck
? {
skipLibCheck: true,
}
: {}),
...(argvArg && argvArg.allowimplicitany
? {
noImplicitAny: false,
}
: {}),
...(argvArg && argvArg.commonjs
? {
module: plugins.typescript.ModuleKind.CommonJS,
moduleResolution: plugins.typescript.ModuleResolutionKind.NodeJs,
}
: {}),
2018-07-25 18:56:42 +00:00
};
2022-03-18 13:32:35 +00:00
console.log(mergedOptions);
2022-03-15 09:18:08 +00:00
2018-07-25 18:56:42 +00:00
return mergedOptions;
};
/**
* the internal main compiler function that compiles the files
*/
2022-03-18 13:31:48 +00:00
export const compiler = async (
2018-07-25 18:56:42 +00:00
fileNames: string[],
options: plugins.typescript.CompilerOptions,
2019-08-26 14:28:03 +00:00
argvArg?: any
2018-07-25 18:56:42 +00:00
): Promise<any[]> => {
2022-03-18 13:31:48 +00:00
if (options.skipLibCheck) {
2022-03-18 13:32:35 +00:00
console.log('? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?');
2022-03-18 13:31:48 +00:00
console.log('You are skipping libcheck... Is that really wanted?');
2023-06-03 14:45:40 +00:00
console.log('continuing in 5 seconds...');
2022-03-18 13:32:35 +00:00
console.log('? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?');
2022-03-18 13:31:48 +00:00
await plugins.smartdelay.delayFor(5000);
}
console.log(`Compiling ${fileNames.length} files...`);
2020-03-13 19:23:01 +00:00
const done = plugins.smartpromise.defer<any[]>();
const program = plugins.typescript.createProgram(fileNames, options);
const emitResult = program.emit();
2018-07-25 18:56:42 +00:00
// implement check only
/*let emitResult = program.emit(undefined,(args) => {
console.log(args)
});*/
2020-03-13 19:23:01 +00:00
const allDiagnostics = plugins.typescript
2018-07-25 18:56:42 +00:00
.getPreEmitDiagnostics(program)
.concat(emitResult.diagnostics);
2022-03-18 13:32:35 +00:00
allDiagnostics.forEach((diagnostic) => {
if (diagnostic.file) {
2020-03-13 19:23:01 +00:00
const { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start!);
const message = plugins.typescript.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
2018-07-25 18:56:42 +00:00
console.log(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`);
} else {
console.log(
`${plugins.typescript.flattenDiagnosticMessageText(diagnostic.messageText, '\n')}`
);
}
});
2018-07-25 18:56:42 +00:00
2020-03-13 19:23:01 +00:00
const exitCode = emitResult.emitSkipped ? 1 : 0;
2018-07-25 18:56:42 +00:00
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;
};