From a48d6128d541fdcce86127f4b01eb1832d35f290 Mon Sep 17 00:00:00 2001 From: julien huang Date: Wed, 27 Dec 2023 14:22:56 +0100 Subject: [PATCH 1/7] fix: resolve alias ending with / or \ --- src/utils.ts | 4 ++-- test/utils.spec.ts | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index f3f160e..cf2605d 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -42,8 +42,8 @@ export function normalizeAliases(_aliases: Record) { export function resolveAlias(path: string, aliases: Record) { const _path = normalizeWindowsPath(path); aliases = normalizeAliases(aliases); - for (const alias in aliases) { - if (_path.startsWith(alias) && pathSeparators.has(_path[alias.length])) { + for (const alias in aliases) { + if (_path.startsWith(alias) && pathSeparators.has(_path[alias[alias.length - 1] === '/' ? alias.length - 1 : alias.length])) { return join(aliases[alias], _path.slice(alias.length)); } } diff --git a/test/utils.spec.ts b/test/utils.spec.ts index 4845de0..5dbacc9 100644 --- a/test/utils.spec.ts +++ b/test/utils.spec.ts @@ -9,6 +9,7 @@ describe("alias", () => { "@": "/root", bingpot: "@/bingpot/index.ts", test: "@bingpot/index.ts", + "~~/": "C:/nitro" }; const aliases = normalizeAliases(_aliases); @@ -20,6 +21,7 @@ describe("alias", () => { "@foo/bar/utils": "@foo/bar/dist/utils.mjs", "bingpot": "/root/bingpot/index.ts", "test": "@bingpot/index.ts", + "~~/": "C:/nitro", } `); }); @@ -43,6 +45,9 @@ describe("alias", () => { expect(resolveAlias("foo/bar.js", aliases)).toBe("foo/bar.js"); expect(resolveAlias("./bar.js", aliases)).toBe("./bar.js"); }); + it('respect ending with /', () => { + expect(resolveAlias("~~/user-module/mod", aliases)).toBe('C:/nitro/user-module/mod') + }) }); }); From dcf4f2d1e04a69a68d5c887aca45996601d6f056 Mon Sep 17 00:00:00 2001 From: julien huang Date: Wed, 27 Dec 2023 14:33:22 +0100 Subject: [PATCH 2/7] fix: also allow \ --- src/utils.ts | 5 +++-- test/utils.spec.ts | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index cf2605d..06345e1 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,7 +1,8 @@ import { join } from "./path"; import { normalizeWindowsPath } from "./_internal"; -const pathSeparators = new Set(["/", "\\", undefined]); +const SEPARATORS = ["/", "\\"]; +const pathSeparators = new Set([...SEPARATORS, undefined]); const normalizedAliasSymbol = Symbol.for("pathe:normalizedAlias"); @@ -43,7 +44,7 @@ export function resolveAlias(path: string, aliases: Record) { const _path = normalizeWindowsPath(path); aliases = normalizeAliases(aliases); for (const alias in aliases) { - if (_path.startsWith(alias) && pathSeparators.has(_path[alias[alias.length - 1] === '/' ? alias.length - 1 : alias.length])) { + if (_path.startsWith(alias) && pathSeparators.has(_path[SEPARATORS.includes(alias[alias.length - 1]) ? alias.length - 1 : alias.length])) { return join(aliases[alias], _path.slice(alias.length)); } } diff --git a/test/utils.spec.ts b/test/utils.spec.ts index 5dbacc9..6a3eb18 100644 --- a/test/utils.spec.ts +++ b/test/utils.spec.ts @@ -9,7 +9,7 @@ describe("alias", () => { "@": "/root", bingpot: "@/bingpot/index.ts", test: "@bingpot/index.ts", - "~~/": "C:/nitro" + "~~/": "C:/nitro", }; const aliases = normalizeAliases(_aliases); @@ -47,7 +47,7 @@ describe("alias", () => { }); it('respect ending with /', () => { expect(resolveAlias("~~/user-module/mod", aliases)).toBe('C:/nitro/user-module/mod') - }) + }); }); }); From 0b244c87e613835c47212d8213ba4e0271e4a9a2 Mon Sep 17 00:00:00 2001 From: julien huang Date: Wed, 27 Dec 2023 20:34:12 +0100 Subject: [PATCH 3/7] chore: prettier --- src/utils.ts | 13 +++++++++++-- test/utils.spec.ts | 6 ++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 06345e1..57579a2 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -43,8 +43,17 @@ export function normalizeAliases(_aliases: Record) { export function resolveAlias(path: string, aliases: Record) { const _path = normalizeWindowsPath(path); aliases = normalizeAliases(aliases); - for (const alias in aliases) { - if (_path.startsWith(alias) && pathSeparators.has(_path[SEPARATORS.includes(alias[alias.length - 1]) ? alias.length - 1 : alias.length])) { + for (const alias in aliases) { + if ( + _path.startsWith(alias) && + pathSeparators.has( + _path[ + SEPARATORS.includes(alias[alias.length - 1]) + ? alias.length - 1 + : alias.length + ], + ) + ) { return join(aliases[alias], _path.slice(alias.length)); } } diff --git a/test/utils.spec.ts b/test/utils.spec.ts index 6a3eb18..58f61fe 100644 --- a/test/utils.spec.ts +++ b/test/utils.spec.ts @@ -45,8 +45,10 @@ describe("alias", () => { expect(resolveAlias("foo/bar.js", aliases)).toBe("foo/bar.js"); expect(resolveAlias("./bar.js", aliases)).toBe("./bar.js"); }); - it('respect ending with /', () => { - expect(resolveAlias("~~/user-module/mod", aliases)).toBe('C:/nitro/user-module/mod') + it("respect ending with /", () => { + expect(resolveAlias("~~/user-module/mod", aliases)).toBe( + "C:/nitro/user-module/mod", + ); }); }); }); From 680e2df0ea29058bac2f0834090d5081973ae319 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 10 Jan 2024 13:42:09 +0100 Subject: [PATCH 4/7] update tests --- test/utils.spec.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/test/utils.spec.ts b/test/utils.spec.ts index 58f61fe..ebf5e4f 100644 --- a/test/utils.spec.ts +++ b/test/utils.spec.ts @@ -9,7 +9,8 @@ describe("alias", () => { "@": "/root", bingpot: "@/bingpot/index.ts", test: "@bingpot/index.ts", - "~~/": "C:/nitro", + "~/": "/src", + "~win/": "C:/src", }; const aliases = normalizeAliases(_aliases); @@ -21,7 +22,8 @@ describe("alias", () => { "@foo/bar/utils": "@foo/bar/dist/utils.mjs", "bingpot": "/root/bingpot/index.ts", "test": "@bingpot/index.ts", - "~~/": "C:/nitro", + "~/": "/src", + "~win/": "C:/src", } `); }); @@ -46,9 +48,8 @@ describe("alias", () => { expect(resolveAlias("./bar.js", aliases)).toBe("./bar.js"); }); it("respect ending with /", () => { - expect(resolveAlias("~~/user-module/mod", aliases)).toBe( - "C:/nitro/user-module/mod", - ); + expect(resolveAlias("~/foo/bar", aliases)).toBe("/src/foo/bar"); + expect(resolveAlias("~win/foo/bar", aliases)).toBe("C:/src/foo/bar"); }); }); }); From 87baf365a80a0af056dd7c2bcdf26ca42b588acd Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 10 Jan 2024 13:57:05 +0100 Subject: [PATCH 5/7] update tests --- test/utils.spec.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/utils.spec.ts b/test/utils.spec.ts index ebf5e4f..23fad40 100644 --- a/test/utils.spec.ts +++ b/test/utils.spec.ts @@ -9,7 +9,9 @@ describe("alias", () => { "@": "/root", bingpot: "@/bingpot/index.ts", test: "@bingpot/index.ts", + "~": "/src/index.js", "~/": "/src", + "~win": "C:/src/index.js", "~win/": "C:/src", }; const aliases = normalizeAliases(_aliases); @@ -22,7 +24,9 @@ describe("alias", () => { "@foo/bar/utils": "@foo/bar/dist/utils.mjs", "bingpot": "/root/bingpot/index.ts", "test": "@bingpot/index.ts", + "~": "/src/index.js", "~/": "/src", + "~win": "C:/src/index.js", "~win/": "C:/src", } `); From dce2e2ac814cf5d7f9912bf5db7991f50ff17963 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 10 Jan 2024 14:06:18 +0100 Subject: [PATCH 6/7] update test --- test/utils.spec.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/utils.spec.ts b/test/utils.spec.ts index 23fad40..71a9fd9 100644 --- a/test/utils.spec.ts +++ b/test/utils.spec.ts @@ -9,9 +9,9 @@ describe("alias", () => { "@": "/root", bingpot: "@/bingpot/index.ts", test: "@bingpot/index.ts", - "~": "/src/index.js", + "~": "/root/index.js", "~/": "/src", - "~win": "C:/src/index.js", + "~win": "C:/root/index.js", "~win/": "C:/src", }; const aliases = normalizeAliases(_aliases); @@ -24,9 +24,9 @@ describe("alias", () => { "@foo/bar/utils": "@foo/bar/dist/utils.mjs", "bingpot": "/root/bingpot/index.ts", "test": "@bingpot/index.ts", - "~": "/src/index.js", + "~": "/root/index.js", "~/": "/src", - "~win": "C:/src/index.js", + "~win": "C:/root/index.js", "~win/": "C:/src", } `); From 9aeeee241b692017361d76af9a1963eafb33dfee Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 10 Jan 2024 14:22:08 +0100 Subject: [PATCH 7/7] update impl --- src/utils.ts | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 57579a2..887ca5f 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,8 +1,7 @@ import { join } from "./path"; import { normalizeWindowsPath } from "./_internal"; -const SEPARATORS = ["/", "\\"]; -const pathSeparators = new Set([...SEPARATORS, undefined]); +const pathSeparators = new Set(["/", "\\", undefined]); const normalizedAliasSymbol = Symbol.for("pathe:normalizedAlias"); @@ -43,18 +42,16 @@ export function normalizeAliases(_aliases: Record) { export function resolveAlias(path: string, aliases: Record) { const _path = normalizeWindowsPath(path); aliases = normalizeAliases(aliases); - for (const alias in aliases) { - if ( - _path.startsWith(alias) && - pathSeparators.has( - _path[ - SEPARATORS.includes(alias[alias.length - 1]) - ? alias.length - 1 - : alias.length - ], - ) - ) { - return join(aliases[alias], _path.slice(alias.length)); + for (const [alias, to] of Object.entries(aliases)) { + if (!_path.startsWith(alias)) { + continue; + } + + // Strip trailing slash from alias for check + const _alias = hasTrailingSlash(alias) ? alias.slice(0, -1) : alias; + + if (hasTrailingSlash(_path[_alias.length])) { + return join(to, _path.slice(alias.length)); } } return _path; @@ -71,3 +68,9 @@ export function filename(path: string) { function _compareAliases(a: string, b: string) { return b.split("/").length - a.split("/").length; } + +// Returns true if path ends with a slash or **is empty** +function hasTrailingSlash(path = "/") { + const lastChar = path[path.length - 1]; + return lastChar === "/" || lastChar === "\\"; +}