diff --git a/packages/vite/src/node/plugins/asset.ts b/packages/vite/src/node/plugins/asset.ts index 4603af6729a9a5..66e3ae51db60d8 100644 --- a/packages/vite/src/node/plugins/asset.ts +++ b/packages/vite/src/node/plugins/asset.ts @@ -28,6 +28,7 @@ import { } from '../utils' import { FS_PREFIX } from '../constants' import type { ModuleGraph } from '../server/moduleGraph' +import { getChunkMetadata } from './metadata' // referenceId is base64url but replaces - with $ export const assetUrlRE = /__VITE_ASSET__([\w$]+)__(?:\$_(.*?)__)?/g @@ -93,7 +94,7 @@ export function renderAssetUrlInJS( s ||= new MagicString(code) const [full, referenceId, postfix = ''] = match const file = ctx.getFileName(referenceId) - chunk.viteMetadata!.importedAssets.add(cleanUrl(file)) + getChunkMetadata(chunk.fileName)!.importedAssets.add(cleanUrl(file)) const filename = file + postfix const replacement = toOutputFilePathInJS( filename, diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index dce6264b514fb3..fbc09cb4a39e0e 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -70,6 +70,7 @@ import { renderAssetUrlInJS, } from './asset' import type { ESBuildOptions } from './esbuild' +import { getChunkMetadata } from './metadata' // const debug = createDebugger('vite:css') @@ -567,7 +568,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin { // replace asset url references with resolved url. chunkCSS = chunkCSS.replace(assetUrlRE, (_, fileHash, postfix = '') => { const filename = this.getFileName(fileHash) + postfix - chunk.viteMetadata!.importedAssets.add(cleanUrl(filename)) + getChunkMetadata(chunk.fileName)!.importedAssets.add(cleanUrl(filename)) return toOutputFilePathInCss( filename, 'asset', @@ -648,8 +649,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin { generatedAssets .get(config)! .set(referenceId, { originalName, isEntry }) - chunk.viteMetadata!.importedCss.add(this.getFileName(referenceId)) - + getChunkMetadata(chunk.fileName)!.importedCss.add(this.getFileName(referenceId)) if (emitTasksLength === emitTasks.length) { // this is the last task, clear `emitTasks` to free up memory emitTasks = [] @@ -709,9 +709,9 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin { }, augmentChunkHash(chunk) { - if (chunk.viteMetadata?.importedCss.size) { + if (getChunkMetadata(chunk.fileName)?.importedCss.size) { let hash = '' - for (const id of chunk.viteMetadata.importedCss) { + for (const id of getChunkMetadata(chunk.fileName)!.importedCss) { hash += id } return hash @@ -752,14 +752,14 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin { // chunks instead. chunk.imports = chunk.imports.filter((file) => { if (pureCssChunkNames.includes(file)) { - const { importedCss, importedAssets } = ( + const { importedCss, importedAssets } = getChunkMetadata(( bundle[file] as OutputChunk - ).viteMetadata! + ).fileName)! importedCss.forEach((file) => - chunk.viteMetadata!.importedCss.add(file), + getChunkMetadata(chunk.fileName)!.importedCss.add(file), ) importedAssets.forEach((file) => - chunk.viteMetadata!.importedAssets.add(file), + getChunkMetadata(chunk.fileName)!.importedAssets.add(file), ) return false } diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts index a56927d14b992f..d41c26cc2ba6c3 100644 --- a/packages/vite/src/node/plugins/html.ts +++ b/packages/vite/src/node/plugins/html.ts @@ -36,6 +36,7 @@ import { } from './asset' import { isCSSRequest } from './css' import { modulePreloadPolyfillId } from './modulePreloadPolyfill' +import { getChunkMetadata } from './metadata' interface ScriptAssetsUrl { start: number @@ -682,7 +683,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { }) } - chunk.viteMetadata!.importedCss.forEach((file) => { + getChunkMetadata(chunk.fileName)!.importedCss.forEach((file) => { if (!seen.has(file)) { seen.add(file) tags.push({ @@ -832,7 +833,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { result = result.replace(assetUrlRE, (_, fileHash, postfix = '') => { const file = this.getFileName(fileHash) if (chunk) { - chunk.viteMetadata!.importedAssets.add(cleanUrl(file)) + getChunkMetadata(chunk.fileName)!.importedAssets.add(cleanUrl(file)) } return toOutputAssetFilePath(file) + postfix }) diff --git a/packages/vite/src/node/plugins/importAnalysisBuild.ts b/packages/vite/src/node/plugins/importAnalysisBuild.ts index 918a219f322a75..2f6b6e22fe0efd 100644 --- a/packages/vite/src/node/plugins/importAnalysisBuild.ts +++ b/packages/vite/src/node/plugins/importAnalysisBuild.ts @@ -25,6 +25,7 @@ import { genSourceMapUrl } from '../server/sourcemap' import { getDepsOptimizer, optimizedDepNeedsInterop } from '../optimizer' import { removedPureCssFilesCache } from './css' import { createParseErrorInfo, interopNamedImports } from './importAnalysis' +import { getChunkMetadata } from './metadata' type FileDep = { url: string @@ -522,7 +523,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { chunk.imports.forEach(addDeps) // Ensure that the css imported by current chunk is loaded after the dependencies. // So the style of current chunk won't be overwritten unexpectedly. - chunk.viteMetadata!.importedCss.forEach((file) => { + getChunkMetadata(chunk.fileName)!.importedCss.forEach((file) => { deps.add(addFileDep(file)) }) } else { @@ -530,8 +531,8 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { removedPureCssFilesCache.get(config)! const chunk = removedPureCssFiles.get(filename) if (chunk) { - if (chunk.viteMetadata!.importedCss.size) { - chunk.viteMetadata!.importedCss.forEach((file) => { + if (getChunkMetadata(chunk.fileName)!.importedCss.size) { + getChunkMetadata(chunk.fileName)!.importedCss.forEach((file) => { deps.add(addFileDep(file)) }) hasRemovedPureCssChunk = true diff --git a/packages/vite/src/node/plugins/manifest.ts b/packages/vite/src/node/plugins/manifest.ts index 613d0b586c988f..01f560971d3f44 100644 --- a/packages/vite/src/node/plugins/manifest.ts +++ b/packages/vite/src/node/plugins/manifest.ts @@ -6,6 +6,7 @@ import type { Plugin } from '../plugin' import { normalizePath } from '../utils' import { generatedAssets } from './asset' import type { GeneratedAssetMeta } from './asset' +import { getChunkMetadata } from './metadata' export type Manifest = Record @@ -92,11 +93,11 @@ export function manifestPlugin(config: ResolvedConfig): Plugin { } } - if (chunk.viteMetadata?.importedCss.size) { - manifestChunk.css = [...chunk.viteMetadata.importedCss] + if (getChunkMetadata(chunk.fileName)?.importedCss.size) { + manifestChunk.css = [...getChunkMetadata(chunk.fileName)!.importedCss] } - if (chunk.viteMetadata?.importedAssets.size) { - manifestChunk.assets = [...chunk.viteMetadata.importedAssets] + if (getChunkMetadata(chunk.fileName)?.importedAssets.size) { + manifestChunk.assets = [...getChunkMetadata(chunk.fileName)!.importedAssets] } return manifestChunk diff --git a/packages/vite/src/node/plugins/metadata.ts b/packages/vite/src/node/plugins/metadata.ts index 5ef1ae5968ba41..dd000febdfba95 100644 --- a/packages/vite/src/node/plugins/metadata.ts +++ b/packages/vite/src/node/plugins/metadata.ts @@ -1,5 +1,12 @@ import type { Plugin } from '../plugin' +interface ChunkMetadata { + importedAssets: Set + importedCss: Set +} + +const chunkMetadataMap = new Map() + /** * Prepares the rendered chunks to contain additional metadata during build. */ @@ -8,11 +15,15 @@ export function metadataPlugin(): Plugin { name: 'vite:build-metadata', async renderChunk(_code, chunk) { - chunk.viteMetadata = { + chunkMetadataMap.set(chunk.fileName, { importedAssets: new Set(), importedCss: new Set(), - } + }) return null }, } } + +export function getChunkMetadata(fileName: string): ChunkMetadata | undefined { + return chunkMetadataMap.get(fileName) +} diff --git a/packages/vite/src/node/ssr/ssrManifestPlugin.ts b/packages/vite/src/node/ssr/ssrManifestPlugin.ts index c4d8c7b176628d..ebbb604139f5f8 100644 --- a/packages/vite/src/node/ssr/ssrManifestPlugin.ts +++ b/packages/vite/src/node/ssr/ssrManifestPlugin.ts @@ -12,6 +12,7 @@ import { normalizePath, numberToPos, } from '../utils' +import { getChunkMetadata } from '../plugins/metadata' export function ssrManifestPlugin(config: ResolvedConfig): Plugin { // module id => preload assets mapping @@ -32,11 +33,11 @@ export function ssrManifestPlugin(config: ResolvedConfig): Plugin { mappedChunks.push(joinUrlSegments(base, chunk.fileName)) // tags for entry chunks are already generated in static HTML, // so we only need to record info for non-entry chunks. - chunk.viteMetadata!.importedCss.forEach((file) => { + getChunkMetadata(chunk.fileName)!.importedCss.forEach((file) => { mappedChunks.push(joinUrlSegments(base, file)) }) } - chunk.viteMetadata!.importedAssets.forEach((file) => { + getChunkMetadata(chunk.fileName)!.importedAssets.forEach((file) => { mappedChunks.push(joinUrlSegments(base, file)) }) } @@ -73,7 +74,7 @@ export function ssrManifestPlugin(config: ResolvedConfig): Plugin { analyzed.add(filename) const chunk = bundle[filename] as OutputChunk | undefined if (chunk) { - chunk.viteMetadata!.importedCss.forEach((file) => { + getChunkMetadata(chunk.fileName)!.importedCss.forEach((file) => { deps.push(joinUrlSegments(base, file)) // TODO:base }) chunk.imports.forEach(addDeps)