-
-
Notifications
You must be signed in to change notification settings - Fork 210
/
build.ts
59 lines (53 loc) · 1.68 KB
/
build.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import type { Plugin } from 'vite'
import { injectServiceWorker } from '../html'
import { _generateBundle, _generateSW } from '../api'
import type { PWAPluginContext } from '../context'
export function BuildPlugin(ctx: PWAPluginContext) {
const transformIndexHtmlHandler = (html: string) => {
const { options, useImportRegister } = ctx
if (options.disable)
return html
// if virtual register is requested, do not inject.
if (options.injectRegister === 'auto')
options.injectRegister = useImportRegister ? null : 'script'
return injectServiceWorker(html, options, false)
}
return <Plugin>{
name: 'vite-plugin-pwa:build',
enforce: 'post',
apply: 'build',
transformIndexHtml: {
order: 'post',
handler(html) {
return transformIndexHtmlHandler(html)
},
enforce: 'post', // deprecated since Vite 4
transform(html) { // deprecated since Vite 4
return transformIndexHtmlHandler(html)
},
},
async generateBundle(_, bundle) {
const pwaAssetsGenerator = await ctx.pwaAssetsGenerator
if (pwaAssetsGenerator)
pwaAssetsGenerator.injectManifestIcons()
return _generateBundle(ctx, bundle)
},
closeBundle: {
sequential: true,
order: ctx.userOptions?.integration?.closeBundleOrder,
async handler() {
if (!ctx.viteConfig.build.ssr) {
const pwaAssetsGenerator = await ctx.pwaAssetsGenerator
if (pwaAssetsGenerator)
await pwaAssetsGenerator.generate()
if (!ctx.options.disable)
await _generateSW(ctx)
}
},
},
async buildEnd(error) {
if (error)
throw error
},
}
}