index.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. "use strict";
  2. var __assign = (this && this.__assign) || function () {
  3. __assign = Object.assign || function(t) {
  4. for (var s, i = 1, n = arguments.length; i < n; i++) {
  5. s = arguments[i];
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  7. t[p] = s[p];
  8. }
  9. return t;
  10. };
  11. return __assign.apply(this, arguments);
  12. };
  13. var __importDefault = (this && this.__importDefault) || function (mod) {
  14. return (mod && mod.__esModule) ? mod : { "default": mod };
  15. };
  16. Object.defineProperty(exports, "__esModule", { value: true });
  17. exports.optionalRequireCwd = exports.optionalRequire = exports.makeOptionalRequire = exports.tryResolve = exports.tryRequire = exports.setDefaultLog = void 0;
  18. var assert_1 = __importDefault(require("assert"));
  19. var require_at_1 = __importDefault(require("require-at"));
  20. /* eslint-disable max-params, complexity, no-eval */
  21. // `require` from this module's context
  22. // Using `eval` to avoid tripping bundlers like webpack
  23. var xrequire = eval("require");
  24. // Copied from https://github.com/yarnpkg/berry/blob/d5454007c9c76becfa97b36a92de299a3694afd5/packages/yarnpkg-pnp/sources/loader/makeApi.ts#L27
  25. // Splits a require request into its components, or return null if the request is a file path
  26. var pnpDependencyNameRegExp = /^(?![a-zA-Z]:[\\/]|\\\\|\.{0,2}(?:\/|$))((?:node:)?(?:@[^/]+\/)?[^/]+)\/*(.*|)$/;
  27. /**
  28. * Change a module name request into a Yarn Berry PnP dependency name,
  29. * since the dependency name is what will be included in the error message.
  30. * For example, `optionalRequire('my-package/package.json')` will print a message like
  31. * `Your application tried to access my-package,` without the `/package.json` at the end of it.
  32. * This function grabs the dependency name only, or returns `null` if it can't find it.
  33. * @param {string} name Requested name
  34. * @returns {string} Dependency name
  35. */
  36. function getPnpDependencyName(name) {
  37. var dependencyNameMatch = name.match(pnpDependencyNameRegExp);
  38. if (!dependencyNameMatch)
  39. return null;
  40. return dependencyNameMatch[1];
  41. }
  42. /**
  43. * Check if an error from require is really due to the module not found,
  44. * and not because the module itself trying to require another module
  45. * that's not found.
  46. *
  47. * @param err - the error
  48. * @param name - name of the module being required
  49. * @returns true or false
  50. */
  51. function findModuleNotFound(err, name) {
  52. // Check the first line of the error message
  53. var msg = err.message.split("\n")[0];
  54. /* istanbul ignore if */
  55. if (!msg) {
  56. return false;
  57. }
  58. // Check for "Cannot find module 'foo'"
  59. if (msg.includes("'" + name + "'")) {
  60. return true;
  61. }
  62. var pnpDependencyName = getPnpDependencyName(name);
  63. if (pnpDependencyName) {
  64. return (
  65. // Check for "Your application tried to access foo (a peer dependency) ..." (Yarn Berry PnP)
  66. // https://github.com/yarnpkg/berry/blob/e81dc0d29bb2f41818d9c5c1c74bab1406fb979b/packages/yarnpkg-pnp/sources/loader/makeApi.ts#L680
  67. msg.includes(" " + pnpDependencyName + " ") ||
  68. // Check for "Your application tried to access foo. While ..." (Yarn Berry PnP)
  69. // https://github.com/yarnpkg/berry/blob/e81dc0d29bb2f41818d9c5c1c74bab1406fb979b/packages/yarnpkg-pnp/sources/loader/makeApi.ts#L704
  70. msg.includes(" " + pnpDependencyName + ". ") ||
  71. // Check for "Your application tried to access foo, but ..." (Yarn Berry PnP)
  72. // https://github.com/yarnpkg/berry/blob/e81dc0d29bb2f41818d9c5c1c74bab1406fb979b/packages/yarnpkg-pnp/sources/loader/makeApi.ts#L718
  73. msg.includes(" " + pnpDependencyName + ", "));
  74. }
  75. return false;
  76. }
  77. /**
  78. * Default log function
  79. *
  80. * @param message - message to log
  81. * @param path - path of the module to require
  82. */
  83. function defaultLog(message, path) {
  84. console.log("Just FYI: " + message + "; Path \"" + path + "\"");
  85. }
  86. var __defaultLog = defaultLog;
  87. function setDefaultLog(log) {
  88. __defaultLog = log;
  89. }
  90. exports.setDefaultLog = setDefaultLog;
  91. function _getOptions(optsOrMsg, requireFunction, log) {
  92. if (requireFunction === void 0) { requireFunction = xrequire; }
  93. if (typeof optsOrMsg === "object") {
  94. var opts = __assign({ require: requireFunction, log: log }, optsOrMsg);
  95. (0, assert_1.default)(!(opts.hasOwnProperty("notFound") && opts.hasOwnProperty("default")), "optionalRequire: options set with both `notFound` and `default`");
  96. return opts;
  97. }
  98. else {
  99. return { require: requireFunction, log: log, message: optsOrMsg };
  100. }
  101. }
  102. /**
  103. * Internal optional require implementation
  104. *
  105. * @param path - path to module to require
  106. * @param optsOrMsg - options or message to log in case module not found
  107. * @returns require or resolve result
  108. */
  109. function _optionalRequire(path, opts) {
  110. try {
  111. return opts.resolve ? opts.require.resolve(path) : opts.require(path);
  112. }
  113. catch (e) {
  114. // exception that's not due to the module itself not found
  115. if (e.code !== "MODULE_NOT_FOUND" || !findModuleNotFound(e, path)) {
  116. // if the module we are requiring fail because it try to require a
  117. // module that's not found, then we have to report this as failed.
  118. if (typeof opts.fail === "function") {
  119. return opts.fail(e);
  120. }
  121. throw e;
  122. }
  123. if (opts.message) {
  124. var message = opts.message === true ? "" : opts.message + " - ";
  125. var r = opts.resolve ? "resolved" : "found";
  126. opts.log(message + "optional module not " + r, path);
  127. }
  128. if (typeof opts.notFound === "function") {
  129. return opts.notFound(e);
  130. }
  131. return opts.default;
  132. }
  133. }
  134. /**
  135. * try to require a module with optional handling in case it's not found or fail to require
  136. *
  137. * @param callerRequire - `require` from caller context
  138. * @param path - path to module to require
  139. * @param optsOrMsg - options or message to log in case of exceptions
  140. * @returns require result
  141. */
  142. function tryRequire(callerRequire, path, optsOrMsg) {
  143. var opts = _getOptions(optsOrMsg, callerRequire, __defaultLog);
  144. opts.resolve = false;
  145. return _optionalRequire(path, opts);
  146. }
  147. exports.tryRequire = tryRequire;
  148. /**
  149. * try to resolve a module with optional handling in case it's not found or fail to require
  150. *
  151. * @param callerRequire - `require` from caller context
  152. * @param path - path to module to require
  153. * @param optsOrMsg - options or message to log in case of exceptions
  154. * @returns resolve result
  155. */
  156. function tryResolve(callerRequire, path, optsOrMsg) {
  157. var opts = _getOptions(optsOrMsg, callerRequire, __defaultLog);
  158. opts.resolve = true;
  159. return _optionalRequire(path, opts);
  160. }
  161. exports.tryResolve = tryResolve;
  162. /**
  163. * Make an optional require function, using the `require` from caller's context.
  164. *
  165. * @param callerRequire - `require` from caller's context
  166. * @param log - function to log if module is not found
  167. * @returns required module
  168. */
  169. function makeOptionalRequire(callerRequire, log) {
  170. var x = function (path, optsOrMsg) {
  171. var opts = _getOptions(optsOrMsg, callerRequire, x.log);
  172. return _optionalRequire(path, opts);
  173. };
  174. x.resolve = function (path, optsOrMsg) {
  175. var opts = _getOptions(optsOrMsg, callerRequire, x.log);
  176. opts.resolve = true;
  177. return _optionalRequire(path, opts);
  178. };
  179. x.log = log || __defaultLog;
  180. return x;
  181. }
  182. exports.makeOptionalRequire = makeOptionalRequire;
  183. /**
  184. * A default optionalRequire function using `require` from optional-require's context.
  185. *
  186. * @remarks because `require` is from optional-require's context, you won't be able to
  187. * do `optionalRequire("./my-module")`.
  188. *
  189. * You can still override the `require` using `options.require` with the one from your
  190. * calling context.
  191. *
  192. */
  193. exports.optionalRequire = makeOptionalRequire(xrequire);
  194. /**
  195. * An optionalRequire function using `require` from CWD context
  196. *
  197. * @remarks because `require` is from CWD context, if you do `optionalRequireCwd("./my-module")`
  198. * then it will look for `my-module` in CWD.
  199. *
  200. * @remarks You can still override the `require` using `options.require` with the one from your
  201. * calling context.
  202. */
  203. exports.optionalRequireCwd = makeOptionalRequire((0, require_at_1.default)(process.cwd()));
  204. //# sourceMappingURL=index.js.map