From 787353c0e10531b108eb5b9e74346962eed7df45 Mon Sep 17 00:00:00 2001 From: sun0day Date: Tue, 5 Sep 2023 18:03:44 +0800 Subject: [PATCH 1/7] fix(manifest): preserve pure css chunk assets --- packages/vite/src/node/plugins/asset.ts | 2 ++ packages/vite/src/node/plugins/css.ts | 8 +++++++- packages/vite/src/node/plugins/manifest.ts | 24 +++++++++++++++++----- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/packages/vite/src/node/plugins/asset.ts b/packages/vite/src/node/plugins/asset.ts index 9a91234f9378f6..c99694d75a993e 100644 --- a/packages/vite/src/node/plugins/asset.ts +++ b/packages/vite/src/node/plugins/asset.ts @@ -42,6 +42,8 @@ const assetCache = new WeakMap>() export interface GeneratedAssetMeta { originalName: string isEntry?: boolean + // for pure css chunk's assets, e.g: fonts + importedAssets?: Set } export const generatedAssets = new WeakMap< ResolvedConfig, diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index a9fcf4a50c13aa..6fef458650a765 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -653,7 +653,13 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin { const originalName = isPreProcessor(lang) ? cssAssetName : cssFileName generatedAssets .get(config)! - .set(referenceId, { originalName, isEntry }) + .set(referenceId, { + originalName, + isEntry, + importedAssets: isPureCssChunk + ? chunk.viteMetadata?.importedAssets + : undefined, + }) chunk.viteMetadata!.importedCss.add(this.getFileName(referenceId)) if (emitTasksLength === emitTasks.length) { diff --git a/packages/vite/src/node/plugins/manifest.ts b/packages/vite/src/node/plugins/manifest.ts index 0950121c0bcbe2..e0ff1e901ced9d 100644 --- a/packages/vite/src/node/plugins/manifest.ts +++ b/packages/vite/src/node/plugins/manifest.ts @@ -63,7 +63,10 @@ export function manifestPlugin(config: ResolvedConfig): Plugin { return filteredImports } - function createChunk(chunk: OutputChunk): ManifestChunk { + function createChunk( + chunk: OutputChunk, + fileNameToAssetMeta: Map, + ): ManifestChunk { const manifestChunk: ManifestChunk = { file: chunk.fileName, } @@ -92,13 +95,21 @@ export function manifestPlugin(config: ResolvedConfig): Plugin { } } - if (chunk.viteMetadata?.importedCss.size) { - manifestChunk.css = [...chunk.viteMetadata.importedCss] - } if (chunk.viteMetadata?.importedAssets.size) { manifestChunk.assets = [...chunk.viteMetadata.importedAssets] } + if (chunk.viteMetadata?.importedCss.size) { + manifestChunk.css = [...chunk.viteMetadata.importedCss] + manifestChunk.css.forEach((cssFile) => { + const asset = fileNameToAssetMeta.get(cssFile) + if (asset?.importedAssets) { + manifestChunk.assets ||= [] + manifestChunk.assets.push(...asset.importedAssets) + } + }) + } + return manifestChunk } @@ -127,7 +138,10 @@ export function manifestPlugin(config: ResolvedConfig): Plugin { for (const file in bundle) { const chunk = bundle[file] if (chunk.type === 'chunk') { - manifest[getChunkName(chunk)] = createChunk(chunk) + manifest[getChunkName(chunk)] = createChunk( + chunk, + fileNameToAssetMeta, + ) } else if (chunk.type === 'asset' && typeof chunk.name === 'string') { // Add every unique asset to the manifest, keyed by its original name const assetMeta = fileNameToAssetMeta.get(chunk.fileName) From 66a39292eda0f756096429f3c9e024c3c09dc25b Mon Sep 17 00:00:00 2001 From: sun0day Date: Tue, 5 Sep 2023 18:55:08 +0800 Subject: [PATCH 2/7] fix: dedupe assets --- packages/vite/src/node/plugins/manifest.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/vite/src/node/plugins/manifest.ts b/packages/vite/src/node/plugins/manifest.ts index e0ff1e901ced9d..dc8588951ea76c 100644 --- a/packages/vite/src/node/plugins/manifest.ts +++ b/packages/vite/src/node/plugins/manifest.ts @@ -95,8 +95,10 @@ export function manifestPlugin(config: ResolvedConfig): Plugin { } } + let assetSet: Set | null = null + if (chunk.viteMetadata?.importedAssets.size) { - manifestChunk.assets = [...chunk.viteMetadata.importedAssets] + assetSet = chunk.viteMetadata.importedAssets } if (chunk.viteMetadata?.importedCss.size) { @@ -104,12 +106,15 @@ export function manifestPlugin(config: ResolvedConfig): Plugin { manifestChunk.css.forEach((cssFile) => { const asset = fileNameToAssetMeta.get(cssFile) if (asset?.importedAssets) { - manifestChunk.assets ||= [] - manifestChunk.assets.push(...asset.importedAssets) + assetSet = new Set([...(assetSet || []), ...asset.importedAssets]) } }) } + if (assetSet?.size) { + manifestChunk.assets = [...assetSet] + } + return manifestChunk } From d18387165cd3eac594fefbd62ec0ab2e7c1d3456 Mon Sep 17 00:00:00 2001 From: sun0day Date: Tue, 5 Sep 2023 19:10:48 +0800 Subject: [PATCH 3/7] chore: prettier --- packages/vite/src/node/plugins/css.ts | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index 6fef458650a765..f20086fcc13c4c 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -651,15 +651,13 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin { source: chunkCSS, }) const originalName = isPreProcessor(lang) ? cssAssetName : cssFileName - generatedAssets - .get(config)! - .set(referenceId, { - originalName, - isEntry, - importedAssets: isPureCssChunk - ? chunk.viteMetadata?.importedAssets - : undefined, - }) + generatedAssets.get(config)!.set(referenceId, { + originalName, + isEntry, + importedAssets: isPureCssChunk + ? chunk.viteMetadata?.importedAssets + : undefined, + }) chunk.viteMetadata!.importedCss.add(this.getFileName(referenceId)) if (emitTasksLength === emitTasks.length) { From e0963d599528786f4b40ea3c58d01c8e87a2b68e Mon Sep 17 00:00:00 2001 From: sun0day Date: Thu, 7 Sep 2023 12:25:23 +0800 Subject: [PATCH 4/7] Revert "fix: dedupe assets" This reverts commit 66a39292eda0f756096429f3c9e024c3c09dc25b. --- packages/vite/src/node/plugins/manifest.ts | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/packages/vite/src/node/plugins/manifest.ts b/packages/vite/src/node/plugins/manifest.ts index dc8588951ea76c..e0ff1e901ced9d 100644 --- a/packages/vite/src/node/plugins/manifest.ts +++ b/packages/vite/src/node/plugins/manifest.ts @@ -95,10 +95,8 @@ export function manifestPlugin(config: ResolvedConfig): Plugin { } } - let assetSet: Set | null = null - if (chunk.viteMetadata?.importedAssets.size) { - assetSet = chunk.viteMetadata.importedAssets + manifestChunk.assets = [...chunk.viteMetadata.importedAssets] } if (chunk.viteMetadata?.importedCss.size) { @@ -106,15 +104,12 @@ export function manifestPlugin(config: ResolvedConfig): Plugin { manifestChunk.css.forEach((cssFile) => { const asset = fileNameToAssetMeta.get(cssFile) if (asset?.importedAssets) { - assetSet = new Set([...(assetSet || []), ...asset.importedAssets]) + manifestChunk.assets ||= [] + manifestChunk.assets.push(...asset.importedAssets) } }) } - if (assetSet?.size) { - manifestChunk.assets = [...assetSet] - } - return manifestChunk } From c6f938db47fe2d9101743b3651881e1fc6ed4991 Mon Sep 17 00:00:00 2001 From: sun0day Date: Thu, 7 Sep 2023 12:26:04 +0800 Subject: [PATCH 5/7] Revert "chore: prettier" This reverts commit d18387165cd3eac594fefbd62ec0ab2e7c1d3456. --- packages/vite/src/node/plugins/css.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index f20086fcc13c4c..6fef458650a765 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -651,13 +651,15 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin { source: chunkCSS, }) const originalName = isPreProcessor(lang) ? cssAssetName : cssFileName - generatedAssets.get(config)!.set(referenceId, { - originalName, - isEntry, - importedAssets: isPureCssChunk - ? chunk.viteMetadata?.importedAssets - : undefined, - }) + generatedAssets + .get(config)! + .set(referenceId, { + originalName, + isEntry, + importedAssets: isPureCssChunk + ? chunk.viteMetadata?.importedAssets + : undefined, + }) chunk.viteMetadata!.importedCss.add(this.getFileName(referenceId)) if (emitTasksLength === emitTasks.length) { From 560e3c14e07c6572c5cf12648e2ea2395d5ac239 Mon Sep 17 00:00:00 2001 From: sun0day Date: Thu, 7 Sep 2023 12:26:09 +0800 Subject: [PATCH 6/7] Revert "fix(manifest): preserve pure css chunk assets" This reverts commit 787353c0e10531b108eb5b9e74346962eed7df45. --- packages/vite/src/node/plugins/asset.ts | 2 -- packages/vite/src/node/plugins/css.ts | 8 +------- packages/vite/src/node/plugins/manifest.ts | 24 +++++----------------- 3 files changed, 6 insertions(+), 28 deletions(-) diff --git a/packages/vite/src/node/plugins/asset.ts b/packages/vite/src/node/plugins/asset.ts index c99694d75a993e..9a91234f9378f6 100644 --- a/packages/vite/src/node/plugins/asset.ts +++ b/packages/vite/src/node/plugins/asset.ts @@ -42,8 +42,6 @@ const assetCache = new WeakMap>() export interface GeneratedAssetMeta { originalName: string isEntry?: boolean - // for pure css chunk's assets, e.g: fonts - importedAssets?: Set } export const generatedAssets = new WeakMap< ResolvedConfig, diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index 6fef458650a765..a9fcf4a50c13aa 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -653,13 +653,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin { const originalName = isPreProcessor(lang) ? cssAssetName : cssFileName generatedAssets .get(config)! - .set(referenceId, { - originalName, - isEntry, - importedAssets: isPureCssChunk - ? chunk.viteMetadata?.importedAssets - : undefined, - }) + .set(referenceId, { originalName, isEntry }) chunk.viteMetadata!.importedCss.add(this.getFileName(referenceId)) if (emitTasksLength === emitTasks.length) { diff --git a/packages/vite/src/node/plugins/manifest.ts b/packages/vite/src/node/plugins/manifest.ts index e0ff1e901ced9d..0950121c0bcbe2 100644 --- a/packages/vite/src/node/plugins/manifest.ts +++ b/packages/vite/src/node/plugins/manifest.ts @@ -63,10 +63,7 @@ export function manifestPlugin(config: ResolvedConfig): Plugin { return filteredImports } - function createChunk( - chunk: OutputChunk, - fileNameToAssetMeta: Map, - ): ManifestChunk { + function createChunk(chunk: OutputChunk): ManifestChunk { const manifestChunk: ManifestChunk = { file: chunk.fileName, } @@ -95,19 +92,11 @@ export function manifestPlugin(config: ResolvedConfig): Plugin { } } - if (chunk.viteMetadata?.importedAssets.size) { - manifestChunk.assets = [...chunk.viteMetadata.importedAssets] - } - if (chunk.viteMetadata?.importedCss.size) { manifestChunk.css = [...chunk.viteMetadata.importedCss] - manifestChunk.css.forEach((cssFile) => { - const asset = fileNameToAssetMeta.get(cssFile) - if (asset?.importedAssets) { - manifestChunk.assets ||= [] - manifestChunk.assets.push(...asset.importedAssets) - } - }) + } + if (chunk.viteMetadata?.importedAssets.size) { + manifestChunk.assets = [...chunk.viteMetadata.importedAssets] } return manifestChunk @@ -138,10 +127,7 @@ export function manifestPlugin(config: ResolvedConfig): Plugin { for (const file in bundle) { const chunk = bundle[file] if (chunk.type === 'chunk') { - manifest[getChunkName(chunk)] = createChunk( - chunk, - fileNameToAssetMeta, - ) + manifest[getChunkName(chunk)] = createChunk(chunk) } else if (chunk.type === 'asset' && typeof chunk.name === 'string') { // Add every unique asset to the manifest, keyed by its original name const assetMeta = fileNameToAssetMeta.get(chunk.fileName) From 4b3eb42ed5312719024c11ba7c0bf8ecd98918c8 Mon Sep 17 00:00:00 2001 From: sun0day Date: Thu, 7 Sep 2023 12:35:07 +0800 Subject: [PATCH 7/7] fix: add imported assets --- packages/vite/src/node/plugins/css.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index a9fcf4a50c13aa..16c9112edbfe5d 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -766,11 +766,15 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin { // chunks instead. chunk.imports = chunk.imports.filter((file) => { if (pureCssChunkNames.includes(file)) { - const { importedCss } = (bundle[file] as OutputChunk) - .viteMetadata! + const { importedCss, importedAssets } = ( + bundle[file] as OutputChunk + ).viteMetadata! importedCss.forEach((file) => chunk.viteMetadata!.importedCss.add(file), ) + importedAssets.forEach((file) => + chunk.viteMetadata!.importedAssets.add(file), + ) return false } return true