UNPKG

1.84 kBJavaScriptView Raw
1"use strict";
2
3const {
4 addHook
5} = require("pirates");
6
7const sourceMapSupport = require("source-map-support");
8
9let piratesRevert;
10const maps = Object.create(null);
11
12function installSourceMapSupport() {
13 installSourceMapSupport = () => {};
14
15 sourceMapSupport.install({
16 handleUncaughtExceptions: false,
17 environment: "node",
18
19 retrieveSourceMap(filename) {
20 const map = maps == null ? void 0 : maps[filename];
21
22 if (map) {
23 return {
24 url: null,
25 map: map
26 };
27 } else {
28 return null;
29 }
30 }
31
32 });
33}
34
35{
36 const Module = require("module");
37
38 let compiling = false;
39 const internalModuleCache = Module._cache;
40
41 var compileBabel7 = function compileBabel7(client, code, filename) {
42 if (!client.isLocalClient) return compile(client, code, filename);
43 if (compiling) return code;
44 const globalModuleCache = Module._cache;
45
46 try {
47 compiling = true;
48 Module._cache = internalModuleCache;
49 return compile(client, code, filename);
50 } finally {
51 compiling = false;
52 Module._cache = globalModuleCache;
53 }
54 };
55}
56
57function compile(client, inputCode, filename) {
58 const result = client.transform(inputCode, filename);
59 if (result === null) return inputCode;
60 const {
61 code,
62 map
63 } = result;
64
65 if (map) {
66 maps[filename] = map;
67 installSourceMapSupport();
68 }
69
70 return code;
71}
72
73exports.register = function register(client, opts = {}) {
74 var _opts$extensions;
75
76 if (piratesRevert) piratesRevert();
77 piratesRevert = addHook(compileBabel7.bind(null, client), {
78 exts: (_opts$extensions = opts.extensions) != null ? _opts$extensions : client.getDefaultExtensions(),
79 ignoreNodeModules: false
80 });
81 client.setOptions(opts);
82};
83
84exports.revert = function revert() {
85 if (piratesRevert) piratesRevert();
86};
\No newline at end of file