requizzle.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. Copyright 2014 Google LLC
  3. Copyright 2012-2013 Johannes Ewald
  4. Use of this source code is governed by the MIT License, available in this package's LICENSE file
  5. or at http://opensource.org/licenses/MIT.
  6. */
  7. /** @module lib/requizzle */
  8. const loader = require('./loader');
  9. const Module = require('module');
  10. const NATIVE_MODULE_PREFIX = 'node:';
  11. /**
  12. * Function that returns text to swizzle into the module.
  13. *
  14. * @typedef module:lib/requizzle~wrapperFunction
  15. * @type {function}
  16. * @param {string} targetPath - The path to the target module.
  17. * @param {string} parentModulePath - The path to the module that is requiring the target module.
  18. * @return {string} The text to insert before or after the module's source code.
  19. */
  20. /**
  21. * Options for the wrappers that will be swizzled into the target module.
  22. *
  23. * @typedef module:lib/requizzle~options
  24. * @type {Object}
  25. * @property {Object=} options.extras - Functions that generate text to swizzle into the target
  26. * module.
  27. * @property {module:lib/requizzle~wrapperFunction} options.extras.after - Function that returns
  28. * text to insert after the module's source code.
  29. * @property {module:lib/requizzle~wrapperFunction} options.extras.before - Function that returns
  30. * text to insert before the module's source code.
  31. * @property {(Array.<string>|string)} options.requirePaths - Additional paths to search when
  32. * resolving module paths in the target module.
  33. */
  34. function isNativeModule(targetPath, parentModule) {
  35. let lookupPaths;
  36. let isNative = false;
  37. if (targetPath.startsWith(NATIVE_MODULE_PREFIX)) {
  38. isNative = true;
  39. } else {
  40. lookupPaths = Module._resolveLookupPaths(targetPath, parentModule, true);
  41. /* istanbul ignore next */
  42. isNative =
  43. lookupPaths === null ||
  44. (lookupPaths.length === 2 && lookupPaths[1].length === 0 && lookupPaths[0] === targetPath);
  45. }
  46. return isNative;
  47. }
  48. /**
  49. * Create a `Requizzle` instance. If you provide options, Requizzle will default to those options
  50. * when you call {@link Requizzle#requizzle}.
  51. *
  52. * @class
  53. * @param {!module:lib/requizzle~options} options - Options for the wrappers that will be swizzled
  54. * into the target module.
  55. * @param {Object=} cache - For internal use.
  56. */
  57. class Requizzle {
  58. constructor(options, cache) {
  59. this._options = options;
  60. this._cache = cache || {
  61. module: {},
  62. source: {},
  63. };
  64. }
  65. /**
  66. * Load the module, swizzling in the requested changes.
  67. *
  68. * @param {!string} targetPath - The path to the module that will be loaded.
  69. * @return {Module} The swizzled module.
  70. */
  71. requizzle(targetPath) {
  72. const options = this._options;
  73. const parentModule = options.parent;
  74. let targetModule;
  75. let wrapper;
  76. // Don't interfere with native modules
  77. if (isNativeModule(targetPath, parentModule)) {
  78. return require(targetPath);
  79. }
  80. // Resolve the filename relative to the parent module
  81. targetPath = Module._resolveFilename(targetPath, parentModule);
  82. wrapper = loader.createWrapper(targetPath, parentModule, this._cache, this._options);
  83. targetModule = loader.load(targetPath, parentModule, wrapper, this._cache, this._options);
  84. return targetModule.exports;
  85. }
  86. }
  87. module.exports = Requizzle;