WorkerDependency.js 4.6 KB

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