WebWorkerMainTemplatePlugin.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const Template = require("../Template");
  7. class WebWorkerMainTemplatePlugin {
  8. apply(mainTemplate) {
  9. mainTemplate.plugin("local-vars", function(source, chunk) {
  10. if(chunk.chunks.length > 0) {
  11. return this.asString([
  12. source,
  13. "",
  14. "// object to store loaded chunks",
  15. "// \"1\" means \"already loaded\"",
  16. "var installedChunks = {",
  17. this.indent(
  18. chunk.ids.map((id) => `${id}: 1`).join(",\n")
  19. ),
  20. "};"
  21. ]);
  22. }
  23. return source;
  24. });
  25. mainTemplate.plugin("require-ensure", function(_, chunk, hash) {
  26. const chunkFilename = this.outputOptions.chunkFilename;
  27. return this.asString([
  28. "return new Promise(function(resolve) {",
  29. this.indent([
  30. "// \"1\" is the signal for \"already loaded\"",
  31. "if(!installedChunks[chunkId]) {",
  32. this.indent([
  33. "importScripts(" +
  34. this.applyPluginsWaterfall("asset-path", JSON.stringify(chunkFilename), {
  35. hash: `" + ${this.renderCurrentHashCode(hash)} + "`,
  36. hashWithLength: (length) => `" + ${this.renderCurrentHashCode(hash, length)} + "`,
  37. chunk: {
  38. id: "\" + chunkId + \""
  39. }
  40. }) + ");"
  41. ]),
  42. "}",
  43. "resolve();"
  44. ]),
  45. "});"
  46. ]);
  47. });
  48. mainTemplate.plugin("bootstrap", function(source, chunk, hash) {
  49. if(chunk.chunks.length > 0) {
  50. const chunkCallbackName = this.outputOptions.chunkCallbackName || Template.toIdentifier("webpackChunk" + (this.outputOptions.library || ""));
  51. return this.asString([
  52. source,
  53. `this[${JSON.stringify(chunkCallbackName)}] = function webpackChunkCallback(chunkIds, moreModules) {`,
  54. this.indent([
  55. "for(var moduleId in moreModules) {",
  56. this.indent(this.renderAddModule(hash, chunk, "moduleId", "moreModules[moduleId]")),
  57. "}",
  58. "while(chunkIds.length)",
  59. this.indent("installedChunks[chunkIds.pop()] = 1;")
  60. ]),
  61. "};"
  62. ]);
  63. }
  64. return source;
  65. });
  66. mainTemplate.plugin("hot-bootstrap", function(source, chunk, hash) {
  67. const hotUpdateChunkFilename = this.outputOptions.hotUpdateChunkFilename;
  68. const hotUpdateMainFilename = this.outputOptions.hotUpdateMainFilename;
  69. const hotUpdateFunction = this.outputOptions.hotUpdateFunction || Template.toIdentifier("webpackHotUpdate" + (this.outputOptions.library || ""));
  70. const currentHotUpdateChunkFilename = this.applyPluginsWaterfall("asset-path", JSON.stringify(hotUpdateChunkFilename), {
  71. hash: `" + ${this.renderCurrentHashCode(hash)} + "`,
  72. hashWithLength: (length) => `" + ${this.renderCurrentHashCode(hash, length)} + "`,
  73. chunk: {
  74. id: "\" + chunkId + \""
  75. }
  76. });
  77. const currentHotUpdateMainFilename = this.applyPluginsWaterfall("asset-path", JSON.stringify(hotUpdateMainFilename), {
  78. hash: `" + ${this.renderCurrentHashCode(hash)} + "`,
  79. hashWithLength: (length) => `" + ${this.renderCurrentHashCode(hash, length)} + "`,
  80. });
  81. return source + "\n" +
  82. `var parentHotUpdateCallback = self[${JSON.stringify(hotUpdateFunction)}];\n` +
  83. `self[${JSON.stringify(hotUpdateFunction)}] = ` +
  84. Template.getFunctionContent(require("./WebWorkerMainTemplate.runtime.js"))
  85. .replace(/\/\/\$semicolon/g, ";")
  86. .replace(/\$require\$/g, this.requireFn)
  87. .replace(/\$hotMainFilename\$/g, currentHotUpdateMainFilename)
  88. .replace(/\$hotChunkFilename\$/g, currentHotUpdateChunkFilename)
  89. .replace(/\$hash\$/g, JSON.stringify(hash));
  90. });
  91. mainTemplate.plugin("hash", function(hash) {
  92. hash.update("webworker");
  93. hash.update("3");
  94. hash.update(`${this.outputOptions.publicPath}`);
  95. hash.update(`${this.outputOptions.filename}`);
  96. hash.update(`${this.outputOptions.chunkFilename}`);
  97. hash.update(`${this.outputOptions.chunkCallbackName}`);
  98. hash.update(`${this.outputOptions.library}`);
  99. });
  100. }
  101. }
  102. module.exports = WebWorkerMainTemplatePlugin;