@@ -5,7 +5,6 @@ import { Buffer } from 'node:buffer'
5
5
import * as mrmime from 'mrmime'
6
6
import type {
7
7
NormalizedOutputOptions ,
8
- OutputAsset ,
9
8
OutputOptions ,
10
9
PluginContext ,
11
10
PreRenderedAsset ,
@@ -22,24 +21,19 @@ import type { ResolvedConfig } from '../config'
22
21
import { cleanUrl , getHash , joinUrlSegments , normalizePath } from '../utils'
23
22
import { FS_PREFIX } from '../constants'
24
23
25
- export const assetUrlRE = / _ _ V I T E _ A S S E T _ _ ( [ a - z \d ] { 8 } ) _ _ (?: \$ _ ( .* ?) _ _ ) ? / g
26
-
27
- export const duplicateAssets = new WeakMap <
28
- ResolvedConfig ,
29
- Map < string , OutputAsset >
30
- > ( )
24
+ export const assetUrlRE = / _ _ V I T E _ A S S E T _ _ ( [ a - z \d ] + ) _ _ (?: \$ _ ( .* ?) _ _ ) ? / g
31
25
32
26
const rawRE = / ( \? | & ) r a w (?: & | $ ) /
33
27
const urlRE = / ( \? | & ) u r l (?: & | $ ) /
34
28
35
29
const assetCache = new WeakMap < ResolvedConfig , Map < string , string > > ( )
36
30
37
- const assetHashToFilenameMap = new WeakMap <
31
+ // chunk.name is the basename for the asset ignoring the directory structure
32
+ // For the manifest, we need to preserve the original file path
33
+ export const generatedAssets = new WeakMap <
38
34
ResolvedConfig ,
39
- Map < string , string >
35
+ Map < string , { originalName : string } >
40
36
> ( )
41
- // save hashes of the files that has been emitted in build watch
42
- const emittedHashMap = new WeakMap < ResolvedConfig , Set < string > > ( )
43
37
44
38
// add own dictionary entry by directly assigning mrmime
45
39
export function registerCustomMime ( ) : void {
@@ -77,10 +71,8 @@ export function renderAssetUrlInJS(
77
71
78
72
while ( ( match = assetUrlRE . exec ( code ) ) ) {
79
73
s ||= new MagicString ( code )
80
- const [ full , hash , postfix = '' ] = match
81
- // some internal plugins may still need to emit chunks (e.g. worker) so
82
- // fallback to this.getFileName for that. TODO: remove, not needed
83
- const file = getAssetFilename ( hash , config ) || ctx . getFileName ( hash )
74
+ const [ full , referenceId , postfix = '' ] = match
75
+ const file = ctx . getFileName ( referenceId )
84
76
chunk . viteMetadata . importedAssets . add ( cleanUrl ( file ) )
85
77
const filename = file + postfix
86
78
const replacement = toOutputFilePathInJS (
@@ -127,18 +119,14 @@ export function renderAssetUrlInJS(
127
119
* Also supports loading plain strings with import text from './foo.txt?raw'
128
120
*/
129
121
export function assetPlugin ( config : ResolvedConfig ) : Plugin {
130
- // assetHashToFilenameMap initialization in buildStart causes getAssetFilename to return undefined
131
- assetHashToFilenameMap . set ( config , new Map ( ) )
132
-
133
122
registerCustomMime ( )
134
123
135
124
return {
136
125
name : 'vite:asset' ,
137
126
138
127
buildStart ( ) {
139
128
assetCache . set ( config , new Map ( ) )
140
- emittedHashMap . set ( config , new Set ( ) )
141
- duplicateAssets . set ( config , new Map ( ) )
129
+ generatedAssets . set ( config , new Map ( ) )
142
130
} ,
143
131
144
132
resolveId ( id ) {
@@ -253,13 +241,6 @@ function fileToDevUrl(id: string, config: ResolvedConfig) {
253
241
return joinUrlSegments ( base , rtn . replace ( / ^ \/ / , '' ) )
254
242
}
255
243
256
- export function getAssetFilename (
257
- hash : string ,
258
- config : ResolvedConfig
259
- ) : string | undefined {
260
- return assetHashToFilenameMap . get ( config ) ?. get ( hash )
261
- }
262
-
263
244
export function getPublicAssetFilename (
264
245
hash : string ,
265
246
config : ResolvedConfig
@@ -458,47 +439,20 @@ async function fileToBuiltUrl(
458
439
url = `data:${ mimeType } ;base64,${ content . toString ( 'base64' ) } `
459
440
} else {
460
441
// emit as asset
461
- // rollup supports `import.meta.ROLLUP_FILE_URL_*`, but it generates code
462
- // that uses runtime url sniffing and it can be verbose when targeting
463
- // non-module format. It also fails to cascade the asset content change
464
- // into the chunk's hash, so we have to do our own content hashing here.
465
- // https://bundlers.tooling.report/hashing/asset-cascade/
466
- // https://github.com/rollup/rollup/issues/3415
467
- const map = assetHashToFilenameMap . get ( config ) !
468
- const contentHash = getHash ( content )
469
442
const { search, hash } = parseUrl ( id )
470
443
const postfix = ( search || '' ) + ( hash || '' )
471
444
472
- const fileName = assetFileNamesToFileName (
473
- resolveAssetFileNames ( config ) ,
474
- file ,
475
- contentHash ,
476
- content
477
- )
478
- if ( ! map . has ( contentHash ) ) {
479
- map . set ( contentHash , fileName )
480
- }
481
- const emittedSet = emittedHashMap . get ( config ) !
482
- const duplicates = duplicateAssets . get ( config ) !
483
- const name = normalizePath ( path . relative ( config . root , file ) )
484
- if ( ! emittedSet . has ( contentHash ) ) {
485
- pluginContext . emitFile ( {
486
- name,
487
- fileName,
488
- type : 'asset' ,
489
- source : content
490
- } )
491
- emittedSet . add ( contentHash )
492
- } else {
493
- duplicates . set ( name , {
494
- name,
495
- fileName : map . get ( contentHash ) ! ,
496
- type : 'asset' ,
497
- source : content
498
- } )
499
- }
445
+ const referenceId = pluginContext . emitFile ( {
446
+ // Ignore directory structure for asset file names
447
+ name : path . basename ( file ) ,
Has a conversation. Original line has a conversation.
448
+ type : 'asset' ,
449
+ source : content
450
+ } )
451
+
452
+ const originalName = normalizePath ( path . relative ( config . root , file ) )
453
+ generatedAssets . get ( config ) ! . set ( referenceId , { originalName } )
500
454
501
- url = `__VITE_ASSET__${ contentHash } __${ postfix ? `$_${ postfix } __` : `` } ` // TODO_BASE
455
+ url = `__VITE_ASSET__${ referenceId } __${ postfix ? `$_${ postfix } __` : `` } ` // TODO_BASE
502
456
}
503
457
504
458
cache . set ( id , url )