-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
193 lines (147 loc) · 3.51 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
'use strict';
const path = require('path');
const fs = require('fs');
const sourceMapSupport = require('source-map-support');
const escapeRegExp = require('lodash.escaperegexp');
const isRegExp = require('lodash.isregexp');
const minimatch = require('minimatch');
const addHook = require('pirates').addHook;
const arrify = require('arrify');
const slash = require('slash');
const buble = require('buble');
const registerCache = require('./cache');
const startsWith = (str, match) => str.indexOf(match) === 0;
const regexify = val => {
if (!val) {
return new RegExp(/.^/);
}
if (Array.isArray(val)) {
val = new RegExp(val.map(escapeRegExp).join('|'), 'i');
}
if (typeof val === 'string') {
val = slash(val);
if (startsWith(val, './') || startsWith(val, '*/')) {
val = val.slice(2);
}
if (startsWith(val, '**/')) {
val = val.slice(3);
}
const regex = minimatch.makeRe(val, {nocase: true});
return new RegExp(regex.source.slice(1, -1), 'i');
}
if (isRegExp(val)) {
return val;
}
return new TypeError('Illegial type for regexify');
};
const maps = {};
const transformOpts = {};
let piratesRevert;
let ignore;
let only;
sourceMapSupport.install({
handleUncaughtExtensions: false,
environment: 'node',
retrieveSourceMap: source => {
const map = maps[source];
if (map) {
return {
url: null,
map
};
}
return null;
}
});
registerCache.load();
let cache = registerCache.get();
const cwd = process.cwd();
const getRelativePath = filename => path.relative(cwd, filename);
const mtime = filename => Number(fs.statSync(filename).mtime);
const _shouldIgnore = (pattern, filename) => {
return typeof pattern === 'function' ? pattern(filename) : pattern.test(filename);
};
const shouldIgnore = filename => {
if (!ignore && !only) {
return getRelativePath(filename).split(path.sep).indexOf('node_modules') >= 0;
}
filename = filename.replace(/\\/g, '/');
if (only) {
for (const pattern of only) {
if (_shouldIgnore(pattern, filename)) {
return false;
}
}
return true;
}
if (ignore.length > 0) {
for (const pattern of ignore) {
if (_shouldIgnore(pattern, filename)) {
return true;
}
}
}
return false;
};
const compile = (code, filename) => {
if (shouldIgnore(filename)) {
return code;
}
const cacheKey = `${filename}:${JSON.stringify(transformOpts)}:${buble.VERSION}`;
if (cache) {
const cached = cache[cacheKey];
if (cached && cached.mtime === mtime(filename)) {
return cached.code;
}
}
const opts = Object.assign({}, transformOpts, {
source: filename
});
const result = buble.transform(code, opts);
if (cache) {
result.mtime = mtime(filename);
cache[cacheKey] = result;
}
maps[filename] = result.map;
return result.code;
};
const hookExtensions = exts => {
if (piratesRevert) {
piratesRevert();
}
piratesRevert = addHook(compile, {
exts,
ignoreNodeModules: false
});
};
const revert = () => {
if (piratesRevert) {
piratesRevert();
}
delete require.cache[require.resolve(__filename)];
};
const register = opts => {
opts = opts || {};
if (opts.extensions) {
hookExtensions(opts.extensions);
}
if (opts.cache === false) {
cache = null;
}
if (opts.ignore) {
ignore = arrify(opts.ignore).map(regexify);
}
if (opts.only) {
only = arrify(opts.only).map(regexify);
}
delete opts.extensions;
delete opts.cache;
delete opts.ignore;
delete opts.only;
Object.assign(transformOpts, opts);
};
register({
extensions: ['.js', '.jsx', '.es6', '.es']
});
module.exports = register;
module.exports.revert = revert;