|
1 |
| -import type { OutputBundle, PluginContext } 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'
|
@@ -37,49 +37,49 @@ export function _generateBundle(ctx: PWAPluginContext, bundle?: OutputBundle, pl
|
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 |
| - 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) |
63 | 44 | }
|
64 | 45 |
|
65 | 46 | // if virtual register is requested, do not inject.
|
66 | 47 | if (options.injectRegister === 'auto')
|
67 | 48 | options.injectRegister = useImportRegister ? false : 'script'
|
68 | 49 |
|
69 | 50 | 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] = { |
71 | 72 | // @ts-expect-error: for Vite 3 support, Vite 4 has removed `isAsset` property
|
72 | 73 | isAsset: true,
|
73 | 74 | type: 'asset',
|
74 | 75 | // vite 6 deprecation: replaced with names
|
75 | 76 | name: undefined,
|
76 | 77 | // fix vite 6 build with manifest enabled
|
77 | 78 | names: [],
|
78 |
| - source: generateSimpleSWRegister(options, false), |
79 |
| - fileName: FILE_SW_REGISTER, |
| 79 | + source: asset.source, |
| 80 | + fileName: asset.fileName, |
80 | 81 | }
|
81 | 82 | }
|
82 |
| - return bundle |
83 | 83 | }
|
84 | 84 |
|
85 | 85 | export function createAPI(ctx: PWAPluginContext) {
|
|
0 commit comments