Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(build): remove quotes from preload marker #16562

Merged
merged 7 commits into from
May 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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*\(/

Expand Down Expand Up @@ -260,7 +260,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'
: ''
Expand Down Expand Up @@ -419,15 +419,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) {
Expand Down Expand Up @@ -497,7 +494,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {

s.update(
markerStartPos,
markerStartPos + preloadMarker.length + 2,
markerStartPos + preloadMarker.length,
renderedDeps.length > 0
? `__vite__mapDeps([${renderedDeps.join(',')}])`
: `[]`,
Expand Down Expand Up @@ -526,19 +523,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,
)
}

Expand Down