File size: 3,374 Bytes
8579f8a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | 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<number>(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.
|