-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathSvelteKitPlugin.ts
173 lines (155 loc) · 5.86 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { lstat, mkdir, readFile, rename, 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
},
async generateBundle(_, bundle) {
// generate only for client
if (viteConfig.build.ssr)
return
const api = apiResolver()
if (!api)
return
const assetsGenerator = await api.pwaAssetsGenerator()
if (assetsGenerator)
assetsGenerator.injectManifestIcons()
api.generateBundle(bundle)
},
writeBundle: {
sequential: true,
enforce: 'pre',
async handler() {
const api = apiResolver()
if (!api || viteConfig.build.ssr)
return
const assetsGenerator = await api.pwaAssetsGenerator()
if (assetsGenerator)
await assetsGenerator.generate()
},
},
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`
const clientOutputDir = join(outDir, 'client')
await mkdir(clientOutputDir, { recursive: true })
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(clientOutputDir, '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(clientOutputDir, 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(clientOutputDir, 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')
const injectionPoint = !options.injectManifest || !('injectionPoint' in options.injectManifest) || !!options.injectManifest.injectionPoint
if (injectionPoint) {
// kit fixes sw name to 'service-worker.js'
const injectManifestOptions: import('workbox-build').InjectManifestOptions = {
globDirectory: outDir.replace(/\\/g, '/'),
...options.injectManifest ?? {},
swSrc: join(clientOutputDir, 'service-worker.js').replace(/\\/g, '/'),
swDest: join(clientOutputDir, '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', viteConfig, buildResult)
// rename the sw
if (swName !== 'service-worker.js') {
await rename(
join(clientOutputDir, 'service-worker.js').replace('\\/g', '/'),
join(clientOutputDir, swName).replace('\\/g', '/'),
)
}
}
else {
const { logWorkboxResult } = await import('./log')
// log workbox result
logWorkboxResult('injectManifest', viteConfig)
if (swName !== 'service-worker.js') {
await rename(
join(clientOutputDir, 'service-worker.js').replace('\\/g', '/'),
join(clientOutputDir, swName).replace('\\/g', '/'),
)
}
}
}
},
},
}
}
async function isFile(path: string) {
try {
const stats = await lstat(path)
return stats.isFile()
}
catch {
return false
}
}