WorkerDependency.js 4.0 KB

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