1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.revert = revert;
|
7 | exports.default = register;
|
8 |
|
9 | var _cloneDeep = require("clone-deep");
|
10 |
|
11 | var _sourceMapSupport = require("source-map-support");
|
12 |
|
13 | var registerCache = require("./cache");
|
14 |
|
15 | var babel = require("@babel/core");
|
16 |
|
17 | var _pirates = require("pirates");
|
18 |
|
19 | var _fs = require("fs");
|
20 |
|
21 | var _path = require("path");
|
22 |
|
23 | var _module = require("module");
|
24 |
|
25 | const maps = {};
|
26 | let transformOpts = {};
|
27 | let piratesRevert = null;
|
28 |
|
29 | function installSourceMapSupport() {
|
30 | _sourceMapSupport.install({
|
31 | handleUncaughtExceptions: false,
|
32 | environment: "node",
|
33 |
|
34 | retrieveSourceMap(source) {
|
35 | const map = maps && maps[source];
|
36 |
|
37 | if (map) {
|
38 | return {
|
39 | url: null,
|
40 | map: map
|
41 | };
|
42 | } else {
|
43 | return null;
|
44 | }
|
45 | }
|
46 |
|
47 | });
|
48 | }
|
49 |
|
50 | let cache;
|
51 |
|
52 | function mtime(filename) {
|
53 | return +_fs.statSync(filename).mtime;
|
54 | }
|
55 |
|
56 | function compile(code, filename) {
|
57 | const opts = new babel.OptionManager().init(Object.assign({
|
58 | sourceRoot: _path.dirname(filename) + _path.sep
|
59 | }, _cloneDeep(transformOpts), {
|
60 | filename
|
61 | }));
|
62 | if (opts === null) return code;
|
63 | let cacheKey = `${JSON.stringify(opts)}:${babel.version}`;
|
64 | const env = babel.getEnv(false);
|
65 | if (env) cacheKey += `:${env}`;
|
66 | let cached = cache && cache[cacheKey];
|
67 |
|
68 | if (!cached || cached.mtime !== mtime(filename)) {
|
69 | cached = babel.transform(code, Object.assign({}, opts, {
|
70 | sourceMaps: opts.sourceMaps === undefined ? "both" : opts.sourceMaps,
|
71 | ast: false
|
72 | }));
|
73 |
|
74 | if (cache) {
|
75 | cache[cacheKey] = cached;
|
76 | cached.mtime = mtime(filename);
|
77 | registerCache.setDirty();
|
78 | }
|
79 | }
|
80 |
|
81 | if (cached.map) {
|
82 | if (Object.keys(maps).length === 0) {
|
83 | installSourceMapSupport();
|
84 | }
|
85 |
|
86 | maps[filename] = cached.map;
|
87 | }
|
88 |
|
89 | return cached.code;
|
90 | }
|
91 |
|
92 | let compiling = false;
|
93 | const internalModuleCache = _module._cache;
|
94 |
|
95 | function compileHook(code, filename) {
|
96 | if (compiling) return code;
|
97 | const globalModuleCache = _module._cache;
|
98 |
|
99 | try {
|
100 | compiling = true;
|
101 | _module._cache = internalModuleCache;
|
102 | return compile(code, filename);
|
103 | } finally {
|
104 | compiling = false;
|
105 | _module._cache = globalModuleCache;
|
106 | }
|
107 | }
|
108 |
|
109 | function hookExtensions(exts) {
|
110 | if (piratesRevert) piratesRevert();
|
111 | piratesRevert = (0, _pirates.addHook)(compileHook, {
|
112 | exts,
|
113 | ignoreNodeModules: false
|
114 | });
|
115 | }
|
116 |
|
117 | function revert() {
|
118 | if (piratesRevert) piratesRevert();
|
119 | }
|
120 |
|
121 | function escapeRegExp(string) {
|
122 | return string.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&");
|
123 | }
|
124 |
|
125 | function register(opts = {}) {
|
126 | opts = Object.assign({}, opts);
|
127 | hookExtensions(opts.extensions || babel.DEFAULT_EXTENSIONS);
|
128 |
|
129 | if (opts.cache === false && cache) {
|
130 | registerCache.clear();
|
131 | cache = null;
|
132 | } else if (opts.cache !== false && !cache) {
|
133 | registerCache.load();
|
134 | cache = registerCache.get();
|
135 | }
|
136 |
|
137 | delete opts.extensions;
|
138 | delete opts.cache;
|
139 | transformOpts = Object.assign({}, opts, {
|
140 | caller: Object.assign({
|
141 | name: "@babel/register"
|
142 | }, opts.caller || {})
|
143 | });
|
144 | let {
|
145 | cwd = "."
|
146 | } = transformOpts;
|
147 | cwd = transformOpts.cwd = _path.resolve(cwd);
|
148 |
|
149 | if (transformOpts.ignore === undefined && transformOpts.only === undefined) {
|
150 | transformOpts.only = [new RegExp("^" + escapeRegExp(cwd), "i")];
|
151 | transformOpts.ignore = [new RegExp("^" + escapeRegExp(cwd) + "(?:" + _path.sep + ".*)?" + escapeRegExp(_path.sep + "node_modules" + _path.sep), "i")];
|
152 | }
|
153 | } |
\ | No newline at end of file |