WorkerDependency.js 4.6 KB

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