This repository has been archived by the owner on Aug 1, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·50 lines (43 loc) · 1.48 KB
/
index.js
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
'use strict'
const fs = require('fs')
const path = require('path')
const glob = require('glob')
const NodeVersionAssets = require('node-version-assets')
const precache = require('sw-precache')
module.exports = argv => {
const output = path.dirname(argv[0]) || 'dist'
const swFile = `${output}/service-worker.js`
const htmlFile =
path.extname(argv[0]) === '.html' ? path.basename(argv[0]) : '**/*.html'
glob(`${output}/**/*.{css,js}`, (error, assets) => {
if (error) throw error
glob(`${output}/${htmlFile}`, (error, grepFiles) => {
if (error) throw error
grepFiles.map(htmlFile => {
fs.readFile(htmlFile, 'utf-8', (error, data) => {
if (error) throw error
data = data.replace(
'</body>',
'<script src="service-worker.js"></script><script>if ("serviceWorker" in navigator && window.location.protocol === "https:") navigator.serviceWorker.register("/service-worker.js")</script></body>',
)
fs.writeFile(htmlFile, data, (error, data) =>
!error
? console.log(`${htmlFile} was saved!`)
: console.error(error),
)
})
})
const nva = new NodeVersionAssets({
assets,
grepFiles,
})
nva.run()
precache.write(swFile, {
staticFileGlobs: [
`${output}/**/*.{html,js,css,png,jpg,jpeg,gif,svg,ico,eot,ttf,woff,woff2,txt,webapp,json}`,
],
stripPrefix: output,
})
})
})
}