@@ -50,8 +50,9 @@ export const createFilter = _createFilter as (
50
50
options ?: { resolve ?: string | false | null } ,
51
51
) => ( id : string | unknown ) => boolean
52
52
53
+ const windowsSlashRE = / \\ / g
53
54
export function slash ( p : string ) : string {
54
- return p . replace ( / \\ / g , '/' )
55
+ return p . replace ( windowsSlashRE , '/' )
55
56
}
56
57
57
58
/**
@@ -74,15 +75,19 @@ export function unwrapId(id: string): string {
74
75
: id
75
76
}
76
77
78
+ const replaceSlashOrColonRE = / [ / : ] / g
79
+ const replaceDotRE = / \. / g
80
+ const replaceNestedIdRE = / ( \s * > \s * ) / g
81
+ const replaceHashRE = / # / g
77
82
export const flattenId = ( id : string ) : string =>
78
83
id
79
- . replace ( / [ / : ] / g , '_' )
80
- . replace ( / \. / g , '__' )
81
- . replace ( / ( \s * > \s * ) / g , '___' )
82
- . replace ( / # / g , '____' )
84
+ . replace ( replaceSlashOrColonRE , '_' )
85
+ . replace ( replaceDotRE , '__' )
86
+ . replace ( replaceNestedIdRE , '___' )
87
+ . replace ( replaceHashRE , '____' )
83
88
84
89
export const normalizeId = ( id : string ) : string =>
85
- id . replace ( / ( \s * > \s * ) / g , ' > ' )
90
+ id . replace ( replaceNestedIdRE , ' > ' )
86
91
87
92
//TODO: revisit later to see if the edge case that "compiling using node v12 code to be run in node v16 in the server" is what we intend to support.
88
93
const builtins = new Set ( [
@@ -300,10 +305,14 @@ export function removeDirectQuery(url: string): string {
300
305
return url . replace ( directRequestRE , '$1' ) . replace ( trailingSeparatorRE , '' )
301
306
}
302
307
308
+ const replacePercentageRE = / % / g
303
309
export function injectQuery ( url : string , queryToInject : string ) : string {
304
310
// encode percents for consistent behavior with pathToFileURL
305
311
// see #2614 for details
306
- const resolvedUrl = new URL ( url . replace ( / % / g, '%25' ) , 'relative:///' )
312
+ const resolvedUrl = new URL (
313
+ url . replace ( replacePercentageRE , '%25' ) ,
314
+ 'relative:///' ,
315
+ )
307
316
const { search, hash } = resolvedUrl
308
317
let pathname = cleanUrl ( url )
309
318
pathname = isWindows ? slash ( pathname ) : pathname
@@ -659,13 +668,12 @@ export function processSrcSetSync(
659
668
)
660
669
}
661
670
671
+ const cleanSrcSetRE =
672
+ / (?: u r l | i m a g e | g r a d i e n t | c r o s s - f a d e ) \( [ ^ ) ] * \) | " ( [ ^ " ] | (?< = \\ ) " ) * " | ' ( [ ^ ' ] | (?< = \\ ) ' ) * ' / g
662
673
function splitSrcSet ( srcs : string ) {
663
674
const parts : string [ ] = [ ]
664
675
// There could be a ',' inside of url(data:...), linear-gradient(...) or "data:..."
665
- const cleanedSrcs = srcs . replace (
666
- / (?: u r l | i m a g e | g r a d i e n t | c r o s s - f a d e ) \( [ ^ ) ] * \) | " ( [ ^ " ] | (?< = \\ ) " ) * " | ' ( [ ^ ' ] | (?< = \\ ) ' ) * ' / g,
667
- blankReplacer ,
668
- )
676
+ const cleanedSrcs = srcs . replace ( cleanSrcSetRE , blankReplacer )
669
677
let startIndex = 0
670
678
let splitIndex : number
671
679
do {
@@ -678,22 +686,26 @@ function splitSrcSet(srcs: string) {
678
686
return parts
679
687
}
680
688
689
+ const windowsDriveRE = / ^ [ A - Z ] : /
690
+ const replaceWindowsDriveRE = / ^ ( [ A - Z ] ) : \/ /
691
+ const linuxAbsolutePathRE = / ^ \/ [ ^ / ] /
681
692
function escapeToLinuxLikePath ( path : string ) {
682
- if ( / ^ [ A - Z ] : / . test ( path ) ) {
683
- return path . replace ( / ^ ( [ A - Z ] ) : \/ / , '/windows/$1/' )
693
+ if ( windowsDriveRE . test ( path ) ) {
694
+ return path . replace ( replaceWindowsDriveRE , '/windows/$1/' )
684
695
}
685
- if ( / ^ \/ [ ^ / ] / . test ( path ) ) {
696
+ if ( linuxAbsolutePathRE . test ( path ) ) {
686
697
return `/linux${ path } `
687
698
}
688
699
return path
689
700
}
690
701
702
+ const revertWindowsDriveRE = / ^ \/ w i n d o w s \/ ( [ A - Z ] ) \/ /
691
703
function unescapeToLinuxLikePath ( path : string ) {
692
704
if ( path . startsWith ( '/linux/' ) ) {
693
705
return path . slice ( '/linux' . length )
694
706
}
695
707
if ( path . startsWith ( '/windows/' ) ) {
696
- return path . replace ( / ^ \/ w i n d o w s \/ ( [ A - Z ] ) \/ / , '$1:/' )
708
+ return path . replace ( revertWindowsDriveRE , '$1:/' )
697
709
}
698
710
return path
699
711
}
@@ -1222,6 +1234,10 @@ export function joinUrlSegments(a: string, b: string): string {
1222
1234
return a + b
1223
1235
}
1224
1236
1237
+ export function removeLeadingSlash ( str : string ) : string {
1238
+ return str [ 0 ] === '/' ? str . slice ( 1 ) : str
1239
+ }
1240
+
1225
1241
export function stripBase ( path : string , base : string ) : string {
1226
1242
if ( path === base ) {
1227
1243
return '/'
@@ -1246,3 +1262,8 @@ export function evalValue<T = any>(rawValue: string): T {
1246
1262
` )
1247
1263
return fn ( )
1248
1264
}
1265
+
1266
+ const escapeRegexRE = / [ - / \\ ^ $ * + ? . ( ) | [ \] { } ] / g
1267
+ export function escapeRegex ( str : string ) : string {
1268
+ return str . replace ( escapeRegexRE , '\\$&' )
1269
+ }
0 commit comments