utils.cjs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. 'use strict';
  2. const index = require('./shared/pathe.1f0a373c.cjs');
  3. const pathSeparators = /* @__PURE__ */ new Set(["/", "\\", void 0]);
  4. const normalizedAliasSymbol = Symbol.for("pathe:normalizedAlias");
  5. function normalizeAliases(_aliases) {
  6. if (_aliases[normalizedAliasSymbol]) {
  7. return _aliases;
  8. }
  9. const aliases = Object.fromEntries(
  10. Object.entries(_aliases).sort(([a], [b]) => _compareAliases(a, b))
  11. );
  12. for (const key in aliases) {
  13. for (const alias in aliases) {
  14. if (alias === key || key.startsWith(alias)) {
  15. continue;
  16. }
  17. if (aliases[key].startsWith(alias) && pathSeparators.has(aliases[key][alias.length])) {
  18. aliases[key] = aliases[alias] + aliases[key].slice(alias.length);
  19. }
  20. }
  21. }
  22. Object.defineProperty(aliases, normalizedAliasSymbol, {
  23. value: true,
  24. enumerable: false
  25. });
  26. return aliases;
  27. }
  28. function resolveAlias(path, aliases) {
  29. const _path = index.normalizeWindowsPath(path);
  30. aliases = normalizeAliases(aliases);
  31. for (const [alias, to] of Object.entries(aliases)) {
  32. if (!_path.startsWith(alias)) {
  33. continue;
  34. }
  35. const _alias = hasTrailingSlash(alias) ? alias.slice(0, -1) : alias;
  36. if (hasTrailingSlash(_path[_alias.length])) {
  37. return index.join(to, _path.slice(alias.length));
  38. }
  39. }
  40. return _path;
  41. }
  42. const FILENAME_RE = /(^|[/\\])([^/\\]+?)(?=(\.[^.]+)?$)/;
  43. function filename(path) {
  44. return path.match(FILENAME_RE)?.[2];
  45. }
  46. function _compareAliases(a, b) {
  47. return b.split("/").length - a.split("/").length;
  48. }
  49. function hasTrailingSlash(path = "/") {
  50. const lastChar = path[path.length - 1];
  51. return lastChar === "/" || lastChar === "\\";
  52. }
  53. exports.filename = filename;
  54. exports.normalizeAliases = normalizeAliases;
  55. exports.resolveAlias = resolveAlias;