-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
58 lines (47 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
51
52
53
54
55
56
57
58
const workboxPlugin = require('workbox-webpack-plugin')
const defaultGenerateConfig = {
exclude: [/\.map$/, /^(?:asset-)manifest.*\.js(?:on)?$/],
navigateFallback: '/index.html',
navigateFallbackBlacklist: [
new RegExp('^/__'),
new RegExp('/[^/]+\.[^/]+$'),
],
clientsClaim: true
}
const defaultInjectConfig = {
exclude: defaultGenerateConfig.exclude
}
function findSWPrecachePlugin(element) {
return element.constructor.name === 'SWPrecacheWebpackPlugin'
}
function removeSWPrecachePlugin(config) {
const swPrecachePluginIndex = config.plugins.findIndex(findSWPrecachePlugin)
// Remove the swPrecachePlugin if it was found
if (swPrecachePluginIndex !== -1) {
config.plugins.splice(swPrecachePluginIndex, 1) // mutates
}
}
function rewireWorkboxGenerate(workboxConfig) {
workboxConfig = workboxConfig || defaultGenerateConfig
return function rewireWorkboxInner(config, env) {
removeSWPrecachePlugin(config)
// Add the Workbox plugin
config.plugins.push(new workboxPlugin.GenerateSW(workboxConfig))
return config
}
}
function rewireWorkboxInject(workboxConfig) {
workboxConfig = workboxConfig || defaultInjectConfig
return function rewireWorkboxInner(config, env) {
removeSWPrecachePlugin(config)
// Add the Workbox plugin
config.plugins.push(new workboxPlugin.InjectManifest(workboxConfig))
return config
}
}
module.exports = {
rewireWorkboxGenerate,
rewireWorkboxInject,
defaultGenerateConfig,
defaultInjectConfig
}