-
-
Save iammerrick/945dac5d5049b309ee878c0fcb6a43f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin'); | |
const webpack = require('webpack'); | |
const CHILD_COMPILER_NAME = 'service-worker-plugin'; | |
class ServiceWorkerPlugin { | |
constructor(options) { | |
this.options = options; | |
} | |
apply(compiler) { | |
compiler.plugin('emit', (compilation, callback) => { | |
const stats = compilation.getStats().toJson(); | |
const child = compilation.createChildCompiler(CHILD_COMPILER_NAME, { | |
filename: this.options.output, | |
}); | |
child.apply( | |
new SingleEntryPlugin(compiler.context, this.options.entry, this.options.output) | |
); | |
child.apply( | |
new webpack.DefinePlugin({ | |
SHA: JSON.stringify(process.env.CI_COMMIT || 'unreleased'), | |
ASSETS: JSON.stringify(stats.assets.map((asset) => `/m/public/${asset.name}`).filter((name) => !name.endsWith('.map'))), | |
}) | |
); | |
child.runAsChild((err) => { | |
if (err) callback(err); | |
callback(); | |
}); | |
}); | |
} | |
} | |
module.exports = ServiceWorkerPlugin; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment