Skip to content

Commit

Permalink
fix(manifest): preserve pure css chunk assets
Browse files Browse the repository at this point in the history
  • Loading branch information
sun0day committed Sep 5, 2023
1 parent 1a2e5e6 commit 787353c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
2 changes: 2 additions & 0 deletions packages/vite/src/node/plugins/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ const assetCache = new WeakMap<ResolvedConfig, Map<string, string>>()
export interface GeneratedAssetMeta {
originalName: string
isEntry?: boolean
// for pure css chunk's assets, e.g: fonts
importedAssets?: Set<string>
}
export const generatedAssets = new WeakMap<
ResolvedConfig,
Expand Down
8 changes: 7 additions & 1 deletion packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
24 changes: 19 additions & 5 deletions packages/vite/src/node/plugins/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ export function manifestPlugin(config: ResolvedConfig): Plugin {
return filteredImports
}

function createChunk(chunk: OutputChunk): ManifestChunk {
function createChunk(
chunk: OutputChunk,
fileNameToAssetMeta: Map<string, GeneratedAssetMeta>,
): ManifestChunk {
const manifestChunk: ManifestChunk = {
file: chunk.fileName,
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 787353c

Please sign in to comment.