AddWorkerEntryPointPlugin.js 1.0 KB

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