@@ -4,7 +4,7 @@ import { manifestPlugin as nativeManifestPlugin } from 'rolldown/experimental'
4
4
import type { Plugin } from '../plugin'
5
5
import { normalizePath , sortObjectKeys } from '../utils'
6
6
import { perEnvironmentState } from '../environment'
7
- import { type ResolvedConfig , perEnvironmentPlugin } from '..'
7
+ import { type Environment , type ResolvedConfig , perEnvironmentPlugin } from '..'
8
8
import { cssEntriesMap } from './asset'
9
9
10
10
const endsWithJSRE = / \. [ c m ] ? j s $ /
@@ -25,6 +25,16 @@ export interface ManifestChunk {
25
25
}
26
26
27
27
export function manifestPlugin ( config : ResolvedConfig ) : Plugin {
28
+ const getState = perEnvironmentState ( ( ) => {
29
+ return {
30
+ manifest : { } as Manifest ,
31
+ outputCount : 0 ,
32
+ reset ( ) {
33
+ this . manifest = { }
34
+ this . outputCount = 0
35
+ } ,
36
+ }
37
+ } )
28
38
if (
29
39
config . build . manifest &&
30
40
config . experimental . enableNativePlugin === true
@@ -38,6 +48,8 @@ export function manifestPlugin(config: ResolvedConfig): Plugin {
38
48
? '.vite/manifest.json'
39
49
: environment . config . build . manifest
40
50
51
+ const isLegacySet = new Set < string > ( )
52
+ const envs : Record < string , Environment > = { }
41
53
function getChunkName ( chunk : OutputChunk ) {
42
54
return (
43
55
getChunkOriginalFileName ( chunk , root , false ) ??
@@ -46,7 +58,36 @@ export function manifestPlugin(config: ResolvedConfig): Plugin {
46
58
}
47
59
48
60
return [
49
- nativeManifestPlugin ( { root, outPath } ) ,
61
+ {
62
+ name : 'native:manifest-envs' ,
63
+ buildStart ( ) {
64
+ envs [ environment . name ] = this . environment
65
+ } ,
66
+ ...( config . isOutputOptionsForLegacyChunks
67
+ ? {
68
+ generateBundle ( opts ) {
69
+ const isLegacy =
70
+ environment . config . isOutputOptionsForLegacyChunks ?.( opts ) ??
71
+ false
72
+ if ( isLegacy ) {
73
+ isLegacySet . add ( environment . name )
74
+ } else {
75
+ isLegacySet . delete ( environment . name )
76
+ }
77
+ } ,
78
+ }
79
+ : { } ) ,
80
+ } ,
81
+ nativeManifestPlugin ( {
82
+ root,
83
+ outPath,
84
+ isLegacy : config . isOutputOptionsForLegacyChunks
85
+ ? ( ) => isLegacySet . has ( environment . name )
86
+ : undefined ,
87
+ cssEntries ( ) {
88
+ return cssEntriesMap . get ( envs [ environment . name ] ) !
89
+ } ,
90
+ } ) ,
50
91
{
51
92
name : 'native:manifest-compatible' ,
52
93
generateBundle ( _ , bundle ) {
@@ -69,27 +110,32 @@ export function manifestPlugin(config: ResolvedConfig): Plugin {
69
110
item . assets = [ ...importedAssets ]
70
111
}
71
112
}
72
- if ( manifest ) {
113
+ const output =
114
+ this . environment . config . build . rolldownOptions . output
115
+ const outputLength = Array . isArray ( output ) ? output . length : 1
116
+ if ( manifest && outputLength === 1 ) {
73
117
asset . source = JSON . stringify ( manifest )
118
+ return
119
+ }
120
+
121
+ const state = getState ( this )
122
+ state . outputCount ++
123
+ state . manifest = Object . assign (
124
+ state . manifest ,
125
+ manifest ?? JSON . parse ( asset . source . toString ( ) ) ,
126
+ )
127
+ if ( state . outputCount >= outputLength ) {
128
+ asset . source = JSON . stringify ( state . manifest , undefined , 2 )
129
+ state . reset ( )
130
+ } else {
131
+ delete bundle [ outPath ]
74
132
}
75
133
}
76
134
} ,
77
135
} ,
78
136
]
79
137
} )
80
138
}
81
-
82
- const getState = perEnvironmentState ( ( ) => {
83
- return {
84
- manifest : { } as Manifest ,
85
- outputCount : 0 ,
86
- reset ( ) {
87
- this . manifest = { }
88
- this . outputCount = 0
89
- } ,
90
- }
91
- } )
92
-
93
139
return {
94
140
name : 'vite:manifest' ,
95
141
0 commit comments