UNPKG

siegel

Version:

Web application development ecosystem

26 lines (25 loc) 1.1 kB
import fs from 'fs'; import path from 'path'; import webpack from 'webpack'; import ts from 'typescript'; const { sources, Compilation } = webpack; const NAME = 'siegel-sw-plugin'; const serviceWorkerPlugin = function ({ swPath }) { const filename = path.basename(swPath); const swContent = fs.readFileSync(swPath, 'utf8'); this.apply = function (compiler) { compiler.hooks.thisCompilation.tap(NAME, compilation => { compilation.hooks.processAssets.tap({ name: NAME, stage: Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE }, assets => { const SW_ASSETS = JSON.stringify(Object.keys(assets)); const isTS = filename.endsWith('.ts'); const swContentJS = isTS ? ts.transpile(swContent) : swContent; const SW_SOURCE = `const buildOutput=${SW_ASSETS};${swContentJS}`; compilation.emitAsset(isTS ? filename.replace('.ts', '.js') : filename, new sources.RawSource(SW_SOURCE, true)); }); }); }; }; export default serviceWorkerPlugin;