WorkerDependency.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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("../Dependency").ReferencedExports} ReferencedExports */
  13. /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
  14. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  15. /** @typedef {import("../Entrypoint")} Entrypoint */
  16. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  17. /** @typedef {import("../javascript/JavascriptParser").Range} Range */
  18. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  19. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  20. /** @typedef {import("../util/Hash")} Hash */
  21. /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
  22. /**
  23. * Represents the worker dependency runtime component.
  24. * @typedef {object} WorkerDependencyOptions
  25. * @property {string=} publicPath public path for the worker
  26. * @property {boolean=} needNewUrl true when need generate `new URL(...)`, otherwise false
  27. */
  28. class WorkerDependency extends ModuleDependency {
  29. /**
  30. * Creates an instance of WorkerDependency.
  31. * @param {string} request request
  32. * @param {Range} range range
  33. * @param {WorkerDependencyOptions} workerDependencyOptions options
  34. */
  35. constructor(request, range, workerDependencyOptions) {
  36. super(request);
  37. this.range = range;
  38. // If options are updated, don't forget to update the hash and serialization functions
  39. /** @type {WorkerDependencyOptions} */
  40. this.options = workerDependencyOptions;
  41. /** Cache the hash */
  42. /** @type {undefined | string} */
  43. this._hashUpdate = undefined;
  44. }
  45. /**
  46. * Returns list of exports referenced by this dependency
  47. * @param {ModuleGraph} moduleGraph module graph
  48. * @param {RuntimeSpec} runtime the runtime for which the module is analysed
  49. * @returns {ReferencedExports} referenced exports
  50. */
  51. getReferencedExports(moduleGraph, runtime) {
  52. return Dependency.NO_EXPORTS_REFERENCED;
  53. }
  54. get type() {
  55. return "new Worker()";
  56. }
  57. get category() {
  58. return "worker";
  59. }
  60. /**
  61. * Updates the hash with the data contributed by this instance.
  62. * @param {Hash} hash hash to be updated
  63. * @param {UpdateHashContext} context context
  64. * @returns {void}
  65. */
  66. updateHash(hash, context) {
  67. if (this._hashUpdate === undefined) {
  68. this._hashUpdate = JSON.stringify(this.options);
  69. }
  70. hash.update(this._hashUpdate);
  71. }
  72. /**
  73. * Serializes this instance into the provided serializer context.
  74. * @param {ObjectSerializerContext} context context
  75. */
  76. serialize(context) {
  77. const { write } = context;
  78. write(this.options);
  79. super.serialize(context);
  80. }
  81. /**
  82. * Restores this instance from the provided deserializer context.
  83. * @param {ObjectDeserializerContext} context context
  84. */
  85. deserialize(context) {
  86. const { read } = context;
  87. this.options = read();
  88. super.deserialize(context);
  89. }
  90. }
  91. WorkerDependency.Template = class WorkerDependencyTemplate extends (
  92. ModuleDependency.Template
  93. ) {
  94. /**
  95. * Applies the plugin by registering its hooks on the compiler.
  96. * @param {Dependency} dependency the dependency for which the template should be applied
  97. * @param {ReplaceSource} source the current replace source which can be modified
  98. * @param {DependencyTemplateContext} templateContext the context object
  99. * @returns {void}
  100. */
  101. apply(dependency, source, templateContext) {
  102. const { chunkGraph, moduleGraph, runtimeRequirements } = templateContext;
  103. const dep = /** @type {WorkerDependency} */ (dependency);
  104. const block = /** @type {AsyncDependenciesBlock} */ (
  105. moduleGraph.getParentBlock(dependency)
  106. );
  107. const entrypoint = /** @type {Entrypoint} */ (
  108. chunkGraph.getBlockChunkGroup(block)
  109. );
  110. const chunk = entrypoint.getEntrypointChunk();
  111. // We use the workerPublicPath option if provided, else we fallback to the RuntimeGlobal publicPath
  112. const workerImportBaseUrl = dep.options.publicPath
  113. ? `"${dep.options.publicPath}"`
  114. : RuntimeGlobals.publicPath;
  115. runtimeRequirements.add(RuntimeGlobals.publicPath);
  116. runtimeRequirements.add(RuntimeGlobals.baseURI);
  117. runtimeRequirements.add(RuntimeGlobals.getChunkScriptFilename);
  118. const workerImportStr = `/* worker import */ ${workerImportBaseUrl} + ${
  119. RuntimeGlobals.getChunkScriptFilename
  120. }(${JSON.stringify(chunk.id)}), ${RuntimeGlobals.baseURI}`;
  121. source.replace(
  122. dep.range[0],
  123. dep.range[1] - 1,
  124. dep.options.needNewUrl ? `new URL(${workerImportStr})` : workerImportStr
  125. );
  126. }
  127. };
  128. makeSerializable(WorkerDependency, "webpack/lib/dependencies/WorkerDependency");
  129. module.exports = WorkerDependency;