import webpack from 'webpack' ; class SwVariables { options : any ; constructor ( options = { } ) { this . options = options ; } apply ( compiler ) { const { fileName , variables , text } = this . options ; const { webpack } = compiler ; const { Compilation } = webpack ; const { RawSource } = webpack . sources ; compiler . hooks . thisCompilation . tap ( 'AddVariableToSw' , ( compilation ) => { compilation . hooks . processAssets . tap ( { name : 'AddVariableToSw' , stage : Compilation . PROCESS_ASSETS_STAGE_SUMMARIZE , } , ( assets ) => { if ( assets [ fileName ] ) { let src = assets [ fileName ] . source ( ) ; const pawEnv = Object . keys ( process . env ) . filter ( x => x . indexOf ( 'PAW_' ) !== - 1 && x !== 'PAW_CONFIG_PATH' ) ; const env = { } ; pawEnv . forEach ( ( k ) => { env [ k ] = process . env [ k ] ; } ) ; const { publicPath } = compilation . options . output ; const assetList = Object . keys ( assets ) . map ( filePath => [ publicPath , filePath ] . join ( '' ) ) . filter ( f => f . indexOf ( 'LICENSE.txt' ) === - 1 ) ; const swRevision = JSON . stringify ( new Date ( ) . getTime ( ) ) ; src = `self.__PAW_MANIFEST=[
            { url: '${ variables . appRootUrl || '' }/manifest.json', revision: ${ swRevision } },
            { url: '${ variables . appRootUrl || '' }/sw.js', revision: ${ swRevision } },
            ];${ src }` ; if ( assetList . length ) { src = `${ src };self.paw__offline_assets = ${ JSON . stringify ( assetList ) }` ; } src = `${ src };self.paw__injected_variables = ${ JSON . stringify ( variables ) };` ; if ( text && text . length ) { src = `${ src };${ text }` ; } src = `self.paw__env=${ JSON . stringify ( env ) };${ src }` ; compilation . updateAsset ( fileName , new RawSource ( src ) ) ; } } ) ; } ) ; } } export default SwVariables ;