AddWorkerEntryPointPlugin.js 936 B

123456789101112131415161718192021222324252627
  1. class AddWorkerEntryPointPlugin {
  2. constructor(webpack, { id, entry, output }) {
  3. this.webpack = webpack;
  4. this.options = { id, entry, output };
  5. }
  6. apply(compiler) {
  7. const webpack = this.webpack;
  8. const { id, entry, output } = this.options;
  9. compiler.plugin('make', (compilation, callback) => {
  10. const outputOptions = {
  11. filename: output,
  12. publicPath: compilation.outputOptions.publicPath,
  13. // HACK: globalObject is necessary to fix https://github.com/webpack/webpack/issues/6642
  14. globalObject: 'this',
  15. };
  16. const childCompiler = compilation.createChildCompiler(id, outputOptions, [
  17. new webpack.webworker.WebWorkerTemplatePlugin(),
  18. new webpack.LoaderTargetPlugin('webworker'),
  19. new webpack.SingleEntryPlugin(this.context, entry, 'main'),
  20. ]);
  21. childCompiler.runAsChild(callback);
  22. });
  23. }
  24. }
  25. module.exports = AddWorkerEntryPointPlugin;