requizzle.js 3.1 KB

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