1
- import type { OutputBundle } from 'rollup'
1
+ import type { OutputAsset , OutputBundle , PluginContext } from 'rollup'
2
2
import type { PWAPluginContext } from './context'
3
3
import type { ExtendManifestEntriesHook , VitePluginPWAAPI } from './types'
4
4
import { existsSync } from 'node:fs'
@@ -24,7 +24,7 @@ export async function _generateSW({ options, version, viteConfig }: PWAPluginCon
24
24
await generateServiceWorker ( version , options , viteConfig )
25
25
}
26
26
27
- export function _generateBundle ( ctx : PWAPluginContext , bundle ?: OutputBundle ) {
27
+ export function _generateBundle ( ctx : PWAPluginContext , bundle ?: OutputBundle , pluginCtx ?: PluginContext ) {
28
28
const { options, viteConfig, useImportRegister } = ctx
29
29
if ( options . disable || ! bundle )
30
30
return
@@ -37,37 +37,49 @@ export function _generateBundle(ctx: PWAPluginContext, bundle?: OutputBundle) {
37
37
`${ yellow ( 'WARNING: "theme_color" is missing from the web manifest, your application will not be able to be installed' ) } ` ,
38
38
] . join ( '\n' ) )
39
39
}
40
- bundle [ options . manifestFilename ] = {
41
- // @ts -expect-error: for Vite 3 support, Vite 4 has removed `isAsset` property
42
- isAsset : true ,
43
- type : 'asset' ,
44
- // vite 6 deprecation: replaced with names
45
- name : undefined ,
46
- // fix vite 6 build with manifest enabled
47
- names : [ ] ,
48
- source : generateWebManifestFile ( options ) ,
40
+ emitFile ( {
49
41
fileName : options . manifestFilename ,
50
- }
42
+ source : generateWebManifestFile ( options ) ,
43
+ } , bundle , pluginCtx )
51
44
}
52
45
53
46
// if virtual register is requested, do not inject.
54
47
if ( options . injectRegister === 'auto' )
55
48
options . injectRegister = useImportRegister ? false : 'script'
56
49
57
50
if ( ( options . injectRegister === 'script' || options . injectRegister === 'script-defer' ) && ! existsSync ( resolve ( viteConfig . publicDir , FILE_SW_REGISTER ) ) ) {
58
- bundle [ FILE_SW_REGISTER ] = {
51
+ emitFile ( {
52
+ fileName : FILE_SW_REGISTER ,
53
+ source : generateSimpleSWRegister ( options , false ) ,
54
+ } , bundle , pluginCtx )
55
+ }
56
+ return bundle
57
+ }
58
+
59
+ function emitFile ( asset : Pick < OutputAsset , 'fileName' | 'source' > , bundle : OutputBundle , pluginCtx ?: PluginContext ) {
60
+ if ( pluginCtx ) {
61
+ pluginCtx . emitFile ( {
62
+ type : 'asset' ,
63
+ fileName : asset . fileName ,
64
+ source : asset . source ,
65
+ } )
66
+ }
67
+ else {
68
+ // NOTE: assigning to bundle[foo] directly is discouraged by rollup
69
+ // and is not supported by rolldown.
70
+ // The api consumers should pass in the pluginCtx in the future
71
+ bundle [ asset . fileName ] = {
59
72
// @ts -expect-error: for Vite 3 support, Vite 4 has removed `isAsset` property
60
73
isAsset : true ,
61
74
type : 'asset' ,
62
75
// vite 6 deprecation: replaced with names
63
76
name : undefined ,
64
77
// fix vite 6 build with manifest enabled
65
78
names : [ ] ,
66
- source : generateSimpleSWRegister ( options , false ) ,
67
- fileName : FILE_SW_REGISTER ,
79
+ source : asset . source ,
80
+ fileName : asset . fileName ,
68
81
}
69
82
}
70
- return bundle
71
83
}
72
84
73
85
export function createAPI ( ctx : PWAPluginContext ) {
@@ -142,8 +154,8 @@ export function createAPI(ctx: PWAPluginContext) {
142
154
} ,
143
155
}
144
156
} ,
145
- generateBundle ( bundle ) {
146
- return _generateBundle ( ctx , bundle )
157
+ generateBundle ( bundle , pluginCtx ) {
158
+ return _generateBundle ( ctx , bundle , pluginCtx )
147
159
} ,
148
160
async generateSW ( ) {
149
161
return await _generateSW ( ctx )
0 commit comments