@@ -36,7 +36,7 @@ export const preloadMarker = `__VITE_PRELOAD__`
36
36
export const preloadBaseMarker = `__VITE_PRELOAD_BASE__`
37
37
38
38
export const preloadHelperId = '\0vite/preload-helper'
39
- const preloadMarkerWithQuote = `" ${ preloadMarker } "` as const
39
+ const preloadMarkerWithQuote = new RegExp ( `['"] ${ preloadMarker } ['"]` )
40
40
41
41
const dynamicImportPrefixRE = / i m p o r t \s * \( /
42
42
@@ -49,6 +49,20 @@ function toRelativePath(filename: string, importer: string) {
49
49
return relPath [ 0 ] === '.' ? relPath : `./${ relPath } `
50
50
}
51
51
52
+ function indexOfMatchInSlice (
53
+ str : string ,
54
+ reg : RegExp ,
55
+ pos : number = 0 ,
56
+ ) : number {
57
+ if ( pos !== 0 ) {
58
+ str = str . slice ( pos )
59
+ }
60
+
61
+ const matcher = str . match ( reg )
62
+
63
+ return matcher ?. index !== undefined ? matcher . index + pos : - 1
64
+ }
65
+
52
66
/**
53
67
* Helper for preloading CSS and direct imports of async chunks in parallel to
54
68
* the async chunk itself.
@@ -507,10 +521,17 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
507
521
addDeps ( normalizedFile )
508
522
}
509
523
510
- let markerStartPos = code . indexOf ( preloadMarkerWithQuote , end )
524
+ let markerStartPos = indexOfMatchInSlice (
525
+ code ,
526
+ preloadMarkerWithQuote ,
527
+ end ,
528
+ )
511
529
// fix issue #3051
512
530
if ( markerStartPos === - 1 && imports . length === 1 ) {
513
- markerStartPos = code . indexOf ( preloadMarkerWithQuote )
531
+ markerStartPos = indexOfMatchInSlice (
532
+ code ,
533
+ preloadMarkerWithQuote ,
534
+ )
514
535
}
515
536
516
537
if ( markerStartPos > 0 ) {
@@ -577,7 +598,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
577
598
578
599
s . update (
579
600
markerStartPos ,
580
- markerStartPos + preloadMarkerWithQuote . length ,
601
+ markerStartPos + preloadMarker . length + 2 ,
581
602
`[${ renderedDeps . join ( ',' ) } ]` ,
582
603
)
583
604
rewroteMarkerStartPos . add ( markerStartPos )
@@ -587,19 +608,19 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
587
608
588
609
// there may still be markers due to inlined dynamic imports, remove
589
610
// all the markers regardless
590
- let markerStartPos = code . indexOf ( preloadMarkerWithQuote )
611
+ let markerStartPos = indexOfMatchInSlice ( code , preloadMarkerWithQuote )
591
612
while ( markerStartPos >= 0 ) {
592
613
if ( ! rewroteMarkerStartPos . has ( markerStartPos ) ) {
593
614
s . update (
594
615
markerStartPos ,
595
- markerStartPos + preloadMarkerWithQuote . length ,
616
+ markerStartPos + preloadMarker . length + 2 ,
596
617
'void 0' ,
597
618
)
598
619
}
599
-
600
- markerStartPos = code . indexOf (
620
+ markerStartPos = indexOfMatchInSlice (
621
+ code ,
601
622
preloadMarkerWithQuote ,
602
- markerStartPos + preloadMarkerWithQuote . length ,
623
+ markerStartPos + preloadMarker . length + 2 ,
603
624
)
604
625
}
605
626
0 commit comments