-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
66 lines (53 loc) · 1.72 KB
/
index.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
const ts = require('typescript');
const { version, name } = require('./package.json');
const { join, dirname } = require('path');
const { access, constants, readFileSync } = require('fs');
const debug = require('debug')(name);
debug('versions', {
[name]: version,
typescript: ts.version,
});
const cache = {};
function tsify(content, fileName) {
const searchPath = ts.normalizePath(ts.sys.getCurrentDirectory());
const configFileName = process.env.TSCONFIG || ts.findConfigFile(searchPath, ts.sys.fileExists);
const compilerOptions = getCompilerOptionsViaCache(configFileName);
compilerOptions.sourceMap = false;
compilerOptions.inlineSourceMap = true;
debug('transpileModule', { fileName });
const { outputText, diagnostics } = ts.transpileModule(content, {
fileName,
compilerOptions,
reportDiagnostics: true,
});
debug({ diagnostics });
return outputText;
}
function getCompilerOptionsViaCache(configFileName) {
let options;
if (!(options = cache[configFileName])) {
options = cache[configFileName] = getCompilerOptions(configFileName);
}
return options;
}
function getCompilerOptions(configFileName) {
const { config, error } = ts.readConfigFile(configFileName, ts.sys.readFile);
if (error) {
throw new Error(`TS config error in ${configFileName}: ${error.messageText}`);
}
const { options } = ts.parseJsonConfigFileContent(
config,
ts.sys,
ts.getDirectoryPath(configFileName),
{},
configFileName);
debug('getCompilerOptions', { configFileName, options });
return options;
}
module.exports = [{
ext: '.ts',
transform: tsify,
}, {
ext: '.tsx',
transform: tsify,
}];