@@ -6,13 +6,37 @@ const leadingDotSlashRegExp = /^\.\.?[/\\]/
66const slashRegExp = / [ / \\ ] /
77const nodeModulesPathRegExp = / (?: ^ | [ / \\ ] ) n o d e _ m o d u l e s (?: [ / \\ ] | $ ) /
88
9+ let _buffer
910/*@__NO_SIDE_EFFECTS__ */
10- function isNodeModules ( filepath ) {
11+ function getBuffer ( ) {
12+ if ( _buffer === undefined ) {
13+ // Use non-'node:' prefixed require to avoid Webpack errors.
14+ // eslint-disable-next-line n/prefer-node-protocol
15+ _buffer = /*@__PURE__ */ require ( 'buffer' )
16+ }
17+ return _buffer
18+ }
19+
20+ let _url
21+ /*@__NO_SIDE_EFFECTS__ */
22+ function getUrl ( ) {
23+ if ( _url === undefined ) {
24+ // Use non-'node:' prefixed require to avoid Webpack errors.
25+ // eslint-disable-next-line n/prefer-node-protocol
26+ _url = /*@__PURE__ */ require ( 'url' )
27+ }
28+ return _url
29+ }
30+
31+ /*@__NO_SIDE_EFFECTS__ */
32+ function isNodeModules ( pathLike ) {
33+ const filepath = pathLikeToString ( pathLike )
1134 return nodeModulesPathRegExp . test ( filepath )
1235}
1336
1437/*@__NO_SIDE_EFFECTS__ */
15- function isRelative ( filepath ) {
38+ function isRelative ( pathLike ) {
39+ const filepath = pathLikeToString ( pathLike )
1640 if ( typeof filepath !== 'string' ) {
1741 return false
1842 }
@@ -34,12 +58,13 @@ function isRelative(filepath) {
3458}
3559
3660/*@__NO_SIDE_EFFECTS__ */
37- function normalizePath ( filePath ) {
38- const { length } = filePath
61+ function normalizePath ( pathLike ) {
62+ const filepath = pathLikeToString ( pathLike )
63+ const { length } = filepath
3964 if ( length < 2 ) {
40- return length === 1 && filePath . charCodeAt ( 0 ) === 92 /*'\\'*/
65+ return length === 1 && filepath . charCodeAt ( 0 ) === 92 /*'\\'*/
4166 ? '/'
42- : filePath
67+ : filepath
4368 }
4469
4570 let code = 0
@@ -53,13 +78,13 @@ function normalizePath(filePath) {
5378 // are okay to convert to forward slashes.
5479 // https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions
5580 let prefix = ''
56- if ( length > 4 && filePath . charCodeAt ( 3 ) === 92 /*'\\'*/ ) {
57- const code2 = filePath . charCodeAt ( 2 )
81+ if ( length > 4 && filepath . charCodeAt ( 3 ) === 92 /*'\\'*/ ) {
82+ const code2 = filepath . charCodeAt ( 2 )
5883 // Look for \\?\ or \\.\
5984 if (
6085 ( code2 === 63 /*'?'*/ || code2 === 46 ) /*'.'*/ &&
61- filePath . charCodeAt ( 0 ) === 92 /*'\\'*/ &&
62- filePath . charCodeAt ( 1 ) === 92 /*'\\'*/
86+ filepath . charCodeAt ( 0 ) === 92 /*'\\'*/ &&
87+ filepath . charCodeAt ( 1 ) === 92 /*'\\'*/
6388 ) {
6489 start = 2
6590 prefix = '//'
@@ -68,7 +93,7 @@ function normalizePath(filePath) {
6893 if ( start === 0 ) {
6994 // Trim leading slashes
7095 while (
71- ( ( code = filePath . charCodeAt ( start ) ) ,
96+ ( ( code = filepath . charCodeAt ( start ) ) ,
7297 code === 47 /*'/'*/ || code === 92 ) /*'\\'*/
7398 ) {
7499 start += 1
@@ -77,44 +102,63 @@ function normalizePath(filePath) {
77102 prefix = '/'
78103 }
79104 }
80- let nextIndex = search ( filePath , slashRegExp , start )
105+ let nextIndex = search ( filepath , slashRegExp , start )
81106 if ( nextIndex === - 1 ) {
82- return prefix + filePath . slice ( start )
107+ return prefix + filepath . slice ( start )
83108 }
84109 // Discard any empty string segments by collapsing repeated segment separator slashes.
85110 while ( nextIndex !== - 1 ) {
86- const segment = filePath . slice ( start , nextIndex )
111+ const segment = filepath . slice ( start , nextIndex )
87112 collapsed = collapsed + ( collapsed . length === 0 ? '' : '/' ) + segment
88113 start = nextIndex + 1
89114 while (
90- ( ( code = filePath . charCodeAt ( start ) ) ,
115+ ( ( code = filepath . charCodeAt ( start ) ) ,
91116 code === 47 /*'/'*/ || code === 92 ) /*'\\'*/
92117 ) {
93118 start += 1
94119 }
95- nextIndex = search ( filePath , slashRegExp , start )
120+ nextIndex = search ( filepath , slashRegExp , start )
96121 }
97- const lastSegment = filePath . slice ( start )
122+ const lastSegment = filepath . slice ( start )
98123 if ( lastSegment . length !== 0 ) {
99124 collapsed = collapsed + '/' + lastSegment
100125 }
101126 return prefix + collapsed
102127}
103128
104129/*@__NO_SIDE_EFFECTS__ */
105- function splitPath ( filepath ) {
130+ function pathLikeToString ( pathLike ) {
131+ if ( typeof pathLike === 'string' ) {
132+ return pathLike
133+ }
134+ const Buffer = getBuffer ( )
135+ if ( Buffer . isBuffer ( pathLike ) ) {
136+ return pathLike . toString ( 'utf8' )
137+ }
138+ const url = getUrl ( )
139+ if ( pathLike instanceof URL ) {
140+ return url . fileURLToPath ( pathLike )
141+ }
142+ return String ( pathLike )
143+ }
144+
145+ /*@__NO_SIDE_EFFECTS__ */
146+ function splitPath ( pathLike ) {
147+ const filepath = pathLikeToString ( pathLike )
106148 return filepath . split ( slashRegExp )
107149}
108150
109151/*@__NO_SIDE_EFFECTS__ */
110- function trimLeadingDotSlash ( filepath ) {
152+ function trimLeadingDotSlash ( pathLike ) {
153+ const filepath = pathLikeToString ( pathLike )
111154 return filepath . replace ( leadingDotSlashRegExp , '' )
112155}
113156
114157module . exports = {
115158 isNodeModules,
116159 isRelative,
117160 normalizePath,
161+ pathLikeToString,
118162 splitPath,
119163 trimLeadingDotSlash
120164}
0 commit comments