You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
1021 B

4 years ago
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { ConcatSource } = require("webpack-sources");
  7. class WebWorkerChunkTemplatePlugin {
  8. apply(chunkTemplate) {
  9. chunkTemplate.hooks.render.tap(
  10. "WebWorkerChunkTemplatePlugin",
  11. (modules, chunk) => {
  12. const chunkCallbackName = chunkTemplate.outputOptions.chunkCallbackName;
  13. const globalObject = chunkTemplate.outputOptions.globalObject;
  14. const source = new ConcatSource();
  15. source.add(
  16. `${globalObject}[${JSON.stringify(
  17. chunkCallbackName
  18. )}](${JSON.stringify(chunk.ids)},`
  19. );
  20. source.add(modules);
  21. source.add(")");
  22. return source;
  23. }
  24. );
  25. chunkTemplate.hooks.hash.tap("WebWorkerChunkTemplatePlugin", hash => {
  26. hash.update("webworker");
  27. hash.update("3");
  28. hash.update(`${chunkTemplate.outputOptions.chunkCallbackName}`);
  29. hash.update(`${chunkTemplate.outputOptions.globalObject}`);
  30. });
  31. }
  32. }
  33. module.exports = WebWorkerChunkTemplatePlugin;