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

fix(importAnalysisBuild): support parsing '__VITE_PRELOAD__' #13023

Merged
merged 3 commits into from Apr 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 30 additions & 9 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Expand Up @@ -36,7 +36,7 @@ export const preloadMarker = `__VITE_PRELOAD__`
export const preloadBaseMarker = `__VITE_PRELOAD_BASE__`

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

const dynamicImportPrefixRE = /import\s*\(/

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

function indexOfMatchInSlice(
str: string,
reg: RegExp,
pos: number = 0,
): number {
if (pos !== 0) {
str = str.slice(pos)
}

const matcher = str.match(reg)

return matcher?.index !== undefined ? matcher.index + pos : -1
}

/**
* Helper for preloading CSS and direct imports of async chunks in parallel to
* the async chunk itself.
Expand Down Expand Up @@ -507,10 +521,17 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
addDeps(normalizedFile)
}

let markerStartPos = code.indexOf(preloadMarkerWithQuote, end)
let markerStartPos = indexOfMatchInSlice(
code,
preloadMarkerWithQuote,
end,
)
// fix issue #3051
if (markerStartPos === -1 && imports.length === 1) {
markerStartPos = code.indexOf(preloadMarkerWithQuote)
markerStartPos = indexOfMatchInSlice(
code,
preloadMarkerWithQuote,
)
}

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

s.update(
markerStartPos,
markerStartPos + preloadMarkerWithQuote.length,
markerStartPos + preloadMarker.length + 2,
`[${renderedDeps.join(',')}]`,
)
rewroteMarkerStartPos.add(markerStartPos)
Expand All @@ -587,19 +608,19 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {

// there may still be markers due to inlined dynamic imports, remove
// all the markers regardless
let markerStartPos = code.indexOf(preloadMarkerWithQuote)
let markerStartPos = indexOfMatchInSlice(code, preloadMarkerWithQuote)
while (markerStartPos >= 0) {
if (!rewroteMarkerStartPos.has(markerStartPos)) {
s.update(
markerStartPos,
markerStartPos + preloadMarkerWithQuote.length,
markerStartPos + preloadMarker.length + 2,
'void 0',
)
}

markerStartPos = code.indexOf(
markerStartPos = indexOfMatchInSlice(
code,
preloadMarkerWithQuote,
markerStartPos + preloadMarkerWithQuote.length,
markerStartPos + preloadMarker.length + 2,
)
}

Expand Down