Merge pull request #19 from brijeshb42/master

Add support for webpack 3
This commit is contained in:
Alexandru Dima 2018-07-02 15:05:42 +02:00 committed by GitHub
commit 98d97d8541
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,4 +1,27 @@
const webpack = require('webpack'); const webpack = require('webpack');
const webpackVersion = require('webpack/package.json').version;
const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin');
const LoaderTargetPlugin = require('webpack/lib/LoaderTargetPlugin');
const WebWorkerTemplatePlugin = require('webpack/lib/webworker/WebWorkerTemplatePlugin');
function getCompilerHook(compiler, {id, entry, filename, chunkFilename, plugins}) {
return function(compilation, callback) {
const outputOptions = {
filename,
chunkFilename,
publicPath: compilation.outputOptions.publicPath,
// HACK: globalObject is necessary to fix https://github.com/webpack/webpack/issues/6642
globalObject: 'this',
};
const childCompiler = compilation.createChildCompiler(id, outputOptions, [
new WebWorkerTemplatePlugin(),
new LoaderTargetPlugin('webworker'),
new SingleEntryPlugin(compiler.context, entry, 'main'),
]);
plugins.forEach((plugin) => plugin.apply(childCompiler));
childCompiler.runAsChild(callback);
}
}
class AddWorkerEntryPointPlugin { class AddWorkerEntryPointPlugin {
constructor({ constructor({
@ -12,23 +35,12 @@ class AddWorkerEntryPointPlugin {
} }
apply(compiler) { apply(compiler) {
const { id, entry, filename, chunkFilename, plugins } = this.options; const compilerHook = getCompilerHook(compiler, this.options);
compiler.hooks.make.tapAsync('AddWorkerEntryPointPlugin', (compilation, callback) => { if (webpackVersion < '4') {
const outputOptions = { compiler.plugin('make', compilerHook);
filename, } else {
chunkFilename, compiler.hooks.make.tapAsync('AddWorkerEntryPointPlugin', compilerHook);
publicPath: compilation.outputOptions.publicPath, }
// HACK: globalObject is necessary to fix https://github.com/webpack/webpack/issues/6642
globalObject: 'this',
};
const childCompiler = compilation.createChildCompiler(id, outputOptions, [
new webpack.webworker.WebWorkerTemplatePlugin(),
new webpack.LoaderTargetPlugin('webworker'),
new webpack.SingleEntryPlugin(compiler.context, entry, 'main'),
]);
plugins.forEach((plugin) => plugin.apply(childCompiler));
childCompiler.runAsChild(callback);
});
} }
} }