File tree Expand file tree Collapse file tree 2 files changed +8
-8
lines changed Expand file tree Collapse file tree 2 files changed +8
-8
lines changed Original file line number Diff line number Diff line change 11import 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}
Original file line number Diff line number Diff 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} ) ;
You can’t perform that action at this time.
0 commit comments