项目原始demo,不改动
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
Den här utvecklingskatalogen är arkiverad. Du kan se filer och klona katalogen, men inte öppna ärenden eller genomföra push- eller pull-förfrågningar.

module-resolver.js 2.9 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 = this.options.lookupPaths.concat();
  53. lookupPaths.unshift(extraLookupPath);
  54. /**
  55. * Module._findPath is an internal method to Node.js, then one they use to
  56. * lookup file paths when require() is called. So, we are hooking into the
  57. * exact same logic that Node.js uses.
  58. */
  59. const result = Module._findPath(name, lookupPaths); // eslint-disable-line no-underscore-dangle
  60. if (!result) {
  61. throw new Error(`Cannot find module '${name}'`);
  62. }
  63. return result;
  64. }
  65. }
  66. //------------------------------------------------------------------------------
  67. // Public API
  68. //------------------------------------------------------------------------------
  69. module.exports = ModuleResolver;