-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
46 lines (38 loc) · 1.42 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
/* eslint node/no-deprecated-api: [error, {ignoreGlobalItems: ["require.extensions"]}] */
"use strict";
const path = require("path");
const espowerSource = require("espower-source");
const minimatch = require("minimatch");
const sourceMapSupport = require("source-map-support");
const tsNodeRegister = require("ts-node").register;
const sourceCache = {};
function espowerTypeScript(options, tsNodeOptions) {
tsNodeRegister(tsNodeOptions);
// install source-map-support again to correct the source-map
sourceMapSupport.install({
environment: "node",
retrieveFile: (path) => sourceCache[path],
});
const { extensions = ["ts", "tsx"] } = options;
extensions.forEach((ext) => {
espowerTsRegister(`.${ext}`, options);
});
}
function espowerTsRegister(ext, options) {
const cwd = options.cwd || process.cwd();
const pattern = path.join(cwd, options.pattern);
const originalExtension = require.extensions[ext];
require.extensions[ext] = (module, filepath) => {
if (!minimatch(filepath, pattern)) {
return originalExtension(module, filepath);
}
const originalCompile = module._compile;
module._compile = function (code, filepath) {
const newSource = espowerSource(code, filepath, options);
sourceCache[filepath] = newSource;
return originalCompile.call(this, newSource, filepath);
};
return originalExtension(module, filepath);
};
}
module.exports = espowerTypeScript;