Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(resolveAlias): handle aliases ending with trailing slash #155

Merged
merged 8 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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");

Expand Down Expand Up @@ -43,7 +44,16 @@ export function resolveAlias(path: string, aliases: Record<string, string>) {
const _path = normalizeWindowsPath(path);
aliases = normalizeAliases(aliases);
for (const alias in aliases) {
if (_path.startsWith(alias) && pathSeparators.has(_path[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));
}
}
Expand Down
7 changes: 7 additions & 0 deletions test/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe("alias", () => {
"@": "/root",
bingpot: "@/bingpot/index.ts",
test: "@bingpot/index.ts",
"~~/": "C:/nitro",
};
const aliases = normalizeAliases(_aliases);

Expand All @@ -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",
}
`);
});
Expand All @@ -43,6 +45,11 @@ 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",
);
});
});
});

Expand Down