-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
SvelteKitPlugin.ts
136 lines (123 loc) · 4.62 KB
/
SvelteKitPlugin.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { lstat, readFile, rm, writeFile } from 'node:fs/promises'
import { join } from 'node:path'
import type { Plugin, ResolvedConfig } from 'vite'
import type { VitePWAOptions, VitePluginPWAAPI } from 'vite-plugin-pwa'
// @ts-expect-error export = is not supported by @types/node
import fg from 'fast-glob'
export function SvelteKitPlugin(
options: Partial<VitePWAOptions>,
apiResolver: () => VitePluginPWAAPI | undefined,
) {
let viteConfig: ResolvedConfig
return <Plugin>{
name: 'vite-plugin-pwa:sveltekit:build',
apply: 'build',
enforce: 'pre',
configResolved(config) {
viteConfig = config
},
generateBundle(_, bundle) {
// generate only for client
if (viteConfig.build.ssr)
return
apiResolver()?.generateBundle(bundle)
},
closeBundle: {
sequential: true,
enforce: 'pre',
async handler() {
const api = apiResolver()
if (api && !api.disabled && viteConfig.build.ssr) {
const webManifest = options.manifestFilename ?? 'manifest.webmanifest'
let swName = options.filename ?? 'sw.js'
const outDir = options.outDir ?? `${viteConfig.root}/.svelte-kit/output`
if (!options.strategies || options.strategies === 'generateSW' || options.selfDestroying) {
let path: string
let existsFile: boolean
// remove kit sw: we'll regenerate the sw
if (options.selfDestroying && options.strategies === 'injectManifest') {
if (swName.endsWith('.ts'))
swName = swName.replace(/\.ts$/, '.js')
path = join(outDir, 'client', 'service-worker.js').replace('\\/g', '/')
existsFile = await isFile(path)
if (existsFile)
await rm(path)
}
// regenerate sw before adapter runs: we need to include generated html pages
await api.generateSW()
const serverOutputDir = join(outDir, 'server')
path = join(serverOutputDir, swName).replace(/\\/g, '/')
existsFile = await isFile(path)
if (existsFile) {
const sw = await readFile(path, 'utf-8')
await writeFile(
join(outDir, 'client', swName).replace('\\/g', '/'),
sw,
'utf-8',
)
await rm(path)
}
// move also workbox-*.js when using generateSW
const result = await fg(
['workbox-*.js'], {
cwd: serverOutputDir,
onlyFiles: true,
unique: true,
},
)
if (result && result.length > 0) {
path = join(serverOutputDir, result[0]).replace(/\\/g, '/')
await writeFile(
join(outDir, 'client', result[0]).replace('\\/g', '/'),
await readFile(path, 'utf-8'),
'utf-8',
)
await rm(path)
}
// remove also web manifest in server folder
path = join(serverOutputDir, webManifest).replace(/\\/g, '/')
existsFile = await isFile(path)
if (existsFile)
await rm(path)
return
}
if (swName.endsWith('.ts'))
swName = swName.replace(/\.ts$/, '.js')
// kit fixes sw name to 'service-worker.js'
const injectManifestOptions: import('workbox-build').InjectManifestOptions = {
globDirectory: outDir.replace(/\\/g, '/'),
...options.injectManifest ?? {},
swSrc: join(outDir, 'client', 'service-worker.js').replace(/\\/g, '/'),
swDest: join(outDir, 'client', 'service-worker.js').replace(/\\/g, '/'),
}
const [injectManifest, logWorkboxResult] = await Promise.all([
import('workbox-build').then(m => m.injectManifest),
import('./log').then(m => m.logWorkboxResult),
])
// inject the manifest
const buildResult = await injectManifest(injectManifestOptions)
// log workbox result
logWorkboxResult('injectManifest', buildResult, viteConfig)
// rename the sw
if (swName !== 'service-worker.js') {
await writeFile(
join(outDir, 'client', swName).replace('\\/g', '/'),
await readFile(injectManifestOptions.swSrc, 'utf-8'),
'utf-8',
)
await rm(injectManifestOptions.swDest)
}
}
},
},
}
}
async function isFile(path: string) {
try {
const stats = await lstat(path)
return stats.isFile()
}
catch {
return false
}
}