UNPKG

1.2 kBJavaScriptView Raw
1const ts = require('typescript');
2const glob = require('glob');
3
4function compile(fileNames, options) {
5 const program = ts.createProgram(fileNames, options);
6 program.getSourceFiles().forEach((sourceFile) => {
7 const emitResult = program.emit(sourceFile, (fileName, content) => {
8 if (options.module === ts.ModuleKind.CommonJS) {
9 fileName = fileName.replace(/\.js$/, '.common.js');
10 }
11 ts.sys.writeFile(fileName, content);
12 });
13 if (emitResult.emitSkipped) process.exit(1);
14 });
15
16 // const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
17 // allDiagnostics.forEach((diagnostic) => {
18 // const { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
19 // const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
20 // console.log(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`);
21 // });
22}
23
24const componentsGlob = glob.sync('./components/**/index.ts');
25
26// ES2015
27compile(componentsGlob, {
28 lib: ['es6', 'dom'],
29 module: ts.ModuleKind.ES2015
30});
31
32// CommonJS
33compile(componentsGlob, {
34 lib: ['es6', 'dom'],
35 module: ts.ModuleKind.CommonJS
36});