/
babel-register.js
64 lines (48 loc) · 1.54 KB
/
babel-register.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
var transformFileSync = require("babel-core").transformFileSync;
var fs = require("fs");
var Module = require("module");
module.exports = function (options)
{
var normalized = options.map(function (options)
{
if (options.sources || options.source)
return [toMatch(options.sources || options.source), options.options];
if (options.match === false)
return [never, options.options];
if (!options.match)
throw new Error("bootstrap/babel-register must either include match or source");
return [options.match, options.options];
});
const original = Module._extensions[".js"];
Module._extensions[".js"] = function (module, filename)
{
var found = normalized.find(function (pair)
{
return pair[0](filename);
});
if (!found)
return original(module, filename);
var options = Object.assign({ ast: false }, found[1]);
var transformed = transformFileSync(filename, options).code;
return module._compile(transformed, filename);
}
}
function never()
{
return false;
}
function toMatch(source)
{
var escaped = Array.isArray(source) ?
source.map(escapeRegExp).join("|") :
escapeRegExp(source);
var regexp = new RegExp("^(" + escaped + ")/(?!(.*/)?node_modules/)");
return function (filename)
{
return regexp.test(filename);
};
}
function escapeRegExp(string)
{
return "(" + string.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&") + ")";
}