hook.js 1.8 KB

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