diff --git a/packages/vite/src/node/plugins/importAnalysisBuild.ts b/packages/vite/src/node/plugins/importAnalysisBuild.ts index f392cfda538584..2e685f12ea308e 100644 --- a/packages/vite/src/node/plugins/importAnalysisBuild.ts +++ b/packages/vite/src/node/plugins/importAnalysisBuild.ts @@ -390,6 +390,16 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { const s = new MagicString(code) const rewroteMarkerStartPos = new Set() // position of the leading double quote + const fileDeps: string[] = [] + const addFileDep = (url: string): number => { + const index = fileDeps.indexOf(url) + if (index !== -1) { + return index + } else { + return fileDeps.push(url) - 1 + } + } + if (imports.length) { for (let index = 0; index < imports.length; index++) { // To handle escape sequences in specifier strings, the .n field will be provided where possible. @@ -407,7 +417,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { if (rawUrl[0] === `"` && rawUrl[rawUrl.length - 1] === `"`) url = rawUrl.slice(1, -1) } - const deps: Set = new Set() + const deps: Set = new Set() let hasRemovedPureCssChunk = false if (url) { @@ -420,9 +430,9 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { analyzed.add(filename) const chunk = bundle[filename] as OutputChunk | undefined if (chunk) { - deps.add(chunk.fileName) + deps.add(addFileDep(chunk.fileName)) chunk.viteMetadata.importedCss.forEach((file) => { - deps.add(file) + deps.add(addFileDep(file)) }) chunk.imports.forEach(addDeps) } else { @@ -432,7 +442,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { if (chunk) { if (chunk.viteMetadata.importedCss.size) { chunk.viteMetadata.importedCss.forEach((file) => { - deps.add(file) + deps.add(addFileDep(file)) }) hasRemovedPureCssChunk = true } @@ -466,15 +476,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { deps.size > 1 || // main chunk is removed (hasRemovedPureCssChunk && deps.size > 0) - ? `[${[...deps] - .map((d) => - JSON.stringify( - relativePreloadUrls - ? path.relative(path.dirname(file), d) - : d - ) - ) - .join(',')}]` + ? `__viteMapDep([${[...deps].join(',')}])` : `[]`, { contentOnly: true } ) @@ -483,6 +485,20 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { } } + s.append(`\ +function __viteMapDep(indexes) { + if (!__viteMapDep.viteFileDeps) { + __viteMapDep.viteFileDeps = ${JSON.stringify( + fileDeps.map((fileDep) => + relativePreloadUrls + ? path.relative(path.dirname(file), fileDep) + : fileDep + ) + )} + } + return indexes.map((i) => __viteMapDep.viteFileDeps[i]) +}`) + // there may still be markers due to inlined dynamic imports, remove // all the markers regardless let markerStartPos = code.indexOf(preloadMarkerWithQuote)