class PwaTask extends BalmJS . BalmTask { constructor ( ) { super ( 'pwa' ) ; } setIgnores ( options ) { const defaultIgnores = [ BalmJS . config . pwa . swSrcFilename ] ; if ( options . globIgnores ) { options . globIgnores = options . globIgnores . concat ( defaultIgnores ) ; } else { options . globIgnores = defaultIgnores ; } return options ; } recipe ( customMode ? , customOptions = { } ) { const balmPwa = async ( callback ) : Promise < void > => { const mode = customMode || BalmJS . config . pwa . mode ; let options = { } ; const globDirectory = BalmJS . config . dest . base ; const swDest = BalmJS . file . absPath ( `${ globDirectory }/${ BalmJS . config . pwa . swDestFilename }` ) ; const swSrc = BalmJS . file . absPath ( `${ BalmJS . config . src . base }/${ BalmJS . config . pwa . swSrcFilename }` ) ; let valid = true ; switch ( mode ) { case 'generateSW' : options = Object . assign ( { globDirectory , swDest } , BalmJS . config . pwa . options , customOptions ) ; break ; case 'injectManifest' : options = Object . assign ( { globDirectory , swDest , swSrc } , BalmJS . config . pwa . options , customOptions ) ; break ; default : valid = false ; } if ( valid ) { options = this . setIgnores ( options ) ; BalmJS . logger . debug ( `pwa - ${ mode }` , options , { pre : true } ) ; await require ( 'workbox-build' ) [ mode ] ( options ) . then ( ( { count , size } ) => { BalmJS . logger . info ( `pwa - ${ mode }` , `Generated '${ swDest }', which will precache ${ count } files, totaling ${ size } bytes` ) ; gulp . parallel ( BalmJS . toNamespace ( 'pwa-cache' ) as string ) ( ) ; } ) . catch ( ( err ) => { BalmJS . logger . warn ( `pwa - ${ mode }` , `Service worker generation failed: ${ err }` ) ; } ) ; callback ( ) ; } else { BalmJS . logger . warn ( 'pwa task' , 'Invalid PWA mode' ) ; callback ( ) ; } } ; return balmPwa ; } get fn ( ) : Function { return this . recipe ( ) ; } } export default PwaTask ;