From 4aaf972a5b60e96ac1af3ea6559b1cc1d3a104da Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Wed, 6 Nov 2024 16:24:16 +0900 Subject: [PATCH] chore: partial revert "refactor: use `originalFileNames`/`names` (#18240)" This partially reverts commit f2957c84f69c14c882809889fbd0fc66b97ca3e9. --- packages/vite/src/node/plugins/asset.ts | 17 ++++++-- packages/vite/src/node/plugins/css.ts | 11 +++--- packages/vite/src/node/plugins/html.ts | 3 +- packages/vite/src/node/plugins/manifest.ts | 46 +++++++++++++--------- packages/vite/src/node/plugins/worker.ts | 8 +--- 5 files changed, 48 insertions(+), 37 deletions(-) diff --git a/packages/vite/src/node/plugins/asset.ts b/packages/vite/src/node/plugins/asset.ts index 24dc29ad9f2cc1..92788acd5126c2 100644 --- a/packages/vite/src/node/plugins/asset.ts +++ b/packages/vite/src/node/plugins/asset.ts @@ -43,8 +43,17 @@ const svgExtRE = /\.svg(?:$|\?)/ const assetCache = new WeakMap>() -/** a set of referenceId for entry CSS assets for each environment */ -export const cssEntriesMap = new WeakMap>() +// chunk.name is the basename for the asset ignoring the directory structure +// For the manifest, we need to preserve the original file path and isEntry +// for CSS assets. We keep a map from referenceId to this information. +export interface GeneratedAssetMeta { + originalFileName: string | undefined + isEntry?: boolean +} +export const generatedAssetsMap = new WeakMap< + Environment, + Map +>() // add own dictionary entry by directly assigning mrmime export function registerCustomMime(): void { @@ -144,7 +153,7 @@ export function assetPlugin(config: ResolvedConfig): Plugin { buildStart() { assetCache.set(this.environment, new Map()) - cssEntriesMap.set(this.environment, new Set()) + generatedAssetsMap.set(this.environment, new Map()) }, resolveId(id) { @@ -409,6 +418,8 @@ async function fileToBuiltUrl( originalFileName, source: content, }) + generatedAssetsMap.get(environment)!.set(referenceId, { originalFileName }) + url = `__VITE_ASSET__${referenceId}__${postfix ? `$_${postfix}__` : ``}` } diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index 3e2b4a3f839b7e..8606fcb5d3b2d9 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -90,9 +90,9 @@ import { findNearestPackageData } from '../packages' import { addToHTMLProxyTransformResult } from './html' import { assetUrlRE, - cssEntriesMap, fileToDevUrl, fileToUrl, + generatedAssetsMap, publicAssetUrlCache, publicAssetUrlRE, publicFileToBuiltUrl, @@ -481,9 +481,7 @@ export function cssPostPlugin(config: ResolvedConfig): RolldownPlugin { assetFileNames({ type: 'asset', name: cssAssetName, - names: [cssAssetName], originalFileName: null, - originalFileNames: [], source: '/* vite internal call, ignore */', }), ) @@ -639,6 +637,8 @@ export function cssPostPlugin(config: ResolvedConfig): RolldownPlugin { }, async renderChunk(code, chunk, opts) { + const generatedAssets = generatedAssetsMap.get(this.environment)! + let chunkCSS = '' // the chunk is empty if it's a dynamic entry chunk that only contains a CSS import const isJsChunkEmpty = code === '' && !chunk.isEntry @@ -801,6 +801,7 @@ export function cssPostPlugin(config: ResolvedConfig): RolldownPlugin { originalFileName, source: content, }) + generatedAssets.set(referenceId, { originalFileName }) const filename = this.getFileName(referenceId) getChunkMetadata(chunk)!.importedAssets.add(cleanUrl(filename)) @@ -858,9 +859,7 @@ export function cssPostPlugin(config: ResolvedConfig): RolldownPlugin { originalFileName, source: chunkCSS, }) - if (isEntry) { - cssEntriesMap.get(this.environment)!.add(referenceId) - } + generatedAssets.set(referenceId, { originalFileName, isEntry }) getChunkMetadata(chunk)!.importedCss.add( this.getFileName(referenceId), ) diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts index 65f4990f0ae76e..031755d9f4cbd8 100644 --- a/packages/vite/src/node/plugins/html.ts +++ b/packages/vite/src/node/plugins/html.ts @@ -930,8 +930,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): RolldownPlugin { const cssChunk = cssBundleName && (Object.values(bundle).find( - (chunk) => - chunk.type === 'asset' && chunk.names.includes(cssBundleName), + (chunk) => chunk.type === 'asset' && chunk.name === cssBundleName, ) as OutputAsset | undefined) if (cssChunk) { result = injectToHead(result, [ diff --git a/packages/vite/src/node/plugins/manifest.ts b/packages/vite/src/node/plugins/manifest.ts index 28e603adb9c569..0c267aa3484a85 100644 --- a/packages/vite/src/node/plugins/manifest.ts +++ b/packages/vite/src/node/plugins/manifest.ts @@ -8,8 +8,8 @@ import type { import type { Plugin } from '../plugin' import { normalizePath, sortObjectKeys } from '../utils' import { usePerEnvironmentState } from '../environment' -import { cssEntriesMap } from './asset' import { getChunkMetadata } from './metadata' +import { generatedAssetsMap } from './asset' const endsWithJSRE = /\.[cm]?js$/ @@ -131,15 +131,18 @@ export function manifestPlugin(): Plugin { return manifestChunk } - const entryCssReferenceIds = cssEntriesMap.get(this.environment)! - const entryCssAssetFileNames = new Set(entryCssReferenceIds) - for (const id of entryCssReferenceIds) { - try { - const fileName = this.getFileName(id) - entryCssAssetFileNames.add(fileName) - } catch { - // The asset was generated as part of a different output option. - // It was already handled during the previous run of this plugin. + const assets = generatedAssetsMap.get(this.environment)! + const entryCssAssetFileNames = new Set() + for (const [id, asset] of assets.entries()) { + if (asset.isEntry) { + try { + const fileName = this.getFileName(id) + entryCssAssetFileNames.add(fileName) + } catch { + // The asset was generated as part of a different output option. + // It was already handled during the previous run of this plugin. + assets.delete(id) + } } } @@ -149,24 +152,29 @@ export function manifestPlugin(): Plugin { const chunk = bundle[file] if (chunk.type === 'chunk') { manifest[getChunkName(chunk)] = createChunk(chunk) - } else if (chunk.type === 'asset' && chunk.names.length > 0) { + } else if (chunk.type === 'asset' && typeof chunk.name === 'string') { // Add every unique asset to the manifest, keyed by its original name const src = - chunk.originalFileNames.length > 0 - ? chunk.originalFileNames[0] - : '_' + path.basename(chunk.fileName) + chunk.originalFileName ?? '_' + path.basename(chunk.fileName) const isEntry = entryCssAssetFileNames.has(chunk.fileName) const asset = createAsset(chunk, src, isEntry) // If JS chunk and asset chunk are both generated from the same source file, // prioritize JS chunk as it contains more information const file = manifest[src]?.file - if (!(file && endsWithJSRE.test(file))) { - manifest[src] = asset - fileNameToAsset.set(chunk.fileName, asset) - } + if (file && endsWithJSRE.test(file)) continue + + manifest[src] = asset + fileNameToAsset.set(chunk.fileName, asset) + } + } - for (const originalFileName of chunk.originalFileNames.slice(1)) { + // Add deduplicated assets to the manifest + for (const [referenceId, { originalFileName }] of assets.entries()) { + if (originalFileName && !manifest[originalFileName]) { + const fileName = this.getFileName(referenceId) + const asset = fileNameToAsset.get(fileName) + if (asset) { manifest[originalFileName] = asset } } diff --git a/packages/vite/src/node/plugins/worker.ts b/packages/vite/src/node/plugins/worker.ts index dc44624f05d694..7c72d72b3c3f9e 100644 --- a/packages/vite/src/node/plugins/worker.ts +++ b/packages/vite/src/node/plugins/worker.ts @@ -23,9 +23,7 @@ import { fileToUrl } from './asset' type WorkerBundleAsset = { fileName: string - /** @deprecated */ originalFileName: string | null - originalFileNames: string[] source: string | Uint8Array } @@ -129,7 +127,6 @@ async function bundleWorkerEntry( saveEmitWorkerAsset(config, { fileName: outputChunk.fileName, originalFileName: null, - originalFileNames: [], source: outputChunk.code, }) } @@ -168,7 +165,6 @@ function emitSourcemapForWorkerEntry( saveEmitWorkerAsset(config, { fileName: mapFileName, originalFileName: null, - originalFileNames: [], source: data, }) } @@ -203,7 +199,6 @@ export async function workerFileToUrl( saveEmitWorkerAsset(config, { fileName, originalFileName: null, - originalFileNames: [], source: outputChunk.code, }) workerMap.bundle.set(id, fileName) @@ -499,9 +494,8 @@ export function webWorkerPlugin(config: ResolvedConfig): RolldownPlugin { this.emitFile({ type: 'asset', fileName: asset.fileName, + originalFileName: asset.originalFileName, source: asset.source, - // NOTE: fileName is already generated when bundling the worker - // so no need to pass originalFileNames/names }) }) workerMap.assets.clear()