AddWorkerEntryPointPlugin.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.AddWorkerEntryPointPlugin = void 0;
  4. function getCompilerHook(compiler, { id, entry, filename, chunkFilename, plugins }) {
  5. var _a;
  6. const webpack = (_a = compiler.webpack) !== null && _a !== void 0 ? _a : require('webpack');
  7. return function (compilation, callback) {
  8. var _a;
  9. const outputOptions = {
  10. filename,
  11. chunkFilename,
  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. ]);
  20. const SingleEntryPlugin = (_a = webpack.EntryPlugin) !== null && _a !== void 0 ? _a : webpack.SingleEntryPlugin;
  21. new SingleEntryPlugin(compiler.context, entry, 'main').apply(childCompiler);
  22. plugins.forEach((plugin) => plugin.apply(childCompiler));
  23. childCompiler.runAsChild((err) => callback(err));
  24. };
  25. }
  26. class AddWorkerEntryPointPlugin {
  27. constructor({ id, entry, filename, chunkFilename = undefined, plugins }) {
  28. this.options = { id, entry, filename, chunkFilename, plugins };
  29. }
  30. apply(compiler) {
  31. var _a;
  32. const webpack = (_a = compiler.webpack) !== null && _a !== void 0 ? _a : require('webpack');
  33. const compilerHook = getCompilerHook(compiler, this.options);
  34. const majorVersion = webpack.version.split('.')[0];
  35. if (parseInt(majorVersion) < 4) {
  36. compiler.plugin('make', compilerHook);
  37. }
  38. else {
  39. compiler.hooks.make.tapAsync('AddWorkerEntryPointPlugin', compilerHook);
  40. }
  41. }
  42. }
  43. exports.AddWorkerEntryPointPlugin = AddWorkerEntryPointPlugin;