Skip to content

Commit 42ecf37

Browse files
authored
feat: include duplicate assets in the manifest (#9928)
1 parent a543731 commit 42ecf37

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

packages/vite/src/node/plugins/asset.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Buffer } from 'node:buffer'
55
import * as mrmime from 'mrmime'
66
import type {
77
NormalizedOutputOptions,
8+
OutputAsset,
89
OutputOptions,
910
PluginContext,
1011
PreRenderedAsset,
@@ -20,6 +21,11 @@ import { FS_PREFIX } from '../constants'
2021

2122
export const assetUrlRE = /__VITE_ASSET__([a-z\d]{8})__(?:\$_(.*?)__)?/g
2223

24+
export const duplicateAssets = new WeakMap<
25+
ResolvedConfig,
26+
Map<string, OutputAsset>
27+
>()
28+
2329
const rawRE = /(\?|&)raw(?:&|$)/
2430
const urlRE = /(\?|&)url(?:&|$)/
2531

@@ -129,6 +135,7 @@ export function assetPlugin(config: ResolvedConfig): Plugin {
129135
buildStart() {
130136
assetCache.set(config, new Map())
131137
emittedHashMap.set(config, new Set())
138+
duplicateAssets.set(config, new Map())
132139
},
133140

134141
resolveId(id) {
@@ -470,15 +477,24 @@ async function fileToBuiltUrl(
470477
map.set(contentHash, fileName)
471478
}
472479
const emittedSet = emittedHashMap.get(config)!
480+
const duplicates = duplicateAssets.get(config)!
481+
const name = normalizePath(path.relative(config.root, file))
473482
if (!emittedSet.has(contentHash)) {
474-
const name = normalizePath(path.relative(config.root, file))
475483
pluginContext.emitFile({
476484
name,
477485
fileName,
478486
type: 'asset',
479487
source: content
480488
})
481489
emittedSet.add(contentHash)
490+
} else {
491+
duplicates.set(name, {
492+
name,
493+
fileName: map.get(contentHash)!,
494+
type: 'asset',
495+
source: content,
496+
isAsset: true
497+
})
482498
}
483499

484500
url = `__VITE_ASSET__${contentHash}__${postfix ? `$_${postfix}__` : ``}` // TODO_BASE

packages/vite/src/node/plugins/manifest.ts

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { ResolvedConfig } from '..'
44
import type { Plugin } from '../plugin'
55
import { normalizePath } from '../utils'
66
import { cssEntryFilesCache } from './css'
7+
import { duplicateAssets } from './asset'
78

89
export type Manifest = Record<string, ManifestChunk>
910

@@ -122,6 +123,11 @@ export function manifestPlugin(config: ResolvedConfig): Plugin {
122123
}
123124
}
124125

126+
duplicateAssets.get(config)!.forEach((asset) => {
127+
const chunk = createAsset(asset)
128+
manifest[asset.name!] = chunk
129+
})
130+
125131
outputCount++
126132
const output = config.build.rollupOptions?.output
127133
const outputLength = Array.isArray(output) ? output.length : 1

0 commit comments

Comments
 (0)