WebWorkerMainTemplatePlugin.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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(function(id) {
  19. return id + ": 1";
  20. }).join(",\n")
  21. ),
  22. "};"
  23. ]);
  24. }
  25. return source;
  26. });
  27. mainTemplate.plugin("require-ensure", function(_, chunk, hash) {
  28. const chunkFilename = this.outputOptions.chunkFilename;
  29. return this.asString([
  30. "return new Promise(function(resolve) {",
  31. this.indent([
  32. "// \"1\" is the signal for \"already loaded\"",
  33. "if(!installedChunks[chunkId]) {",
  34. this.indent([
  35. "importScripts(" +
  36. this.applyPluginsWaterfall("asset-path", JSON.stringify(chunkFilename), {
  37. hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"",
  38. hashWithLength: function(length) {
  39. return "\" + " + this.renderCurrentHashCode(hash, length) + " + \"";
  40. }.bind(this),
  41. chunk: {
  42. id: "\" + chunkId + \""
  43. }
  44. }) + ");"
  45. ]),
  46. "}",
  47. "resolve();"
  48. ]),
  49. "});"
  50. ]);
  51. });
  52. mainTemplate.plugin("bootstrap", function(source, chunk, hash) {
  53. if(chunk.chunks.length > 0) {
  54. const chunkCallbackName = this.outputOptions.chunkCallbackName || Template.toIdentifier("webpackChunk" + (this.outputOptions.library || ""));
  55. return this.asString([
  56. source,
  57. "this[" + JSON.stringify(chunkCallbackName) + "] = function webpackChunkCallback(chunkIds, moreModules) {",
  58. this.indent([
  59. "for(var moduleId in moreModules) {",
  60. this.indent(this.renderAddModule(hash, chunk, "moduleId", "moreModules[moduleId]")),
  61. "}",
  62. "while(chunkIds.length)",
  63. this.indent("installedChunks[chunkIds.pop()] = 1;")
  64. ]),
  65. "};"
  66. ]);
  67. }
  68. return source;
  69. });
  70. mainTemplate.plugin("hot-bootstrap", function(source, chunk, hash) {
  71. const hotUpdateChunkFilename = this.outputOptions.hotUpdateChunkFilename;
  72. const hotUpdateMainFilename = this.outputOptions.hotUpdateMainFilename;
  73. const hotUpdateFunction = this.outputOptions.hotUpdateFunction || Template.toIdentifier("webpackHotUpdate" + (this.outputOptions.library || ""));
  74. const currentHotUpdateChunkFilename = this.applyPluginsWaterfall("asset-path", JSON.stringify(hotUpdateChunkFilename), {
  75. hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"",
  76. hashWithLength: function(length) {
  77. return "\" + " + this.renderCurrentHashCode(hash, length) + " + \"";
  78. }.bind(this),
  79. chunk: {
  80. id: "\" + chunkId + \""
  81. }
  82. });
  83. const currentHotUpdateMainFilename = this.applyPluginsWaterfall("asset-path", JSON.stringify(hotUpdateMainFilename), {
  84. hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"",
  85. hashWithLength: function(length) {
  86. return "\" + " + this.renderCurrentHashCode(hash, length) + " + \"";
  87. }.bind(this)
  88. });
  89. return source + "\n" +
  90. "var parentHotUpdateCallback = this[" + JSON.stringify(hotUpdateFunction) + "];\n" +
  91. "this[" + JSON.stringify(hotUpdateFunction) + "] = " + Template.getFunctionContent(require("./WebWorkerMainTemplate.runtime.js"))
  92. .replace(/\/\/\$semicolon/g, ";")
  93. .replace(/\$require\$/g, this.requireFn)
  94. .replace(/\$hotMainFilename\$/g, currentHotUpdateMainFilename)
  95. .replace(/\$hotChunkFilename\$/g, currentHotUpdateChunkFilename)
  96. .replace(/\$hash\$/g, JSON.stringify(hash));
  97. });
  98. mainTemplate.plugin("hash", function(hash) {
  99. hash.update("webworker");
  100. hash.update("3");
  101. hash.update(`${this.outputOptions.publicPath}`);
  102. hash.update(`${this.outputOptions.filename}`);
  103. hash.update(`${this.outputOptions.chunkFilename}`);
  104. hash.update(`${this.outputOptions.chunkCallbackName}`);
  105. hash.update(`${this.outputOptions.library}`);
  106. });
  107. }
  108. }
  109. module.exports = WebWorkerMainTemplatePlugin;