WorkerDependency.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Ivan Kopeykin @vankop
  4. */
  5. "use strict";
  6. const Dependency = require("../Dependency");
  7. const RuntimeGlobals = require("../RuntimeGlobals");
  8. const makeSerializable = require("../util/makeSerializable");
  9. const ModuleDependency = require("./ModuleDependency");
  10. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  11. /** @typedef {import("../AsyncDependenciesBlock")} AsyncDependenciesBlock */
  12. /** @typedef {import("../ChunkGraph")} ChunkGraph */
  13. /** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
  14. /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
  15. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  16. /** @typedef {import("../Entrypoint")} Entrypoint */
  17. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  18. /** @typedef {import("../javascript/JavascriptParser").Range} Range */
  19. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  20. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  21. /** @typedef {import("../util/Hash")} Hash */
  22. /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
  23. class WorkerDependency extends ModuleDependency {
  24. /**
  25. * @param {string} request request
  26. * @param {Range} range range
  27. * @param {object} workerDependencyOptions options
  28. * @param {string=} workerDependencyOptions.publicPath public path for the worker
  29. */
  30. constructor(request, range, workerDependencyOptions) {
  31. super(request);
  32. this.range = range;
  33. // If options are updated, don't forget to update the hash and serialization functions
  34. this.options = workerDependencyOptions;
  35. /** Cache the hash */
  36. this._hashUpdate = undefined;
  37. }
  38. /**
  39. * Returns list of exports referenced by this dependency
  40. * @param {ModuleGraph} moduleGraph module graph
  41. * @param {RuntimeSpec} runtime the runtime for which the module is analysed
  42. * @returns {(string[] | ReferencedExport)[]} referenced exports
  43. */
  44. getReferencedExports(moduleGraph, runtime) {
  45. return Dependency.NO_EXPORTS_REFERENCED;
  46. }
  47. get type() {
  48. return "new Worker()";
  49. }
  50. get category() {
  51. return "worker";
  52. }
  53. /**
  54. * Update the hash
  55. * @param {Hash} hash hash to be updated
  56. * @param {UpdateHashContext} context context
  57. * @returns {void}
  58. */
  59. updateHash(hash, context) {
  60. if (this._hashUpdate === undefined) {
  61. this._hashUpdate = JSON.stringify(this.options);
  62. }
  63. hash.update(this._hashUpdate);
  64. }
  65. /**
  66. * @param {ObjectSerializerContext} context context
  67. */
  68. serialize(context) {
  69. const { write } = context;
  70. write(this.options);
  71. super.serialize(context);
  72. }
  73. /**
  74. * @param {ObjectDeserializerContext} context context
  75. */
  76. deserialize(context) {
  77. const { read } = context;
  78. this.options = read();
  79. super.deserialize(context);
  80. }
  81. }
  82. WorkerDependency.Template = class WorkerDependencyTemplate extends (
  83. ModuleDependency.Template
  84. ) {
  85. /**
  86. * @param {Dependency} dependency the dependency for which the template should be applied
  87. * @param {ReplaceSource} source the current replace source which can be modified
  88. * @param {DependencyTemplateContext} templateContext the context object
  89. * @returns {void}
  90. */
  91. apply(dependency, source, templateContext) {
  92. const { chunkGraph, moduleGraph, runtimeRequirements } = templateContext;
  93. const dep = /** @type {WorkerDependency} */ (dependency);
  94. const block = /** @type {AsyncDependenciesBlock} */ (
  95. moduleGraph.getParentBlock(dependency)
  96. );
  97. const entrypoint = /** @type {Entrypoint} */ (
  98. chunkGraph.getBlockChunkGroup(block)
  99. );
  100. const chunk = entrypoint.getEntrypointChunk();
  101. // We use the workerPublicPath option if provided, else we fallback to the RuntimeGlobal publicPath
  102. const workerImportBaseUrl = dep.options.publicPath
  103. ? `"${dep.options.publicPath}"`
  104. : RuntimeGlobals.publicPath;
  105. runtimeRequirements.add(RuntimeGlobals.publicPath);
  106. runtimeRequirements.add(RuntimeGlobals.baseURI);
  107. runtimeRequirements.add(RuntimeGlobals.getChunkScriptFilename);
  108. source.replace(
  109. dep.range[0],
  110. dep.range[1] - 1,
  111. `/* worker import */ ${workerImportBaseUrl} + ${
  112. RuntimeGlobals.getChunkScriptFilename
  113. }(${JSON.stringify(chunk.id)}), ${RuntimeGlobals.baseURI}`
  114. );
  115. }
  116. };
  117. makeSerializable(WorkerDependency, "webpack/lib/dependencies/WorkerDependency");
  118. module.exports = WorkerDependency;