Skip to content

Commit 447df7c

Browse files
authoredApr 28, 2023
fix(importAnalysisBuild): support parsing '__VITE_PRELOAD__' (#13023)
1 parent fe73198 commit 447df7c

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed
 

‎packages/vite/src/node/plugins/importAnalysisBuild.ts

+30-9
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export const preloadMarker = `__VITE_PRELOAD__`
3636
export const preloadBaseMarker = `__VITE_PRELOAD_BASE__`
3737

3838
export const preloadHelperId = '\0vite/preload-helper'
39-
const preloadMarkerWithQuote = `"${preloadMarker}"` as const
39+
const preloadMarkerWithQuote = new RegExp(`['"]${preloadMarker}['"]`)
4040

4141
const dynamicImportPrefixRE = /import\s*\(/
4242

@@ -49,6 +49,20 @@ function toRelativePath(filename: string, importer: string) {
4949
return relPath[0] === '.' ? relPath : `./${relPath}`
5050
}
5151

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+
5266
/**
5367
* Helper for preloading CSS and direct imports of async chunks in parallel to
5468
* the async chunk itself.
@@ -507,10 +521,17 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
507521
addDeps(normalizedFile)
508522
}
509523

510-
let markerStartPos = code.indexOf(preloadMarkerWithQuote, end)
524+
let markerStartPos = indexOfMatchInSlice(
525+
code,
526+
preloadMarkerWithQuote,
527+
end,
528+
)
511529
// fix issue #3051
512530
if (markerStartPos === -1 && imports.length === 1) {
513-
markerStartPos = code.indexOf(preloadMarkerWithQuote)
531+
markerStartPos = indexOfMatchInSlice(
532+
code,
533+
preloadMarkerWithQuote,
534+
)
514535
}
515536

516537
if (markerStartPos > 0) {
@@ -577,7 +598,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
577598

578599
s.update(
579600
markerStartPos,
580-
markerStartPos + preloadMarkerWithQuote.length,
601+
markerStartPos + preloadMarker.length + 2,
581602
`[${renderedDeps.join(',')}]`,
582603
)
583604
rewroteMarkerStartPos.add(markerStartPos)
@@ -587,19 +608,19 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
587608

588609
// there may still be markers due to inlined dynamic imports, remove
589610
// all the markers regardless
590-
let markerStartPos = code.indexOf(preloadMarkerWithQuote)
611+
let markerStartPos = indexOfMatchInSlice(code, preloadMarkerWithQuote)
591612
while (markerStartPos >= 0) {
592613
if (!rewroteMarkerStartPos.has(markerStartPos)) {
593614
s.update(
594615
markerStartPos,
595-
markerStartPos + preloadMarkerWithQuote.length,
616+
markerStartPos + preloadMarker.length + 2,
596617
'void 0',
597618
)
598619
}
599-
600-
markerStartPos = code.indexOf(
620+
markerStartPos = indexOfMatchInSlice(
621+
code,
601622
preloadMarkerWithQuote,
602-
markerStartPos + preloadMarkerWithQuote.length,
623+
markerStartPos + preloadMarker.length + 2,
603624
)
604625
}
605626

0 commit comments

Comments
 (0)
Please sign in to comment.