patchRequire.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. "use strict";
  2. function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports["default"] = patchRequire;
  7. var path = _interopRequireWildcard(require("path"));
  8. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
  9. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  10. var isWin32 = process.platform === 'win32';
  11. var correctPath = isWin32 ? require('./correctPath').correctPath : function (p) {
  12. return p;
  13. };
  14. function stripBOM(content) {
  15. if (content.charCodeAt(0) === 0xFEFF) {
  16. content = content.slice(1);
  17. }
  18. return content;
  19. }
  20. function patchRequire(vol) {
  21. var unixifyPaths = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  22. var Module = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : require('module');
  23. if (isWin32 && unixifyPaths) {
  24. var original = vol;
  25. vol = {
  26. readFileSync: function readFileSync(path, options) {
  27. return original.readFileSync(correctPath(path), options);
  28. },
  29. realpathSync: function realpathSync(path) {
  30. return original.realpathSync(correctPath(path));
  31. },
  32. statSync: function statSync(path) {
  33. return original.statSync(correctPath(path));
  34. }
  35. };
  36. }
  37. function internalModuleReadFile(path) {
  38. try {
  39. return vol.readFileSync(path, 'utf8');
  40. } catch (err) {}
  41. }
  42. function internalModuleStat(filename) {
  43. try {
  44. return vol.statSync(filename).isDirectory() ? 1 : 0;
  45. } catch (err) {
  46. return -2;
  47. }
  48. }
  49. function stat(filename) {
  50. filename = path._makeLong(filename);
  51. var cache = stat.cache;
  52. if (cache !== null) {
  53. var _result = cache.get(filename);
  54. if (_result !== undefined) return _result;
  55. }
  56. var result = internalModuleStat(filename);
  57. if (cache !== null) cache.set(filename, result);
  58. return result;
  59. }
  60. stat.cache = null;
  61. var preserveSymlinks = false;
  62. function toRealPath(requestPath) {
  63. return vol.realpathSync(requestPath);
  64. }
  65. var packageMainCache = Object.create(null);
  66. function readPackage(requestPath) {
  67. var entry = packageMainCache[requestPath];
  68. if (entry) return entry;
  69. var jsonPath = path.resolve(requestPath, 'package.json');
  70. var json = internalModuleReadFile(path._makeLong(jsonPath));
  71. if (json === undefined) {
  72. return false;
  73. }
  74. var pkg;
  75. try {
  76. pkg = packageMainCache[requestPath] = JSON.parse(json).main;
  77. } catch (e) {
  78. e.path = jsonPath;
  79. e.message = 'Error parsing ' + jsonPath + ': ' + e.message;
  80. throw e;
  81. }
  82. return pkg;
  83. }
  84. function tryFile(requestPath, isMain) {
  85. var rc = stat(requestPath);
  86. if (preserveSymlinks && !isMain) {
  87. return rc === 0 && path.resolve(requestPath);
  88. }
  89. return rc === 0 && toRealPath(requestPath);
  90. }
  91. function tryExtensions(p, exts, isMain) {
  92. for (var i = 0; i < exts.length; i++) {
  93. var filename = tryFile(p + exts[i], isMain);
  94. if (filename) {
  95. return filename;
  96. }
  97. }
  98. return false;
  99. }
  100. function tryPackage(requestPath, exts, isMain) {
  101. var pkg = readPackage(requestPath);
  102. if (!pkg) return false;
  103. var filename = path.resolve(requestPath, pkg);
  104. return tryFile(filename, isMain) || tryExtensions(filename, exts, isMain) || tryExtensions(path.resolve(filename, 'index'), exts, isMain);
  105. }
  106. Module._extensions['.js'] = function (module, filename) {
  107. var content = vol.readFileSync(filename, 'utf8');
  108. module._compile(stripBOM(content), filename);
  109. };
  110. Module._extensions['.json'] = function (module, filename) {
  111. var content = vol.readFileSync(filename, 'utf8');
  112. try {
  113. module.exports = JSON.parse(stripBOM(content));
  114. } catch (err) {
  115. err.message = filename + ': ' + err.message;
  116. throw err;
  117. }
  118. };
  119. var warned = true;
  120. Module._findPath = function (request, paths, isMain) {
  121. if (path.isAbsolute(request)) {
  122. paths = [''];
  123. } else if (!paths || paths.length === 0) {
  124. return false;
  125. }
  126. var cacheKey = request + '\x00' + (paths.length === 1 ? paths[0] : paths.join('\x00'));
  127. var entry = Module._pathCache[cacheKey];
  128. if (entry) return entry;
  129. var exts;
  130. var trailingSlash = request.length > 0 && request.charCodeAt(request.length - 1) === 47;
  131. for (var i = 0; i < paths.length; i++) {
  132. var curPath = paths[i];
  133. if (curPath && stat(curPath) < 1) continue;
  134. var basePath = correctPath(path.resolve(curPath, request));
  135. var filename;
  136. var rc = stat(basePath);
  137. if (!trailingSlash) {
  138. if (rc === 0) {
  139. if (preserveSymlinks && !isMain) {
  140. filename = path.resolve(basePath);
  141. } else {
  142. filename = toRealPath(basePath);
  143. }
  144. } else if (rc === 1) {
  145. if (exts === undefined) exts = Object.keys(Module._extensions);
  146. filename = tryPackage(basePath, exts, isMain);
  147. }
  148. if (!filename) {
  149. if (exts === undefined) exts = Object.keys(Module._extensions);
  150. filename = tryExtensions(basePath, exts, isMain);
  151. }
  152. }
  153. if (!filename && rc === 1) {
  154. if (exts === undefined) exts = Object.keys(Module._extensions);
  155. filename = tryPackage(basePath, exts, isMain);
  156. }
  157. if (!filename && rc === 1) {
  158. if (exts === undefined) exts = Object.keys(Module._extensions);
  159. filename = tryExtensions(path.resolve(basePath, 'index'), exts, isMain);
  160. }
  161. if (filename) {
  162. if (request === '.' && i > 0) {
  163. if (!warned) {
  164. warned = true;
  165. process.emitWarning('warning: require(\'.\') resolved outside the package ' + 'directory. This functionality is deprecated and will be removed ' + 'soon.', 'DeprecationWarning', 'DEP0019');
  166. }
  167. }
  168. Module._pathCache[cacheKey] = filename;
  169. return filename;
  170. }
  171. }
  172. return false;
  173. };
  174. }