WorkerDependency.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. */
  25. constructor(request, range) {
  26. super(request);
  27. this.range = range;
  28. }
  29. /**
  30. * Returns list of exports referenced by this dependency
  31. * @param {ModuleGraph} moduleGraph module graph
  32. * @param {RuntimeSpec} runtime the runtime for which the module is analysed
  33. * @returns {(string[] | ReferencedExport)[]} referenced exports
  34. */
  35. getReferencedExports(moduleGraph, runtime) {
  36. return Dependency.NO_EXPORTS_REFERENCED;
  37. }
  38. get type() {
  39. return "new Worker()";
  40. }
  41. get category() {
  42. return "worker";
  43. }
  44. }
  45. WorkerDependency.Template = class WorkerDependencyTemplate extends (
  46. ModuleDependency.Template
  47. ) {
  48. /**
  49. * @param {Dependency} dependency the dependency for which the template should be applied
  50. * @param {ReplaceSource} source the current replace source which can be modified
  51. * @param {DependencyTemplateContext} templateContext the context object
  52. * @returns {void}
  53. */
  54. apply(dependency, source, templateContext) {
  55. const { chunkGraph, moduleGraph, runtimeRequirements } = templateContext;
  56. const dep = /** @type {WorkerDependency} */ (dependency);
  57. const block = /** @type {AsyncDependenciesBlock} */ (
  58. moduleGraph.getParentBlock(dependency)
  59. );
  60. const entrypoint = /** @type {Entrypoint} */ (
  61. chunkGraph.getBlockChunkGroup(block)
  62. );
  63. const chunk = entrypoint.getEntrypointChunk();
  64. runtimeRequirements.add(RuntimeGlobals.publicPath);
  65. runtimeRequirements.add(RuntimeGlobals.baseURI);
  66. runtimeRequirements.add(RuntimeGlobals.getChunkScriptFilename);
  67. source.replace(
  68. dep.range[0],
  69. dep.range[1] - 1,
  70. `/* worker import */ ${RuntimeGlobals.publicPath} + ${
  71. RuntimeGlobals.getChunkScriptFilename
  72. }(${JSON.stringify(chunk.id)}), ${RuntimeGlobals.baseURI}`
  73. );
  74. }
  75. };
  76. makeSerializable(WorkerDependency, "webpack/lib/dependencies/WorkerDependency");
  77. module.exports = WorkerDependency;