@@ -6,13 +6,37 @@ const leadingDotSlashRegExp = /^\.\.?[/\\]/
6
6
const slashRegExp = / [ / \\ ] /
7
7
const nodeModulesPathRegExp = / (?: ^ | [ / \\ ] ) n o d e _ m o d u l e s (?: [ / \\ ] | $ ) /
8
8
9
+ let _buffer
9
10
/*@__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 )
11
34
return nodeModulesPathRegExp . test ( filepath )
12
35
}
13
36
14
37
/*@__NO_SIDE_EFFECTS__ */
15
- function isRelative ( filepath ) {
38
+ function isRelative ( pathLike ) {
39
+ const filepath = pathLikeToString ( pathLike )
16
40
if ( typeof filepath !== 'string' ) {
17
41
return false
18
42
}
@@ -34,12 +58,13 @@ function isRelative(filepath) {
34
58
}
35
59
36
60
/*@__NO_SIDE_EFFECTS__ */
37
- function normalizePath ( filePath ) {
38
- const { length } = filePath
61
+ function normalizePath ( pathLike ) {
62
+ const filepath = pathLikeToString ( pathLike )
63
+ const { length } = filepath
39
64
if ( length < 2 ) {
40
- return length === 1 && filePath . charCodeAt ( 0 ) === 92 /*'\\'*/
65
+ return length === 1 && filepath . charCodeAt ( 0 ) === 92 /*'\\'*/
41
66
? '/'
42
- : filePath
67
+ : filepath
43
68
}
44
69
45
70
let code = 0
@@ -53,13 +78,13 @@ function normalizePath(filePath) {
53
78
// are okay to convert to forward slashes.
54
79
// https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions
55
80
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 )
58
83
// Look for \\?\ or \\.\
59
84
if (
60
85
( code2 === 63 /*'?'*/ || code2 === 46 ) /*'.'*/ &&
61
- filePath . charCodeAt ( 0 ) === 92 /*'\\'*/ &&
62
- filePath . charCodeAt ( 1 ) === 92 /*'\\'*/
86
+ filepath . charCodeAt ( 0 ) === 92 /*'\\'*/ &&
87
+ filepath . charCodeAt ( 1 ) === 92 /*'\\'*/
63
88
) {
64
89
start = 2
65
90
prefix = '//'
@@ -68,7 +93,7 @@ function normalizePath(filePath) {
68
93
if ( start === 0 ) {
69
94
// Trim leading slashes
70
95
while (
71
- ( ( code = filePath . charCodeAt ( start ) ) ,
96
+ ( ( code = filepath . charCodeAt ( start ) ) ,
72
97
code === 47 /*'/'*/ || code === 92 ) /*'\\'*/
73
98
) {
74
99
start += 1
@@ -77,44 +102,63 @@ function normalizePath(filePath) {
77
102
prefix = '/'
78
103
}
79
104
}
80
- let nextIndex = search ( filePath , slashRegExp , start )
105
+ let nextIndex = search ( filepath , slashRegExp , start )
81
106
if ( nextIndex === - 1 ) {
82
- return prefix + filePath . slice ( start )
107
+ return prefix + filepath . slice ( start )
83
108
}
84
109
// Discard any empty string segments by collapsing repeated segment separator slashes.
85
110
while ( nextIndex !== - 1 ) {
86
- const segment = filePath . slice ( start , nextIndex )
111
+ const segment = filepath . slice ( start , nextIndex )
87
112
collapsed = collapsed + ( collapsed . length === 0 ? '' : '/' ) + segment
88
113
start = nextIndex + 1
89
114
while (
90
- ( ( code = filePath . charCodeAt ( start ) ) ,
115
+ ( ( code = filepath . charCodeAt ( start ) ) ,
91
116
code === 47 /*'/'*/ || code === 92 ) /*'\\'*/
92
117
) {
93
118
start += 1
94
119
}
95
- nextIndex = search ( filePath , slashRegExp , start )
120
+ nextIndex = search ( filepath , slashRegExp , start )
96
121
}
97
- const lastSegment = filePath . slice ( start )
122
+ const lastSegment = filepath . slice ( start )
98
123
if ( lastSegment . length !== 0 ) {
99
124
collapsed = collapsed + '/' + lastSegment
100
125
}
101
126
return prefix + collapsed
102
127
}
103
128
104
129
/*@__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 )
106
148
return filepath . split ( slashRegExp )
107
149
}
108
150
109
151
/*@__NO_SIDE_EFFECTS__ */
110
- function trimLeadingDotSlash ( filepath ) {
152
+ function trimLeadingDotSlash ( pathLike ) {
153
+ const filepath = pathLikeToString ( pathLike )
111
154
return filepath . replace ( leadingDotSlashRegExp , '' )
112
155
}
113
156
114
157
module . exports = {
115
158
isNodeModules,
116
159
isRelative,
117
160
normalizePath,
161
+ pathLikeToString,
118
162
splitPath,
119
163
trimLeadingDotSlash
120
164
}
0 commit comments