register.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. "use strict";
  2. var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
  3. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  4. if (ar || !(i in from)) {
  5. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  6. ar[i] = from[i];
  7. }
  8. }
  9. return to.concat(ar || Array.prototype.slice.call(from));
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. exports.register = void 0;
  13. var match_path_sync_1 = require("./match-path-sync");
  14. var config_loader_1 = require("./config-loader");
  15. var options_1 = require("./options");
  16. var noOp = function () { return void 0; };
  17. function getCoreModules(builtinModules) {
  18. builtinModules = builtinModules || [
  19. "assert",
  20. "buffer",
  21. "child_process",
  22. "cluster",
  23. "crypto",
  24. "dgram",
  25. "dns",
  26. "domain",
  27. "events",
  28. "fs",
  29. "http",
  30. "https",
  31. "net",
  32. "os",
  33. "path",
  34. "punycode",
  35. "querystring",
  36. "readline",
  37. "stream",
  38. "string_decoder",
  39. "tls",
  40. "tty",
  41. "url",
  42. "util",
  43. "v8",
  44. "vm",
  45. "zlib",
  46. ];
  47. var coreModules = {};
  48. for (var _i = 0, builtinModules_1 = builtinModules; _i < builtinModules_1.length; _i++) {
  49. var module_1 = builtinModules_1[_i];
  50. coreModules[module_1] = true;
  51. }
  52. return coreModules;
  53. }
  54. /**
  55. * Installs a custom module load function that can adhere to paths in tsconfig.
  56. * Returns a function to undo paths registration.
  57. */
  58. function register(explicitParams) {
  59. var configLoaderResult = (0, config_loader_1.configLoader)({
  60. cwd: options_1.options.cwd,
  61. explicitParams: explicitParams,
  62. });
  63. if (configLoaderResult.resultType === "failed") {
  64. console.warn("".concat(configLoaderResult.message, ". tsconfig-paths will be skipped"));
  65. return noOp;
  66. }
  67. var matchPath = (0, match_path_sync_1.createMatchPath)(configLoaderResult.absoluteBaseUrl, configLoaderResult.paths, configLoaderResult.mainFields, configLoaderResult.addMatchAll);
  68. // Patch node's module loading
  69. // tslint:disable-next-line:no-require-imports variable-name
  70. var Module = require("module");
  71. var originalResolveFilename = Module._resolveFilename;
  72. var coreModules = getCoreModules(Module.builtinModules);
  73. // tslint:disable-next-line:no-any
  74. Module._resolveFilename = function (request, _parent) {
  75. var isCoreModule = coreModules.hasOwnProperty(request);
  76. if (!isCoreModule) {
  77. var found = matchPath(request);
  78. if (found) {
  79. var modifiedArguments = __spreadArray([found], [].slice.call(arguments, 1), true); // Passes all arguments. Even those that is not specified above.
  80. // tslint:disable-next-line:no-invalid-this
  81. return originalResolveFilename.apply(this, modifiedArguments);
  82. }
  83. }
  84. // tslint:disable-next-line:no-invalid-this
  85. return originalResolveFilename.apply(this, arguments);
  86. };
  87. return function () {
  88. // Return node's module loading to original state.
  89. Module._resolveFilename = originalResolveFilename;
  90. };
  91. }
  92. exports.register = register;
  93. //# sourceMappingURL=register.js.map