Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

module-resolver.js 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * @fileoverview Implements the Node.js require.resolve algorithm
  3. * @author Nicholas C. Zakas
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const Module = require("module");
  10. //------------------------------------------------------------------------------
  11. // Private
  12. //------------------------------------------------------------------------------
  13. const DEFAULT_OPTIONS = {
  14. /*
  15. * module.paths is an array of paths to search for resolving things relative
  16. * to this file. Module.globalPaths contains all of the special Node.js
  17. * directories that can also be searched for modules.
  18. *
  19. * Need to check for existence of module.paths because Jest seems not to
  20. * include it. See https://github.com/eslint/eslint/issues/5791.
  21. */
  22. lookupPaths: module.paths ? module.paths.concat(Module.globalPaths) : Module.globalPaths.concat()
  23. };
  24. /**
  25. * Resolves modules based on a set of options.
  26. */
  27. class ModuleResolver {
  28. /**
  29. * Resolves modules based on a set of options.
  30. * @param {Object} options The options for resolving modules.
  31. * @param {string[]} options.lookupPaths An array of paths to include in the
  32. * lookup with the highest priority paths coming first.
  33. */
  34. constructor(options) {
  35. this.options = Object.assign({}, DEFAULT_OPTIONS, options || {});
  36. }
  37. /**
  38. * Resolves the file location of a given module relative to the configured
  39. * lookup paths.
  40. * @param {string} name The module name to resolve.
  41. * @param {string} extraLookupPath An extra path to look into for the module.
  42. * This path is used with the highest priority.
  43. * @returns {string} The resolved file path for the module.
  44. * @throws {Error} If the module cannot be resolved.
  45. */
  46. resolve(name, extraLookupPath) {
  47. /*
  48. * First, clone the lookup paths so we're not messing things up for
  49. * subsequent calls to this function. Then, move the extraLookupPath to the
  50. * top of the lookup paths list so it will be searched first.
  51. */
  52. const lookupPaths = [extraLookupPath, ...this.options.lookupPaths];
  53. /**
  54. * Module._findPath is an internal method to Node.js, then one they use to
  55. * lookup file paths when require() is called. So, we are hooking into the
  56. * exact same logic that Node.js uses.
  57. */
  58. const result = Module._findPath(name, lookupPaths); // eslint-disable-line no-underscore-dangle
  59. if (!result) {
  60. throw new Error(`Cannot find module '${name}'`);
  61. }
  62. return result;
  63. }
  64. }
  65. //------------------------------------------------------------------------------
  66. // Public API
  67. //------------------------------------------------------------------------------
  68. module.exports = ModuleResolver;