node_modules_paths.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = nodeModulesPaths;
  6. var _path;
  7. function _load_path() {
  8. return _path = _interopRequireDefault(require('path'));
  9. }
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. /**
  12. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  13. *
  14. * This source code is licensed under the MIT license found in the
  15. * LICENSE file in the root directory of this source tree.
  16. *
  17. * Adapted from: https://github.com/substack/node-resolve
  18. *
  19. *
  20. */
  21. function nodeModulesPaths(basedir, options) {
  22. const modules = options && options.moduleDirectory ? [].concat(options.moduleDirectory) : ['node_modules'];
  23. // ensure that `basedir` is an absolute path at this point,
  24. // resolving against the process' current working directory
  25. const basedirAbs = (_path || _load_path()).default.resolve(basedir);
  26. let prefix = '/';
  27. if (/^([A-Za-z]:)/.test(basedirAbs)) {
  28. prefix = '';
  29. } else if (/^\\\\/.test(basedirAbs)) {
  30. prefix = '\\\\';
  31. }
  32. const paths = [basedirAbs];
  33. let parsed = (_path || _load_path()).default.parse(basedirAbs);
  34. while (parsed.dir !== paths[paths.length - 1]) {
  35. paths.push(parsed.dir);
  36. parsed = (_path || _load_path()).default.parse(parsed.dir);
  37. }
  38. const dirs = paths.reduce((dirs, aPath) => {
  39. return dirs.concat(modules.map(moduleDir => {
  40. return (_path || _load_path()).default.isAbsolute(moduleDir) ? aPath === basedirAbs ? moduleDir : '' : (_path || _load_path()).default.join(prefix, aPath, moduleDir);
  41. }));
  42. }, []).filter(dir => dir !== '');
  43. return options.paths ? dirs.concat(options.paths) : dirs;
  44. }