AddWorkerEntryPointPlugin.js 1.2 KB

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