@@ -2,25 +2,39 @@ module.exports = (api, options) => {
22 api . chainWebpack ( webpackConfig => {
33 const name = api . service . pkg . name
44
5+ const userOptions = options . pwa || { }
6+
57 // the pwa plugin hooks on to html-webpack-plugin
68 // and injects icons, manifest links & other PWA related tags into <head>
79 webpackConfig
810 . plugin ( 'pwa' )
911 . use ( require ( './lib/HtmlPwaPlugin' ) , [ Object . assign ( {
1012 name
11- } , options . pwa ) ] )
13+ } , userOptions ) ] )
1214
1315 // generate /service-worker.js in production mode
1416 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+ }
2438 }
2539 } )
2640
0 commit comments