@@ -5,6 +5,7 @@ import { Buffer } from 'node:buffer'
5
5
import * as mrmime from 'mrmime'
6
6
import type {
7
7
NormalizedOutputOptions ,
8
+ OutputAsset ,
8
9
OutputOptions ,
9
10
PluginContext ,
10
11
PreRenderedAsset ,
@@ -20,6 +21,11 @@ import { FS_PREFIX } from '../constants'
20
21
21
22
export const assetUrlRE = / _ _ V I T E _ A S S E T _ _ ( [ a - z \d ] { 8 } ) _ _ (?: \$ _ ( .* ?) _ _ ) ? / g
22
23
24
+ export const duplicateAssets = new WeakMap <
25
+ ResolvedConfig ,
26
+ Map < string , OutputAsset >
27
+ > ( )
28
+
23
29
const rawRE = / ( \? | & ) r a w (?: & | $ ) /
24
30
const urlRE = / ( \? | & ) u r l (?: & | $ ) /
25
31
@@ -129,6 +135,7 @@ export function assetPlugin(config: ResolvedConfig): Plugin {
129
135
buildStart ( ) {
130
136
assetCache . set ( config , new Map ( ) )
131
137
emittedHashMap . set ( config , new Set ( ) )
138
+ duplicateAssets . set ( config , new Map ( ) )
132
139
} ,
133
140
134
141
resolveId ( id ) {
@@ -470,15 +477,24 @@ async function fileToBuiltUrl(
470
477
map . set ( contentHash , fileName )
471
478
}
472
479
const emittedSet = emittedHashMap . get ( config ) !
480
+ const duplicates = duplicateAssets . get ( config ) !
481
+ const name = normalizePath ( path . relative ( config . root , file ) )
473
482
if ( ! emittedSet . has ( contentHash ) ) {
474
- const name = normalizePath ( path . relative ( config . root , file ) )
475
483
pluginContext . emitFile ( {
476
484
name,
477
485
fileName,
478
486
type : 'asset' ,
479
487
source : content
480
488
} )
481
489
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
+ } )
482
498
}
483
499
484
500
url = `__VITE_ASSET__${ contentHash } __${ postfix ? `$_${ postfix } __` : `` } ` // TODO_BASE
0 commit comments