@@ -2,25 +2,39 @@ module.exports = (api, options) => {
2
2
api . chainWebpack ( webpackConfig => {
3
3
const name = api . service . pkg . name
4
4
5
+ const userOptions = options . pwa || { }
6
+
5
7
// the pwa plugin hooks on to html-webpack-plugin
6
8
// and injects icons, manifest links & other PWA related tags into <head>
7
9
webpackConfig
8
10
. plugin ( 'pwa' )
9
11
. use ( require ( './lib/HtmlPwaPlugin' ) , [ Object . assign ( {
10
12
name
11
- } , options . pwa ) ] )
13
+ } , userOptions ) ] )
12
14
13
15
// generate /service-worker.js in production mode
14
16
if ( process . env . NODE_ENV === 'production' ) {
15
- webpackConfig
16
- . plugin ( 'sw-precache' )
17
- . use ( require ( 'sw-precache-webpack-plugin' ) , [ {
18
- cacheId : name ,
19
- filename : 'service-worker.js' ,
20
- staticFileGlobs : [ `${ options . outputDir } /**/*.{js,html,css}` ] ,
21
- minify : true ,
22
- stripPrefix : `${ options . outputDir } /`
23
- } ] )
17
+ // Default to GenerateSW mode, though InjectManifest also might be used.
18
+ const workboxPluginMode = userOptions . workboxPluginMode || 'GenerateSW'
19
+ const workboxWebpackModule = require ( 'workbox-webpack-plugin' )
20
+ if ( workboxPluginMode in workboxWebpackModule ) {
21
+ const workBoxConfig = Object . assign ( {
22
+ cacheId : name ,
23
+ exclude : [
24
+ new RegExp ( '\.map$' ) ,
25
+ new RegExp ( 'img/icons/' ) ,
26
+ new RegExp ( 'favicon\.ico$' ) ,
27
+ new RegExp ( 'manifest\.json$' )
28
+ ]
29
+ } , userOptions . workboxOptions )
30
+
31
+ webpackConfig
32
+ . plugin ( 'workbox' )
33
+ . use ( workboxWebpackModule [ workboxPluginMode ] , [ workBoxConfig ] )
34
+ } else {
35
+ throw new Error ( `${ workboxPluginMode } is not a supported Workbox webpack plugin mode. ` +
36
+ `Valid modes are: ${ Object . keys ( workboxWebpackModule ) . join ( ', ' ) } ` )
37
+ }
24
38
}
25
39
} )
26
40
0 commit comments