1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = register;
|
7 | exports.revert = revert;
|
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, fileMtime;
|
67 |
|
68 | if (cache) {
|
69 | cached = cache[cacheKey];
|
70 | fileMtime = mtime(filename);
|
71 | }
|
72 |
|
73 | if (!cached || cached.mtime !== fileMtime) {
|
74 | cached = babel.transform(code, Object.assign({}, opts, {
|
75 | sourceMaps: opts.sourceMaps === undefined ? "both" : opts.sourceMaps,
|
76 | ast: false
|
77 | }));
|
78 |
|
79 | if (cache) {
|
80 | cache[cacheKey] = cached;
|
81 | cached.mtime = fileMtime;
|
82 | registerCache.setDirty();
|
83 | }
|
84 | }
|
85 |
|
86 | if (cached.map) {
|
87 | if (Object.keys(maps).length === 0) {
|
88 | installSourceMapSupport();
|
89 | }
|
90 |
|
91 | maps[filename] = cached.map;
|
92 | }
|
93 |
|
94 | return cached.code;
|
95 | }
|
96 |
|
97 | let compiling = false;
|
98 | const internalModuleCache = _module._cache;
|
99 |
|
100 | function compileHook(code, filename) {
|
101 | if (compiling) return code;
|
102 | const globalModuleCache = _module._cache;
|
103 |
|
104 | try {
|
105 | compiling = true;
|
106 | _module._cache = internalModuleCache;
|
107 | return compile(code, filename);
|
108 | } finally {
|
109 | compiling = false;
|
110 | _module._cache = globalModuleCache;
|
111 | }
|
112 | }
|
113 |
|
114 | function hookExtensions(exts) {
|
115 | if (piratesRevert) piratesRevert();
|
116 | piratesRevert = (0, _pirates.addHook)(compileHook, {
|
117 | exts,
|
118 | ignoreNodeModules: false
|
119 | });
|
120 | }
|
121 |
|
122 | function revert() {
|
123 | if (piratesRevert) piratesRevert();
|
124 | }
|
125 |
|
126 | function escapeRegExp(string) {
|
127 | return string.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&");
|
128 | }
|
129 |
|
130 | function register(opts = {}) {
|
131 | opts = Object.assign({}, opts);
|
132 | hookExtensions(opts.extensions || babel.DEFAULT_EXTENSIONS);
|
133 |
|
134 | if (opts.cache === false && cache) {
|
135 | registerCache.clear();
|
136 | cache = null;
|
137 | } else if (opts.cache !== false && !cache) {
|
138 | registerCache.load();
|
139 | cache = registerCache.get();
|
140 | }
|
141 |
|
142 | delete opts.extensions;
|
143 | delete opts.cache;
|
144 | transformOpts = Object.assign({}, opts, {
|
145 | caller: Object.assign({
|
146 | name: "@babel/register"
|
147 | }, opts.caller || {})
|
148 | });
|
149 | let {
|
150 | cwd = "."
|
151 | } = transformOpts;
|
152 | cwd = transformOpts.cwd = _path.resolve(cwd);
|
153 |
|
154 | if (transformOpts.ignore === undefined && transformOpts.only === undefined) {
|
155 | transformOpts.only = [new RegExp("^" + escapeRegExp(cwd), "i")];
|
156 | transformOpts.ignore = [new RegExp("^" + escapeRegExp(cwd) + "(?:" + _path.sep + ".*)?" + escapeRegExp(_path.sep + "node_modules" + _path.sep), "i")];
|
157 | }
|
158 | } |
\ | No newline at end of file |