resolvePath.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = resolvePath;
  4. var _path = _interopRequireDefault(require("path"));
  5. var _log = require("./log");
  6. var _mapToRelative = _interopRequireDefault(require("./mapToRelative"));
  7. var _normalizeOptions = _interopRequireDefault(require("./normalizeOptions"));
  8. var _utils = require("./utils");
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. function getRelativePath(sourcePath, currentFile, absFileInRoot, opts) {
  11. const realSourceFileExtension = _path.default.extname(absFileInRoot);
  12. const sourceFileExtension = _path.default.extname(sourcePath);
  13. let relativePath = (0, _mapToRelative.default)(opts.cwd, currentFile, absFileInRoot);
  14. if (realSourceFileExtension !== sourceFileExtension) {
  15. relativePath = (0, _utils.replaceExtension)(relativePath, opts);
  16. }
  17. return (0, _utils.toLocalPath)((0, _utils.toPosixPath)(relativePath));
  18. }
  19. function findPathInRoots(sourcePath, {
  20. extensions,
  21. root
  22. }) {
  23. // Search the source path inside every custom root directory
  24. let resolvedSourceFile;
  25. root.some(basedir => {
  26. resolvedSourceFile = (0, _utils.nodeResolvePath)(`./${sourcePath}`, basedir, extensions);
  27. return resolvedSourceFile !== null;
  28. });
  29. return resolvedSourceFile;
  30. }
  31. function resolvePathFromRootConfig(sourcePath, currentFile, opts) {
  32. const absFileInRoot = findPathInRoots(sourcePath, opts);
  33. if (!absFileInRoot) {
  34. return null;
  35. }
  36. return getRelativePath(sourcePath, currentFile, absFileInRoot, opts);
  37. }
  38. function checkIfPackageExists(modulePath, currentFile, extensions, loglevel) {
  39. const resolvedPath = (0, _utils.nodeResolvePath)(modulePath, currentFile, extensions);
  40. if (resolvedPath === null && loglevel !== 'silent') {
  41. (0, _log.warn)(`Could not resolve "${modulePath}" in file ${currentFile}.`);
  42. }
  43. }
  44. function resolvePathFromAliasConfig(sourcePath, currentFile, opts) {
  45. let aliasedSourceFile;
  46. opts.alias.find(([regExp, substitute]) => {
  47. const execResult = regExp.exec(sourcePath);
  48. if (execResult === null) {
  49. return false;
  50. }
  51. aliasedSourceFile = substitute(execResult);
  52. return true;
  53. });
  54. if (!aliasedSourceFile) {
  55. return null;
  56. }
  57. if ((0, _utils.isRelativePath)(aliasedSourceFile)) {
  58. return (0, _utils.toLocalPath)((0, _utils.toPosixPath)((0, _mapToRelative.default)(opts.cwd, currentFile, aliasedSourceFile)));
  59. }
  60. if (process.env.NODE_ENV !== 'production') {
  61. checkIfPackageExists(aliasedSourceFile, currentFile, opts.extensions, opts.loglevel);
  62. }
  63. return aliasedSourceFile;
  64. }
  65. const resolvers = [resolvePathFromAliasConfig, resolvePathFromRootConfig];
  66. function resolvePath(sourcePath, currentFile, opts) {
  67. if ((0, _utils.isRelativePath)(sourcePath)) {
  68. return sourcePath;
  69. }
  70. const normalizedOpts = (0, _normalizeOptions.default)(currentFile, opts); // File param is a relative path from the environment current working directory
  71. // (not from cwd param)
  72. const absoluteCurrentFile = _path.default.resolve(currentFile);
  73. let resolvedPath = null;
  74. resolvers.some(resolver => {
  75. resolvedPath = resolver(sourcePath, absoluteCurrentFile, normalizedOpts);
  76. return resolvedPath !== null;
  77. });
  78. return resolvedPath;
  79. }