-
Notifications
You must be signed in to change notification settings - Fork 4
/
preprocessor.js
42 lines (37 loc) · 989 Bytes
/
preprocessor.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
// typescript compiler
const tsc = require("typescript")
// typescript configuration
const compilerOptions = {
target: "es2017",
module: "commonjs",
jsx: "react",
allowSyntheticDefaultImports: true,
esModuleInterop: true,
sourceMap: true
}
/**
* Converts TypeScript files to JavaScript.
*
* @param {string} The source code of the file.
* @param {string} The path to the file we're transformation
* @returns The transformed source code
*/
function process(src, path) {
// we only convert .ts and .tsx files
if (path.endsWith(".ts") || path.endsWith(".tsx")) {
// compile TS to JS
const transpiled = tsc.transpileModule(src, {
compilerOptions: compilerOptions,
fileName: path
})
// give Jest the js and source map
return {
code: transpiled.outputText,
map: transpiled.sourceMapText
}
}
// don't modify it, just hand it back
return src
}
// make the pre-processor available to jest
module.exports = { process }