Skip to content

Instantly share code, notes, and snippets.

@iammerrick
Created August 2, 2016 22:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iammerrick/945dac5d5049b309ee878c0fcb6a43f0 to your computer and use it in GitHub Desktop.
Save iammerrick/945dac5d5049b309ee878c0fcb6a43f0 to your computer and use it in GitHub Desktop.
'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