Skip to content

Commit b07167f

Browse files
authored
fix: avoid assigning to bundle object (#843)
* fix: avoid assigning to bundle object * fix: avoid assign for FILE_SW_REGISTER as well
1 parent 11923f3 commit b07167f

File tree

3 files changed

+33
-21
lines changed

3 files changed

+33
-21
lines changed

src/api.ts

+30-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { OutputBundle } from 'rollup'
1+
import type { OutputAsset, OutputBundle, PluginContext } from 'rollup'
22
import type { PWAPluginContext } from './context'
33
import type { ExtendManifestEntriesHook, VitePluginPWAAPI } from './types'
44
import { existsSync } from 'node:fs'
@@ -24,7 +24,7 @@ export async function _generateSW({ options, version, viteConfig }: PWAPluginCon
2424
await generateServiceWorker(version, options, viteConfig)
2525
}
2626

27-
export function _generateBundle(ctx: PWAPluginContext, bundle?: OutputBundle) {
27+
export function _generateBundle(ctx: PWAPluginContext, bundle?: OutputBundle, pluginCtx?: PluginContext) {
2828
const { options, viteConfig, useImportRegister } = ctx
2929
if (options.disable || !bundle)
3030
return
@@ -37,37 +37,49 @@ export function _generateBundle(ctx: PWAPluginContext, bundle?: OutputBundle) {
3737
`${yellow('WARNING: "theme_color" is missing from the web manifest, your application will not be able to be installed')}`,
3838
].join('\n'))
3939
}
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({
4941
fileName: options.manifestFilename,
50-
}
42+
source: generateWebManifestFile(options),
43+
}, bundle, pluginCtx)
5144
}
5245

5346
// if virtual register is requested, do not inject.
5447
if (options.injectRegister === 'auto')
5548
options.injectRegister = useImportRegister ? false : 'script'
5649

5750
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] = {
5972
// @ts-expect-error: for Vite 3 support, Vite 4 has removed `isAsset` property
6073
isAsset: true,
6174
type: 'asset',
6275
// vite 6 deprecation: replaced with names
6376
name: undefined,
6477
// fix vite 6 build with manifest enabled
6578
names: [],
66-
source: generateSimpleSWRegister(options, false),
67-
fileName: FILE_SW_REGISTER,
79+
source: asset.source,
80+
fileName: asset.fileName,
6881
}
6982
}
70-
return bundle
7183
}
7284

7385
export function createAPI(ctx: PWAPluginContext) {
@@ -142,8 +154,8 @@ export function createAPI(ctx: PWAPluginContext) {
142154
},
143155
}
144156
},
145-
generateBundle(bundle) {
146-
return _generateBundle(ctx, bundle)
157+
generateBundle(bundle, pluginCtx) {
158+
return _generateBundle(ctx, bundle, pluginCtx)
147159
},
148160
async generateSW() {
149161
return await _generateSW(ctx)

src/plugins/build.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function BuildPlugin(ctx: PWAPluginContext) {
3535
if (pwaAssetsGenerator)
3636
pwaAssetsGenerator.injectManifestIcons()
3737

38-
return _generateBundle(ctx, bundle)
38+
return _generateBundle(ctx, bundle, this)
3939
},
4040
closeBundle: {
4141
sequential: true,

src/types.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { HtmlLinkPreset } from '@vite-pwa/assets-generator/api'
22
import type { BuiltInPreset, Preset } from '@vite-pwa/assets-generator/config'
3-
import type { OutputBundle, RollupOptions } from 'rollup'
3+
import type { OutputBundle, PluginContext, RollupOptions } from 'rollup'
44
import type { BuildOptions, InlineConfig, Plugin, ResolvedConfig, UserConfig } from 'vite'
55
import type { GenerateSWOptions, InjectManifestOptions, ManifestEntry } from 'workbox-build'
66
import type { PWAAssetsGenerator } from './pwa-assets/types'
@@ -706,7 +706,7 @@ export interface VitePluginPWAAPI {
706706
/*
707707
* Explicitly generate the manifests.
708708
*/
709-
generateBundle: (bundle?: OutputBundle) => OutputBundle | undefined
709+
generateBundle: (bundle?: OutputBundle, pluginCtx?: PluginContext) => OutputBundle | undefined
710710
/*
711711
* Explicitly generate the PWA services worker.
712712
*/

0 commit comments

Comments
 (0)