node.js
4.37 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
"use strict";
exports.__esModule = true;
var _keys = require("babel-runtime/core-js/object/keys");
var _keys2 = _interopRequireDefault(_keys);
var _stringify = require("babel-runtime/core-js/json/stringify");
var _stringify2 = _interopRequireDefault(_stringify);
exports.default = function () {
var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
if (opts.only != null) only = _babelCore.util.arrayify(opts.only, _babelCore.util.regexify);
if (opts.ignore != null) ignore = _babelCore.util.arrayify(opts.ignore, _babelCore.util.regexify);
if (opts.extensions) hookExtensions(_babelCore.util.arrayify(opts.extensions));
if (opts.cache === false) cache = null;
delete opts.extensions;
delete opts.ignore;
delete opts.cache;
delete opts.only;
(0, _extend2.default)(transformOpts, opts);
};
var _cloneDeep = require("lodash/cloneDeep");
var _cloneDeep2 = _interopRequireDefault(_cloneDeep);
var _sourceMapSupport = require("source-map-support");
var _sourceMapSupport2 = _interopRequireDefault(_sourceMapSupport);
var _cache = require("./cache");
var registerCache = _interopRequireWildcard(_cache);
var _extend = require("lodash/extend");
var _extend2 = _interopRequireDefault(_extend);
var _babelCore = require("babel-core");
var babel = _interopRequireWildcard(_babelCore);
var _fs = require("fs");
var _fs2 = _interopRequireDefault(_fs);
var _path = require("path");
var _path2 = _interopRequireDefault(_path);
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; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
_sourceMapSupport2.default.install({
handleUncaughtExceptions: false,
environment: "node",
retrieveSourceMap: function retrieveSourceMap(source) {
var map = maps && maps[source];
if (map) {
return {
url: null,
map: map
};
} else {
return null;
}
}
});
registerCache.load();
var cache = registerCache.get();
var transformOpts = {};
var ignore = void 0;
var only = void 0;
var oldHandlers = {};
var maps = {};
var cwd = process.cwd();
function getRelativePath(filename) {
return _path2.default.relative(cwd, filename);
}
function mtime(filename) {
return +_fs2.default.statSync(filename).mtime;
}
function compile(filename) {
var result = void 0;
var opts = new _babelCore.OptionManager().init((0, _extend2.default)({ sourceRoot: _path2.default.dirname(filename) }, (0, _cloneDeep2.default)(transformOpts), { filename: filename }));
var cacheKey = (0, _stringify2.default)(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, (0, _extend2.default)(opts, {
babelrc: false,
sourceMaps: "both",
ast: false
}));
}
if (cache) {
cache[cacheKey] = result;
result.mtime = mtime(filename);
}
maps[filename] = result.map;
return result.code;
}
function shouldIgnore(filename) {
if (!ignore && !only) {
return getRelativePath(filename).split(_path2.default.sep).indexOf("node_modules") >= 0;
} else {
return _babelCore.util.shouldIgnore(filename, ignore || [], only);
}
}
function loader(m, filename) {
m._compile(compile(filename), filename);
}
function registerExtension(ext) {
var old = oldHandlers[ext] || oldHandlers[".js"] || require.extensions[".js"];
require.extensions[ext] = function (m, filename) {
if (shouldIgnore(filename)) {
old(m, filename);
} else {
loader(m, filename, old);
}
};
}
function hookExtensions(_exts) {
(0, _keys2.default)(oldHandlers).forEach(function (ext) {
var old = oldHandlers[ext];
if (old === undefined) {
delete require.extensions[ext];
} else {
require.extensions[ext] = old;
}
});
oldHandlers = {};
_exts.forEach(function (ext) {
oldHandlers[ext] = require.extensions[ext];
registerExtension(ext);
});
}
hookExtensions(_babelCore.util.canCompile.EXTENSIONS);
module.exports = exports["default"];