loader.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. const _ = require('lodash');
  8. const fs = require('fs');
  9. const Module = require('module');
  10. const originalWrapper = Module.wrapper.slice(0);
  11. const requizzleWrappers = {
  12. extras: require('./wrappers/extras'),
  13. requirePaths: require('./wrappers/requirepaths'),
  14. strict: require('./wrappers/strict'),
  15. };
  16. function wrap(wrappers, script) {
  17. return wrappers[0] + script + wrappers[1];
  18. }
  19. function replaceWrapper(wrapperObj) {
  20. const joiner = '\n';
  21. const before = wrapperObj.before.join(joiner);
  22. const after = wrapperObj.after.join(joiner);
  23. const wrappers = [originalWrapper[0] + before, after + originalWrapper[1]];
  24. Module.wrap = wrap.bind(null, wrappers);
  25. }
  26. function restoreWrapper() {
  27. Module.wrap = wrap.bind(null, originalWrapper);
  28. }
  29. function createModule(targetPath, parentModule, moduleCache) {
  30. moduleCache[targetPath] = moduleCache[targetPath] || new Module(targetPath, parentModule);
  31. return moduleCache[targetPath];
  32. }
  33. /**
  34. * Wrapper for `require()` to prevent the target module's dependencies from being swizzled.
  35. *
  36. * @param {!Module} targetModule - The module that is being swizzled.
  37. * @param {!function} nodeRequire - The original `require()` method for the target module.
  38. * @param {!string} filepath - The value passed to `require()`.
  39. * @return {!Module} The requested module dependency.
  40. */
  41. function requireProxy(targetModule, nodeRequire, filepath) {
  42. restoreWrapper();
  43. targetModule.require = nodeRequire;
  44. return nodeRequire.call(targetModule, filepath);
  45. }
  46. /**
  47. * Wrapper for `require()` to swizzle the target module's dependencies, using the same settings as
  48. * the target module.
  49. *
  50. * @param {!Module} targetModule - The module that is being swizzled.
  51. * @param {!Object} opts - The Requizzle options object.
  52. * @param {!string} filepath - The value passed to `require()`.
  53. * @return {!Module} The requested module dependency.
  54. */
  55. function infectProxy(targetModule, cache, opts, filepath) {
  56. let moduleExports;
  57. // loaded here to avoid circular dependencies
  58. const Requizzle = require('./requizzle');
  59. let requizzle;
  60. opts = _.clone(opts);
  61. opts.parent = targetModule;
  62. requizzle = new Requizzle(opts, cache);
  63. moduleExports = requizzle.requizzle(filepath);
  64. return moduleExports;
  65. }
  66. exports.load = function load(targetPath, parentModule, wrapper, cache, options) {
  67. let nodeRequire;
  68. let targetModule;
  69. // Handle circular requires, and avoid reloading modules unnecessarily
  70. if (cache.module[targetPath]) {
  71. return cache.module[targetPath];
  72. }
  73. targetModule = createModule(targetPath, parentModule, cache.module);
  74. nodeRequire = targetModule.require;
  75. if (options.infect) {
  76. targetModule.require = (filepath) => infectProxy(targetModule, cache, options, filepath);
  77. } else {
  78. targetModule.require = (filepath) => requireProxy(targetModule, nodeRequire, filepath);
  79. }
  80. // update the wrapper before we load the target module
  81. replaceWrapper(wrapper);
  82. targetModule.load(targetModule.id);
  83. // make sure the wrapper is restored even if the target module doesn't load any dependencies
  84. restoreWrapper();
  85. return targetModule;
  86. };
  87. /**
  88. * Check whether the entire module includes a `'use strict'` declaration.
  89. *
  90. * @param {string} src - The source file to check.
  91. * @return {boolean} Set to `true` if the module includes a `use strict` declaration.
  92. */
  93. function detectStrictMode(src) {
  94. return /^\s*(?:["']use strict["'])[ \t]*(?:[\r\n]|;)/g.test(src);
  95. }
  96. function loadSource(targetPath, sourceCache) {
  97. if (sourceCache[targetPath] === undefined) {
  98. sourceCache[targetPath] = fs.readFileSync(targetPath, 'utf8');
  99. }
  100. return sourceCache[targetPath];
  101. }
  102. exports.createWrapper = function createWrapper(targetPath, parentModule, cache, options) {
  103. let src;
  104. const wrapperObject = {
  105. before: [],
  106. after: [],
  107. };
  108. function add(wrapperFunctions, opts) {
  109. const params = [targetPath, parentModule, opts];
  110. ['before', 'after'].forEach((item) => {
  111. const result = wrapperFunctions[item].apply(null, params);
  112. if (result) {
  113. wrapperObject[item].push(result);
  114. }
  115. });
  116. }
  117. // Preserve the module's `use strict` declaration if present
  118. src = loadSource(targetPath, cache.source);
  119. if (detectStrictMode(src) === true) {
  120. add(requizzleWrappers.strict);
  121. }
  122. if (options.requirePaths) {
  123. add(requizzleWrappers.requirePaths, options.requirePaths);
  124. }
  125. if (options.extras) {
  126. add(requizzleWrappers.extras, options.extras);
  127. }
  128. return wrapperObject;
  129. };