register.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 noOp = function () { return void 0; };
  16. function getCoreModules(builtinModules) {
  17. builtinModules = builtinModules || [
  18. "assert",
  19. "buffer",
  20. "child_process",
  21. "cluster",
  22. "crypto",
  23. "dgram",
  24. "dns",
  25. "domain",
  26. "events",
  27. "fs",
  28. "http",
  29. "https",
  30. "net",
  31. "os",
  32. "path",
  33. "punycode",
  34. "querystring",
  35. "readline",
  36. "stream",
  37. "string_decoder",
  38. "tls",
  39. "tty",
  40. "url",
  41. "util",
  42. "v8",
  43. "vm",
  44. "zlib",
  45. ];
  46. var coreModules = {};
  47. for (var _i = 0, builtinModules_1 = builtinModules; _i < builtinModules_1.length; _i++) {
  48. var module_1 = builtinModules_1[_i];
  49. coreModules[module_1] = true;
  50. }
  51. return coreModules;
  52. }
  53. /**
  54. * Installs a custom module load function that can adhere to paths in tsconfig.
  55. * Returns a function to undo paths registration.
  56. */
  57. function register(params) {
  58. var cwd;
  59. var explicitParams;
  60. if (params) {
  61. cwd = params.cwd;
  62. if (params.baseUrl || params.paths) {
  63. explicitParams = params;
  64. }
  65. }
  66. else {
  67. // eslint-disable-next-line
  68. var minimist = require("minimist");
  69. var argv = minimist(process.argv.slice(2), {
  70. // eslint-disable-next-line id-denylist
  71. string: ["project"],
  72. alias: {
  73. project: ["P"],
  74. },
  75. });
  76. cwd = argv.project;
  77. }
  78. var configLoaderResult = (0, config_loader_1.configLoader)({
  79. cwd: cwd !== null && cwd !== void 0 ? cwd : process.cwd(),
  80. explicitParams: explicitParams,
  81. });
  82. if (configLoaderResult.resultType === "failed") {
  83. console.warn("".concat(configLoaderResult.message, ". tsconfig-paths will be skipped"));
  84. return noOp;
  85. }
  86. var matchPath = (0, match_path_sync_1.createMatchPath)(configLoaderResult.absoluteBaseUrl, configLoaderResult.paths, configLoaderResult.mainFields, configLoaderResult.addMatchAll);
  87. // Patch node's module loading
  88. // eslint-disable-next-line @typescript-eslint/no-require-imports,@typescript-eslint/no-var-requires
  89. var Module = require("module");
  90. // eslint-disable-next-line no-underscore-dangle
  91. var originalResolveFilename = Module._resolveFilename;
  92. var coreModules = getCoreModules(Module.builtinModules);
  93. // eslint-disable-next-line @typescript-eslint/no-explicit-any,no-underscore-dangle
  94. Module._resolveFilename = function (request, _parent) {
  95. var isCoreModule = coreModules.hasOwnProperty(request);
  96. if (!isCoreModule) {
  97. var found = matchPath(request);
  98. if (found) {
  99. var modifiedArguments = __spreadArray([found], [].slice.call(arguments, 1), true); // Passes all arguments. Even those that is not specified above.
  100. return originalResolveFilename.apply(this, modifiedArguments);
  101. }
  102. }
  103. return originalResolveFilename.apply(this, arguments);
  104. };
  105. return function () {
  106. // Return node's module loading to original state.
  107. // eslint-disable-next-line no-underscore-dangle
  108. Module._resolveFilename = originalResolveFilename;
  109. };
  110. }
  111. exports.register = register;
  112. //# sourceMappingURL=register.js.map