-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.ts
59 lines (52 loc) · 1.5 KB
/
compile.ts
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
import * as ts from "typescript";
import { sync as globSync } from "glob";
import {
transform,
} from "./src";
declare module "fs-extra" {
export function outputJsonSync(file: string, data: any, opts?: {}): void;
}
const CJS_CONFIG: ts.CompilerOptions = {
experimentalDecorators: true,
jsx: ts.JsxEmit.React,
module: ts.ModuleKind.ESNext,
moduleResolution: ts.ModuleResolutionKind.NodeJs,
noEmitOnError: false,
noUnusedLocals: true,
noUnusedParameters: true,
stripInternal: true,
resolveJsonModule: true,
declaration: true,
baseUrl: __dirname,
target: ts.ScriptTarget.ES2015
};
export default function compile(
input: string,
options: ts.CompilerOptions = CJS_CONFIG
) {
const files = globSync(input);
const compilerHost = ts.createCompilerHost(options);
const program = ts.createProgram(files, options, compilerHost);
const msgs = {};
let emitResult = program.emit(undefined, undefined, undefined, undefined, {
before: [
transform({})
],
afterDeclarations: [
transform({isDeclaration: true})
]
});
let allDiagnostics = ts
.getPreEmitDiagnostics(program)
.concat(emitResult.diagnostics);
allDiagnostics.forEach(diagnostic => {
let { line, character } = diagnostic.file.getLineAndCharacterOfPosition(
diagnostic.start
);
let message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n");
console.log(
`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`
);
});
return msgs;
}