forked from leebyron/async-to-gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
register.js
76 lines (72 loc) · 2.84 KB
/
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
64
65
66
67
68
69
70
71
72
73
74
75
76
var asyncToGen = require('./index');
// Supported options:
//
// - sourceMaps: Include inline source maps. (default: true)
// - includes: A Regexp/String to determine which files should be transformed.
// (alias: include)
// - excludes: A Regexp/String to determine which files should not be
// transformed, defaults to ignoring /node_modules/, provide null
// to exclude nothing. (alias: exclude)
var options;
module.exports = function setOptions(newOptions) {
options = newOptions;
}
// Swizzle Module#_compile on each applicable module instance.
// NOTE: if using alongside Babel or another require-hook which simply
// over-writes the require.extensions and does not continue execution, then
// this require hook must come after it. Encourage those module authors to call
// the prior loader in their require hooks.
var jsLoader = require.extensions['.js'];
var exts = [ '.js', '.jsx', '.flow', '.es6' ];
exts.forEach(function (ext) {
var superLoader = require.extensions[ext] || jsLoader;
require.extensions[ext] = function (module, filename) {
if (shouldTransform(filename, options)) {
var super_compile = module._compile;
module._compile = function _compile(code, filename) {
var sourceMaps = options && 'sourceMaps' in options ? options.sourceMaps : true;
var result = asyncToGen(code, options);
var code = result.toString();
if (sourceMaps) {
var map = result.generateMap();
delete map.file;
delete map.sourcesContent;
map.sources[0] = filename;
code += '\n//# sourceMappingURL=' + map.toUrl();
}
super_compile.call(this, code, filename);
};
}
superLoader(module, filename);
};
});
function shouldTransform(filename, options) {
var includes = options && regexpPattern(options.includes || options.include);
var excludes =
options && 'excludes' in options ? regexpPattern(options.excludes) :
options && 'exclude' in options ? regexpPattern(options.exclude) :
/\/node_modules\//;
return (!includes || includes.test(filename)) && !(excludes && excludes.test(filename));
}
// Given a null | string | RegExp | any, returns null | Regexp or throws a
// more helpful error.
function regexpPattern(pattern) {
if (!pattern) {
return pattern;
}
// A very simplified glob transform which allows passing legible strings like
// "myPath/*.js" instead of a harder to read RegExp like /\/myPath\/.*\.js/.
if (typeof pattern === 'string') {
pattern = pattern.replace(/\./g, '\\.').replace(/\*/g, '.*');
if (pattern[0] !== '/') {
pattern = '/' + pattern;
}
return new RegExp(pattern);
}
if (typeof pattern.test === 'function') {
return pattern;
}
throw new Error(
'async-to-gen: includes and excludes must be RegExp or path strings. Got: ' + pattern
);
}