This repository has been archived by the owner on Dec 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
index.js
87 lines (72 loc) · 2.23 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
const
appPaths = require('../../app-paths'),
PwaManifestPlugin = require('./plugin.pwa-manifest')
module.exports = function (chain, cfg) {
// write manifest.json file
chain.plugin('pwa-manifest')
.use(PwaManifestPlugin, [ cfg ])
let defaultOptions
const
WorkboxPlugin = require('workbox-webpack-plugin'),
pluginMode = cfg.pwa.workboxPluginMode,
log = require('../../helpers/logger')('app:workbox')
if (pluginMode === 'GenerateSW') {
const pkg = require(appPaths.resolve.app('package.json'))
defaultOptions = {
cacheId: pkg.name || 'quasar-pwa-app'
}
log('[GenerateSW] Will generate a service-worker file. Ignoring your custom written one.')
}
else {
defaultOptions = {
swSrc: appPaths.resolve.app(cfg.sourceFiles.serviceWorker)
}
log('[InjectManifest] Using your custom service-worker written file')
}
let opts = Object.assign(
defaultOptions,
cfg.pwa.workboxOptions
)
if (cfg.ctx.mode.ssr) {
if (!opts.directoryIndex) {
opts.directoryIndex = '/'
}
// if Object form:
if (cfg.ssr.pwa && cfg.ssr.pwa !== true) {
const merge = require('webpack-merge')
opts = merge(opts, cfg.ssr.pwa)
}
if (pluginMode === 'GenerateSW') {
opts.runtimeCaching = opts.runtimeCaching || []
if (!opts.runtimeCaching.find(entry => entry.urlPattern === '/')) {
opts.runtimeCaching.unshift({
urlPattern: '/',
handler: 'networkFirst'
})
}
}
}
if (cfg.ctx.dev) {
log('⚠️ Forcing PWA into the network-first approach to not break Hot Module Replacement while developing.')
// forcing network-first strategy
opts.chunks = [ 'quasar-bogus-chunk' ]
if (opts.excludeChunks) {
delete opts.excludeChunks
}
if (pluginMode === 'GenerateSW') {
opts.runtimeCaching = opts.runtimeCaching || []
opts.runtimeCaching.push({
urlPattern: /^http/,
handler: 'networkFirst'
})
}
}
opts.swDest = 'service-worker.js'
chain.plugin('workbox')
.use(WorkboxPlugin[pluginMode], [ opts ])
if (!cfg.ctx.mode.ssr) {
const HtmlPwaPlugin = require('./plugin.html-pwa').plugin
chain.plugin('html-pwa')
.use(HtmlPwaPlugin, [ cfg ])
}
}