Skip to content

Commit 6ebab07

Browse files
committed
fix: avoid assign for FILE_SW_REGISTER as well
1 parent 5edac5f commit 6ebab07

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

src/api.ts

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { OutputBundle, PluginContext } 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'
@@ -37,49 +37,49 @@ export function _generateBundle(ctx: PWAPluginContext, bundle?: OutputBundle, pl
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-
if (pluginCtx) {
41-
pluginCtx.emitFile({
42-
type: 'asset',
43-
fileName: options.manifestFilename,
44-
source: generateWebManifestFile(options),
45-
})
46-
}
47-
else {
48-
// NOTE: assigning to bundle[foo] directly is discouraged by rollup
49-
// and is not supported by rolldown.
50-
// The api consumers should pass in the pluginCtx in the future
51-
bundle[options.manifestFilename] = {
52-
// @ts-expect-error: for Vite 3 support, Vite 4 has removed `isAsset` property
53-
isAsset: true,
54-
type: 'asset',
55-
// vite 6 deprecation: replaced with names
56-
name: undefined,
57-
// fix vite 6 build with manifest enabled
58-
names: [],
59-
source: generateWebManifestFile(options),
60-
fileName: options.manifestFilename,
61-
}
62-
}
40+
emitFile({
41+
fileName: options.manifestFilename,
42+
source: generateWebManifestFile(options),
43+
}, bundle, pluginCtx)
6344
}
6445

6546
// if virtual register is requested, do not inject.
6647
if (options.injectRegister === 'auto')
6748
options.injectRegister = useImportRegister ? false : 'script'
6849

6950
if ((options.injectRegister === 'script' || options.injectRegister === 'script-defer') && !existsSync(resolve(viteConfig.publicDir, FILE_SW_REGISTER))) {
70-
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] = {
7172
// @ts-expect-error: for Vite 3 support, Vite 4 has removed `isAsset` property
7273
isAsset: true,
7374
type: 'asset',
7475
// vite 6 deprecation: replaced with names
7576
name: undefined,
7677
// fix vite 6 build with manifest enabled
7778
names: [],
78-
source: generateSimpleSWRegister(options, false),
79-
fileName: FILE_SW_REGISTER,
79+
source: asset.source,
80+
fileName: asset.fileName,
8081
}
8182
}
82-
return bundle
8383
}
8484

8585
export function createAPI(ctx: PWAPluginContext) {

0 commit comments

Comments
 (0)