Skip to content

Commit 72d4934

Browse files
committed
fix: 修正路径相对计算
1 parent b02f50a commit 72d4934

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

src/utils/path.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import path from 'node:path';
22

3-
export function toRelative(to: string, from?: string) {
4-
if (!from) return to;
5-
if (!path.isAbsolute(to)) return to;
3+
export function toRelative(toFile: string, fromFile?: string) {
4+
if (!fromFile) return toFile;
5+
if (!path.isAbsolute(toFile)) return toFile;
66

7-
const relative = path.relative(from, to);
7+
const relative = path.relative(path.dirname(fromFile), toFile);
88
return relative.startsWith('.') ? relative : `./${relative}`;
99
}

test/utils/path.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ test('toRelative', () => {
44
expect(toRelative('./a/b/c')).toBe('./a/b/c');
55
expect(toRelative('/a/b/c')).toBe('/a/b/c');
66
expect(toRelative('./a/b/c', '/a')).toBe('./a/b/c');
7-
expect(toRelative('/a/b/c', '/a')).toBe('./b/c');
8-
expect(toRelative('/a/b/c', '/a/b')).toBe('./c');
9-
expect(toRelative('/a/b/c', '/a/b/e')).toBe('../c');
10-
expect(toRelative('/a/b/c', '/a/d/e')).toBe('../../b/c');
7+
expect(toRelative('/a/b/c', '/a')).toBe('./a/b/c');
8+
expect(toRelative('/a/b/c', '/a/b')).toBe('./b/c');
9+
expect(toRelative('/a/b/c', '/a/b/e')).toBe('./c');
10+
expect(toRelative('/a/b/c', '/a/d/e')).toBe('../b/c');
1111
});

0 commit comments

Comments
 (0)