-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
104 lines (86 loc) · 3 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
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
const { writeFileSync, readFileSync } = require('fs')
const path = require('path')
const swPrecache = require('sw-precache')
const UglifyJS = require('uglify-es')
const urlJoin = require('url-join')
const getServiceWorkder = options =>
swPrecache.generate(options).catch(err => {
throw err
})
const getValue = function(obj, key) {
return key.split('.').reduce(function(o, x) {
return typeof o == 'undefined' || o === null ? o : o[x]
}, obj)
}
// These configs are supplied as array of strings and should be converted to array of regexps
const regexpConfigs = [
'dontCacheBustUrlsMatching',
'ignoreUrlParametersMatching',
'navigateFallbackWhitelist',
'staticFileGlobsIgnorePatterns'
]
module.exports = bundler => {
const { minify, publicURL, outDir, rootDir } = bundler.options
bundler.on('bundled', async () => {
let pkg
const entryAsset = getValue(bundler, 'mainBundle.entryAsset')
try {
pkg = await entryAsset.getPackage()
} catch (err) {
throw new Error(
'The bundler properties/sub-properties (mainBundle / entryAsset) seems to be not present'
)
}
const swPrecacheConfigs = pkg['sw-precache']
if (swPrecacheConfigs) {
// Respect regexp configs
regexpConfigs.forEach(config => {
if (swPrecacheConfigs[config]) {
swPrecacheConfigs[config] = swPrecacheConfigs[config].map(
strValue => new RegExp(strValue)
)
}
})
}
// Update default stripPrefix for Windows file path format
const isWin = /^win/.test(process.platform)
let stripPrefixDefault = outDir + '/'
if (isWin) {
stripPrefixDefault = stripPrefixDefault.replace(/\\/g, '/')
}
const options = {
cacheId: pkg.name, // default cacheId
dontCacheBustUrlsMatching: /\.\w{8}\./,
navigateFallback: urlJoin(publicURL, 'index.html'),
staticFileGlobs: [
outDir + '/**/*.{js,html,css,png,jpg,gif,svg,eot,ttf,woff,woff2,ogg,wav,mp3,wasm,webp,pdf}'
],
staticFileGlobsIgnorePatterns: [
/\.map$/,
/asset-manifest\.json$/,
/service-worker\.js$/
],
stripPrefix: stripPrefixDefault,
replacePrefix: urlJoin(publicURL, '/'),
// https://firebase.google.com/docs/hosting/reserved-urls#reserved_urls_and_service_workers
navigateFallbackWhitelist: [/^(?!\/__).*/],
// merge user configs
...swPrecacheConfigs
}
getServiceWorkder(options).then(codes => {
// Adding additionalCode to ServiceWorker File
if(options.additionalCodeFile) {
const additionalCode = readFileSync(path.resolve(rootDir, options.additionalCodeFile), 'utf8');
codes += additionalCode;
}
const fileName = 'service-worker.js'
if (minify) {
const compressedCodes = {}
compressedCodes[fileName] = codes
codes = UglifyJS.minify(compressedCodes).code
}
const serviceWorkerFilePath = path.resolve(outDir, fileName)
writeFileSync(serviceWorkerFilePath, codes)
})
})
}