diff --git a/packages/vite/src/node/plugins/importAnalysisBuild.ts b/packages/vite/src/node/plugins/importAnalysisBuild.ts index e5a076303..e6836c117 100644 --- a/packages/vite/src/node/plugins/importAnalysisBuild.ts +++ b/packages/vite/src/node/plugins/importAnalysisBuild.ts @@ -45,6 +45,55 @@ function findPreloadMarker(str: string, pos: number = 0): number { return result?.index ?? -1 } +/** + * Pairs each dynamic `import()` with the `__VITE_PRELOAD__` marker that belongs to it. + * + * Each import is wrapped as `__vitePreload(factory, __VITE_PRELOAD__)`, so its marker + * comes right after it. Nested imports nest the wrappers, so the imports and markers + * form balanced brackets. For example, in `import('a').then(() => import('b'))`, b's + * marker ends up before a's. Pairing each import with the next marker would give b's + * marker to both imports and drop a's CSS to `void 0` (#22700). Instead the markers are + * matched with a stack in one pass, and each marker closes the innermost import that is + * still open. + * + * The returned array is parallel to `imports`. Entry `i` holds the start position of + * import `i`'s marker, or -1 when the import has no marker. + */ +export function matchImportsToPreloadMarkers( + code: string, + imports: readonly ImportSpecifier[], +): number[] { + const importMarkerPos = new Array(imports.length).fill(-1) + if (imports.length === 0) return importMarkerPos + + const openImports: number[] = [] + let nextImport = 0 + for ( + let markerStartPos = findPreloadMarker(code, imports[0].e); + markerStartPos !== -1; + markerStartPos = findPreloadMarker( + code, + markerStartPos + preloadMarker.length, + ) + ) { + while ( + nextImport < imports.length && + imports[nextImport].e <= markerStartPos + ) { + openImports.push(nextImport++) + } + if (openImports.length) { + importMarkerPos[openImports.pop()!] = markerStartPos + } + } + // #3051: a lone import whose marker isn't placed after it pairs with the only marker + if (imports.length === 1 && importMarkerPos[0] === -1) { + importMarkerPos[0] = findPreloadMarker(code) + } + + return importMarkerPos +} + /** * Helper for preloading CSS and direct imports of async chunks in parallel to * the async chunk itself. @@ -369,6 +418,8 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin[] { } if (imports.length) { + const importMarkerPos = matchImportsToPreloadMarkers(code, imports) + for (let index = 0; index < imports.length; index++) { // To handle escape sequences in specifier strings, the .n field will be provided where possible. const { @@ -438,11 +489,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin[] { addDeps(normalizedFile) } - let markerStartPos = findPreloadMarker(code, end) - // fix issue #3051 - if (markerStartPos === -1 && imports.length === 1) { - markerStartPos = findPreloadMarker(code) - } + const markerStartPos = importMarkerPos[index] if (markerStartPos > 0) { // the dep list includes the main chunk, so only need to reload when there are actual other deps.