@@ -2,7 +2,15 @@ import assert from 'node:assert';
22import Path from 'node:path' ;
33import { pathToFileURL } from 'node:url' ;
44
5- import { pathWindowsDriveLetterToUpper , regExpWindowsPathDriveLetter , toFilePathOrHref } from './fileUrl.mjs' ;
5+ import {
6+ isFileURL ,
7+ isWindows ,
8+ isWindowsFileUrl ,
9+ isWindowsPathnameWithDriveLatter ,
10+ pathWindowsDriveLetterToUpper ,
11+ regExpWindowsPathDriveLetter ,
12+ toFilePathOrHref ,
13+ } from './fileUrl.mjs' ;
614import {
715 addTrailingSlash ,
816 isUrlLike ,
@@ -12,8 +20,6 @@ import {
1220 urlToUrlRelative ,
1321} from './url.mjs' ;
1422
15- export const isWindows = process . platform === 'win32' ;
16-
1723const isWindowsPathRegEx = regExpWindowsPathDriveLetter ;
1824const isWindowsPathname = regExpWindowsPath ;
1925
@@ -127,18 +133,26 @@ export class FileUrlBuilder {
127133 */
128134 #toFileURL( filenameOrUrl : string | URL , relativeTo ?: string | URL ) : URL {
129135 if ( typeof filenameOrUrl !== 'string' ) return filenameOrUrl ;
130- if ( isUrlLike ( filenameOrUrl ) ) return new URL ( filenameOrUrl ) ;
136+ if ( isUrlLike ( filenameOrUrl ) ) return normalizeWindowsUrl ( new URL ( filenameOrUrl ) ) ;
131137 relativeTo ??= this . cwd ;
132138 isWindows && ( filenameOrUrl = filenameOrUrl . replaceAll ( '\\' , '/' ) ) ;
139+ if ( this . isAbsolute ( filenameOrUrl ) && isFileURL ( relativeTo ) ) {
140+ const pathname = this . normalizeFilePathForUrl ( filenameOrUrl ) ;
141+ if ( isWindowsFileUrl ( relativeTo ) && ! isWindowsPathnameWithDriveLatter ( pathname ) ) {
142+ const relFilePrefix = relativeTo . toString ( ) . slice ( 0 , 10 ) ;
143+ return normalizeWindowsUrl ( new URL ( relFilePrefix + pathname ) ) ;
144+ }
145+ return normalizeWindowsUrl ( new URL ( 'file://' + pathname ) ) ;
146+ }
133147 if ( isUrlLike ( relativeTo ) ) {
134148 const pathname = this . normalizeFilePathForUrl ( filenameOrUrl ) ;
135- return new URL ( pathname , relativeTo ) ;
149+ return normalizeWindowsUrl ( new URL ( pathname , relativeTo ) ) ;
136150 }
137151 // Resolve removes the trailing slash, so we need to add it back.
138152 const appendSlash = filenameOrUrl . endsWith ( '/' ) ? '/' : '' ;
139153 const pathname =
140154 this . normalizeFilePathForUrl ( this . path . resolve ( relativeTo . toString ( ) , filenameOrUrl ) ) + appendSlash ;
141- return this . pathToFileURL ( pathname , this . cwd ) ;
155+ return normalizeWindowsUrl ( new URL ( 'file://' + pathname ) ) ;
142156 }
143157
144158 /**
@@ -158,7 +172,7 @@ export class FileUrlBuilder {
158172 }
159173
160174 #urlToFilePathOrHref( url : URL ) : string {
161- if ( url . protocol !== ProtocolFile ) return url . href ;
175+ if ( url . protocol !== ProtocolFile || url . hostname ) return url . href ;
162176 const p =
163177 this . path === Path
164178 ? toFilePathOrHref ( url )
0 commit comments