hook.cjs 1.8 KB

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