sync.js 4.6 KB

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