Skip to content

Commit 9853190

Browse files
PengBoUESTCpengbo43
andauthored
refactor(build): remove quotes from preload marker (#16562)
Co-authored-by: pengbo43 <pengbo43@jd.com>
1 parent 4102ca9 commit 9853190

1 file changed

Lines changed: 9 additions & 12 deletions

File tree

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export const preloadMarker = `__VITE_PRELOAD__`
3737
exportconstpreloadBaseMarker=`__VITE_PRELOAD_BASE__`
3838

3939
exportconstpreloadHelperId='\0vite/preload-helper.js'
40-
constpreloadMarkerWithQuote=newRegExp(`['"]${preloadMarker}['"]`,'g')
40+
constpreloadMarkerRE=newRegExp(preloadMarker,'g')
4141

4242
constdynamicImportPrefixRE=/import\s*\(/
4343

@@ -268,7 +268,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
268268
str().prependLeft(expStart,`${preloadMethod}(() => `)
269269
str().appendRight(
270270
expEnd,
271-
`,${isModernFlag}?"${preloadMarker}":void 0${
271+
`,${isModernFlag}?${preloadMarker}:void 0${
272272
optimizeModulePreloadRelativePaths||customModulePreloadPaths
273273
? ',import.meta.url'
274274
: ''
@@ -427,15 +427,12 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
427427

428428
letmarkerStartPos=indexOfMatchInSlice(
429429
code,
430-
preloadMarkerWithQuote,
430+
preloadMarkerRE,
431431
end,
432432
)
433433
// fix issue #3051
434434
if(markerStartPos===-1&&imports.length===1){
435-
markerStartPos=indexOfMatchInSlice(
436-
code,
437-
preloadMarkerWithQuote,
438-
)
435+
markerStartPos=indexOfMatchInSlice(code,preloadMarkerRE)
439436
}
440437

441438
if(markerStartPos>0){
@@ -505,7 +502,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
505502

506503
s.update(
507504
markerStartPos,
508-
markerStartPos+preloadMarker.length+2,
505+
markerStartPos+preloadMarker.length,
509506
renderedDeps.length>0
510507
? `__vite__mapDeps([${renderedDeps.join(',')}])`
511508
: `[]`,
@@ -534,19 +531,19 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
534531

535532
// there may still be markers due to inlined dynamic imports, remove
536533
// all the markers regardless
537-
letmarkerStartPos=indexOfMatchInSlice(code,preloadMarkerWithQuote)
534+
letmarkerStartPos=indexOfMatchInSlice(code,preloadMarkerRE)
538535
while(markerStartPos>=0){
539536
if(!rewroteMarkerStartPos.has(markerStartPos)){
540537
s.update(
541538
markerStartPos,
542-
markerStartPos+preloadMarker.length+2,
539+
markerStartPos+preloadMarker.length,
543540
'void 0',
544541
)
545542
}
546543
markerStartPos=indexOfMatchInSlice(
547544
code,
548-
preloadMarkerWithQuote,
549-
markerStartPos+preloadMarker.length+2,
545+
preloadMarkerRE,
546+
markerStartPos+preloadMarker.length,
550547
)
551548
}
552549

0 commit comments

Comments
 (0)