node.js
6.17 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
"use strict";
exports.__esModule = true;
// istanbul ignore next
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj["default"] = obj; return newObj; } }
// istanbul ignore next
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
var _sourceMapSupport = require("source-map-support");
var _sourceMapSupport2 = _interopRequireDefault(_sourceMapSupport);
var _cache = require("./cache");
var registerCache = _interopRequireWildcard(_cache);
var _transformationFileOptionsOptionManager = require("../../transformation/file/options/option-manager");
var _transformationFileOptionsOptionManager2 = _interopRequireDefault(_transformationFileOptionsOptionManager);
var _lodashObjectExtend = require("lodash/object/extend");
var _lodashObjectExtend2 = _interopRequireDefault(_lodashObjectExtend);
var _node = require("../node");
var babel = _interopRequireWildcard(_node);
var _lodashCollectionEach = require("lodash/collection/each");
var _lodashCollectionEach2 = _interopRequireDefault(_lodashCollectionEach);
var _util = require("../../util");
var util = _interopRequireWildcard(_util);
var _fs = require("fs");
var _fs2 = _interopRequireDefault(_fs);
var _path = require("path");
var _path2 = _interopRequireDefault(_path);
/**
* Install sourcemaps into node.
*/
_sourceMapSupport2["default"].install({
handleUncaughtExceptions: false,
retrieveSourceMap: function retrieveSourceMap(source) {
var map = maps && maps[source];
if (map) {
return {
url: null,
map: map
};
} else {
return null;
}
}
});
/**
* Load and setup cache.
*/
registerCache.load();
var cache = registerCache.get();
/**
* Store options.
*/
var transformOpts = {};
var ignore;
var only;
var oldHandlers = {};
var maps = {};
var cwd = process.cwd();
/**
* Get path from `filename` relative to the current working directory.
*/
var getRelativePath = function getRelativePath(filename) {
return _path2["default"].relative(cwd, filename);
};
/**
* Get last modified time for a `filename`.
*/
var mtime = function mtime(filename) {
return +_fs2["default"].statSync(filename).mtime;
};
/**
* Compile a `filename` with optional `opts`.
*/
var compile = function compile(filename) {
var opts = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var result;
opts.filename = filename;
var optsManager = new _transformationFileOptionsOptionManager2["default"]();
optsManager.mergeOptions(transformOpts);
opts = optsManager.init(opts);
var cacheKey = JSON.stringify(opts) + ":" + babel.version;
var env = process.env.BABEL_ENV || process.env.NODE_ENV;
if (env) cacheKey += ":" + env;
if (cache) {
var cached = cache[cacheKey];
if (cached && cached.mtime === mtime(filename)) {
result = cached;
}
}
if (!result) {
result = babel.transformFileSync(filename, _lodashObjectExtend2["default"](opts, {
sourceMap: "both",
ast: false
}));
}
if (cache) {
result.mtime = mtime(filename);
cache[cacheKey] = result;
}
maps[filename] = result.map;
return result.code;
};
/**
* Test if a `filename` should be ignored by Babel.
*/
var shouldIgnore = function shouldIgnore(filename) {
if (!ignore && !only) {
return getRelativePath(filename).split(_path2["default"].sep).indexOf("node_modules") >= 0;
} else {
return util.shouldIgnore(filename, ignore || [], only);
}
};
/**
* Monkey patch istanbul if it is running so that it works properly.
*/
var istanbulMonkey = {};
if (process.env.running_under_istanbul) {
// we need to monkey patch fs.readFileSync so we can hook into
// what istanbul gets, it's extremely dirty but it's the only way
var _readFileSync = _fs2["default"].readFileSync;
_fs2["default"].readFileSync = function (filename) {
if (istanbulMonkey[filename]) {
delete istanbulMonkey[filename];
var code = compile(filename, {
auxiliaryCommentBefore: "istanbul ignore next"
});
istanbulMonkey[filename] = true;
return code;
} else {
return _readFileSync.apply(this, arguments);
}
};
}
/**
* Replacement for the loader for istanbul.
*/
var istanbulLoader = function istanbulLoader(m, filename, old) {
istanbulMonkey[filename] = true;
old(m, filename);
};
/**
* Default loader.
*/
var normalLoader = function normalLoader(m, filename) {
m._compile(compile(filename), filename);
};
/**
* Register a loader for an extension.
*/
var registerExtension = function registerExtension(ext) {
var old = oldHandlers[ext] || oldHandlers[".js"] || require.extensions[".js"];
var loader = normalLoader;
if (process.env.running_under_istanbul) loader = istanbulLoader;
require.extensions[ext] = function (m, filename) {
if (shouldIgnore(filename)) {
old(m, filename);
} else {
loader(m, filename, old);
}
};
};
/**
* Register loader for given extensions.
*/
var hookExtensions = function hookExtensions(_exts) {
_lodashCollectionEach2["default"](oldHandlers, function (old, ext) {
if (old === undefined) {
delete require.extensions[ext];
} else {
require.extensions[ext] = old;
}
});
oldHandlers = {};
_lodashCollectionEach2["default"](_exts, function (ext) {
oldHandlers[ext] = require.extensions[ext];
registerExtension(ext);
});
};
/**
* Register loader for default extensions.
*/
hookExtensions(util.canCompile.EXTENSIONS);
/**
* Update options at runtime.
*/
exports["default"] = function () {
var opts = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
if (opts.only != null) only = util.arrayify(opts.only, util.regexify);
if (opts.ignore != null) ignore = util.arrayify(opts.ignore, util.regexify);
if (opts.extensions) hookExtensions(util.arrayify(opts.extensions));
if (opts.cache === false) cache = null;
delete opts.extensions;
delete opts.ignore;
delete opts.cache;
delete opts.only;
_lodashObjectExtend2["default"](transformOpts, opts);
};
module.exports = exports["default"];