-
Notifications
You must be signed in to change notification settings - Fork 0
/
typescript-compiler.js
68 lines (66 loc) · 2.02 KB
/
typescript-compiler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import ts from "typescript";
import { join } from "path";
export const outDir = "_temp";
/**
* 调用typescript的编译接口
* see: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API
* @param {string[]} fileNames
* @param { ts.CompilerOptions} options
* @returns void
*/
function compile(fileNames, options, callback) {
console.log("get compile options!");
const host = ts.createCompilerHost(options);
// host.writeFile = myWriteFile
let program = ts.createProgram(fileNames, options, host);
console.log("start compliing!");
let emitResult = program.emit();
let allDiagnostics = ts
.getPreEmitDiagnostics(program)
.concat(emitResult.diagnostics);
allDiagnostics.forEach((diagnostic) => {
if (diagnostic.file) {
let { line, character } = ts.getLineAndCharacterOfPosition(
diagnostic.file,
diagnostic.start
);
let message = ts.flattenDiagnosticMessageText(
diagnostic.messageText,
"\n"
);
console.log(
`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`
);
} else {
console.log(
ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n")
);
}
});
console.log("compile end!");
callback && callback();
}
/**
*
* @param {string} mainPath
* @param {string} configPath
* @returns void
*/
export function compileWidthConfig({
mainPath = join("src", "index.ts"),
configPath = "../../tsconfig.json",
callback,
}) {
if (!configPath) {
throw new Error("Could not find a valid 'tsconfig.json'.");
}
// import(configPath, { assert: { type: "json" } }).then((module) => { // 一些node版本要求断言
import(configPath).then((module) => {
const compilerOptions = module.default.compilerOptions;
compilerOptions.moduleResolution = ts.ModuleResolutionKind.NodeJs;
compilerOptions.module = ts.ModuleKind.ES2020;
compilerOptions.target = ts.ScriptTarget.ES2020;
compilerOptions.outDir = outDir;
compile([mainPath], compilerOptions, callback);
});
}