From 98531901eef3ac64242f8438c3ef83e65c2ac88c Mon Sep 17 00:00:00 2001 From: pengbo <57180744+PengBoUESTC@users.noreply.github.com> Date: Wed, 29 May 2024 22:21:25 +0800 Subject: [PATCH] refactor(build): remove quotes from preload marker (#16562) Co-authored-by: pengbo43 --- .../src/node/plugins/importAnalysisBuild.ts | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/packages/vite/src/node/plugins/importAnalysisBuild.ts b/packages/vite/src/node/plugins/importAnalysisBuild.ts index a00ed2461c404b..22a0d61ef3aae3 100644 --- a/packages/vite/src/node/plugins/importAnalysisBuild.ts +++ b/packages/vite/src/node/plugins/importAnalysisBuild.ts @@ -37,7 +37,7 @@ export const preloadMarker = `__VITE_PRELOAD__` export const preloadBaseMarker = `__VITE_PRELOAD_BASE__` export const preloadHelperId = '\0vite/preload-helper.js' -const preloadMarkerWithQuote = new RegExp(`['"]${preloadMarker}['"]`, 'g') +const preloadMarkerRE = new RegExp(preloadMarker, 'g') const dynamicImportPrefixRE = /import\s*\(/ @@ -268,7 +268,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { str().prependLeft(expStart, `${preloadMethod}(() => `) str().appendRight( expEnd, - `,${isModernFlag}?"${preloadMarker}":void 0${ + `,${isModernFlag}?${preloadMarker}:void 0${ optimizeModulePreloadRelativePaths || customModulePreloadPaths ? ',import.meta.url' : '' @@ -427,15 +427,12 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { let markerStartPos = indexOfMatchInSlice( code, - preloadMarkerWithQuote, + preloadMarkerRE, end, ) // fix issue #3051 if (markerStartPos === -1 && imports.length === 1) { - markerStartPos = indexOfMatchInSlice( - code, - preloadMarkerWithQuote, - ) + markerStartPos = indexOfMatchInSlice(code, preloadMarkerRE) } if (markerStartPos > 0) { @@ -505,7 +502,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { s.update( markerStartPos, - markerStartPos + preloadMarker.length + 2, + markerStartPos + preloadMarker.length, renderedDeps.length > 0 ? `__vite__mapDeps([${renderedDeps.join(',')}])` : `[]`, @@ -534,19 +531,19 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { // there may still be markers due to inlined dynamic imports, remove // all the markers regardless - let markerStartPos = indexOfMatchInSlice(code, preloadMarkerWithQuote) + let markerStartPos = indexOfMatchInSlice(code, preloadMarkerRE) while (markerStartPos >= 0) { if (!rewroteMarkerStartPos.has(markerStartPos)) { s.update( markerStartPos, - markerStartPos + preloadMarker.length + 2, + markerStartPos + preloadMarker.length, 'void 0', ) } markerStartPos = indexOfMatchInSlice( code, - preloadMarkerWithQuote, - markerStartPos + preloadMarker.length + 2, + preloadMarkerRE, + markerStartPos + preloadMarker.length, ) }