Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

192 рядки
6.2 KiB

4 роки тому
  1. var isCore = require('is-core-module');
  2. var fs = require('fs');
  3. var path = require('path');
  4. var caller = require('./caller');
  5. var nodeModulesPaths = require('./node-modules-paths');
  6. var normalizeOptions = require('./normalize-options');
  7. var realpathFS = fs.realpathSync && typeof fs.realpathSync.native === 'function' ? fs.realpathSync.native : fs.realpathSync;
  8. var defaultIsFile = function isFile(file) {
  9. try {
  10. var stat = fs.statSync(file);
  11. } catch (e) {
  12. if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false;
  13. throw e;
  14. }
  15. return stat.isFile() || stat.isFIFO();
  16. };
  17. var defaultIsDir = function isDirectory(dir) {
  18. try {
  19. var stat = fs.statSync(dir);
  20. } catch (e) {
  21. if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false;
  22. throw e;
  23. }
  24. return stat.isDirectory();
  25. };
  26. var defaultRealpathSync = function realpathSync(x) {
  27. try {
  28. return realpathFS(x);
  29. } catch (realpathErr) {
  30. if (realpathErr.code !== 'ENOENT') {
  31. throw realpathErr;
  32. }
  33. }
  34. return x;
  35. };
  36. var maybeRealpathSync = function maybeRealpathSync(realpathSync, x, opts) {
  37. if (opts && opts.preserveSymlinks === false) {
  38. return realpathSync(x);
  39. }
  40. return x;
  41. };
  42. var getPackageCandidates = function getPackageCandidates(x, start, opts) {
  43. var dirs = nodeModulesPaths(start, opts, x);
  44. for (var i = 0; i < dirs.length; i++) {
  45. dirs[i] = path.join(dirs[i], x);
  46. }
  47. return dirs;
  48. };
  49. module.exports = function resolveSync(x, options) {
  50. if (typeof x !== 'string') {
  51. throw new TypeError('Path must be a string.');
  52. }
  53. var opts = normalizeOptions(x, options);
  54. var isFile = opts.isFile || defaultIsFile;
  55. var readFileSync = opts.readFileSync || fs.readFileSync;
  56. var isDirectory = opts.isDirectory || defaultIsDir;
  57. var realpathSync = opts.realpathSync || defaultRealpathSync;
  58. var packageIterator = opts.packageIterator;
  59. var extensions = opts.extensions || ['.js'];
  60. var includeCoreModules = opts.includeCoreModules !== false;
  61. var basedir = opts.basedir || path.dirname(caller());
  62. var parent = opts.filename || basedir;
  63. opts.paths = opts.paths || [];
  64. // ensure that `basedir` is an absolute path at this point, resolving against the process' current working directory
  65. var absoluteStart = maybeRealpathSync(realpathSync, path.resolve(basedir), opts);
  66. if ((/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/).test(x)) {
  67. var res = path.resolve(absoluteStart, x);
  68. if (x === '.' || x === '..' || x.slice(-1) === '/') res += '/';
  69. var m = loadAsFileSync(res) || loadAsDirectorySync(res);
  70. if (m) return maybeRealpathSync(realpathSync, m, opts);
  71. } else if (includeCoreModules && isCore(x)) {
  72. return x;
  73. } else {
  74. var n = loadNodeModulesSync(x, absoluteStart);
  75. if (n) return maybeRealpathSync(realpathSync, n, opts);
  76. }
  77. var err = new Error("Cannot find module '" + x + "' from '" + parent + "'");
  78. err.code = 'MODULE_NOT_FOUND';
  79. throw err;
  80. function loadAsFileSync(x) {
  81. var pkg = loadpkg(path.dirname(x));
  82. if (pkg && pkg.dir && pkg.pkg && opts.pathFilter) {
  83. var rfile = path.relative(pkg.dir, x);
  84. var r = opts.pathFilter(pkg.pkg, x, rfile);
  85. if (r) {
  86. x = path.resolve(pkg.dir, r); // eslint-disable-line no-param-reassign
  87. }
  88. }
  89. if (isFile(x)) {
  90. return x;
  91. }
  92. for (var i = 0; i < extensions.length; i++) {
  93. var file = x + extensions[i];
  94. if (isFile(file)) {
  95. return file;
  96. }
  97. }
  98. }
  99. function loadpkg(dir) {
  100. if (dir === '' || dir === '/') return;
  101. if (process.platform === 'win32' && (/^\w:[/\\]*$/).test(dir)) {
  102. return;
  103. }
  104. if ((/[/\\]node_modules[/\\]*$/).test(dir)) return;
  105. var pkgfile = path.join(maybeRealpathSync(realpathSync, dir, opts), 'package.json');
  106. if (!isFile(pkgfile)) {
  107. return loadpkg(path.dirname(dir));
  108. }
  109. var body = readFileSync(pkgfile);
  110. try {
  111. var pkg = JSON.parse(body);
  112. } catch (jsonErr) {}
  113. if (pkg && opts.packageFilter) {
  114. // v2 will pass pkgfile
  115. pkg = opts.packageFilter(pkg, /*pkgfile,*/ dir); // eslint-disable-line spaced-comment
  116. }
  117. return { pkg: pkg, dir: dir };
  118. }
  119. function loadAsDirectorySync(x) {
  120. var pkgfile = path.join(maybeRealpathSync(realpathSync, x, opts), '/package.json');
  121. if (isFile(pkgfile)) {
  122. try {
  123. var body = readFileSync(pkgfile, 'UTF8');
  124. var pkg = JSON.parse(body);
  125. } catch (e) {}
  126. if (pkg && opts.packageFilter) {
  127. // v2 will pass pkgfile
  128. pkg = opts.packageFilter(pkg, /*pkgfile,*/ x); // eslint-disable-line spaced-comment
  129. }
  130. if (pkg && pkg.main) {
  131. if (typeof pkg.main !== 'string') {
  132. var mainError = new TypeError('package “' + pkg.name + '” `main` must be a string');
  133. mainError.code = 'INVALID_PACKAGE_MAIN';
  134. throw mainError;
  135. }
  136. if (pkg.main === '.' || pkg.main === './') {
  137. pkg.main = 'index';
  138. }
  139. try {
  140. var m = loadAsFileSync(path.resolve(x, pkg.main));
  141. if (m) return m;
  142. var n = loadAsDirectorySync(path.resolve(x, pkg.main));
  143. if (n) return n;
  144. } catch (e) {}
  145. }
  146. }
  147. return loadAsFileSync(path.join(x, '/index'));
  148. }
  149. function loadNodeModulesSync(x, start) {
  150. var thunk = function () { return getPackageCandidates(x, start, opts); };
  151. var dirs = packageIterator ? packageIterator(x, start, thunk, opts) : thunk();
  152. for (var i = 0; i < dirs.length; i++) {
  153. var dir = dirs[i];
  154. if (isDirectory(path.dirname(dir))) {
  155. var m = loadAsFileSync(dir);
  156. if (m) return m;
  157. var n = loadAsDirectorySync(dir);
  158. if (n) return n;
  159. }
  160. }
  161. }
  162. };