-
Notifications
You must be signed in to change notification settings - Fork 1
/
wmr.config.ts
54 lines (42 loc) · 1.49 KB
/
wmr.config.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
import { promises as fs } from 'fs'
import * as path from 'path'
import type { Options, Plugin } from 'wmr'
export default (options: Options) => {
options.host = '0.0.0.0'
options.port = parseInt(process.env.PORT || '8080', 10)
options.middleware.push(async (req, res, next) => {
const reqPath = req.path.replace(/^\//, '')
if (reqPath.startsWith('service-worker.') && reqPath.endsWith('.js')) {
const realPath = path.join(options.out, 'chunks', reqPath)
const bytes = await fs.readFile(realPath)
res.writeHead(200, {
'Content-Type': 'application/javascript'
})
res.end(bytes)
} else {
next()
}
})
options.plugins = [cacheUrlsPlugin()]
return options
}
function cacheUrlsPlugin(): Plugin {
return {
enforce: 'post',
name: 'cache-urls-plugin',
writeBundle: async function(options, bundle) {
const urls: string[] = []
for (const [filename, output] of Object.entries(bundle)) {
// NOTE: don't cache the service worker itself and don't worry about index.html
if (output.fileName.startsWith('service-worker.') || output.fileName === 'index.html') {
continue
}
urls.push(`/${output.fileName}`)
}
// NOTE: we cannot use this.emitFile because cache-urls.json is already in the bundle
const pathInDist = path.join(options.dir, 'cache-urls.json')
await fs.writeFile(pathInDist, JSON.stringify(urls))
console.debug(`wrote ${pathInDist}`)
}
}
}