ModuleInitFragmentDependency.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Natsu @xiaoxiaojx
  4. */
  5. "use strict";
  6. const InitFragment = require("../InitFragment");
  7. const makeSerializable = require("../util/makeSerializable");
  8. const NullDependency = require("./NullDependency");
  9. /** @import { ReplaceSource } from "webpack-sources" */
  10. /** @import Dependency from "../Dependency" */
  11. /** @import { DependencyTemplateContext } from "../DependencyTemplate" */
  12. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext<[string, string[], string | undefined, string | undefined]>} ObjectSerializerContext */
  13. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext<[string, string[], string | undefined, string | undefined]>} ObjectDeserializerContext */
  14. /**
  15. * A dependency that adds an init fragment to the module
  16. */
  17. class ModuleInitFragmentDependency extends NullDependency {
  18. /**
  19. * Creates an instance of ModuleInitFragmentDependency.
  20. * @param {string} initCode the initialization code
  21. * @param {string[]} runtimeRequirements runtime requirements
  22. * @param {string=} key unique key to avoid emitting the same initialization code twice
  23. * @param {string=} buildTimeInitCode initialization code for build-time execution, when `initCode` is only valid in emitted output
  24. */
  25. constructor(initCode, runtimeRequirements, key, buildTimeInitCode) {
  26. super();
  27. /** @type {string} */
  28. this.initCode = initCode;
  29. /** @type {string[]} */
  30. this.runtimeRequirements = runtimeRequirements;
  31. /** @type {string | undefined} */
  32. this.key = key;
  33. /** @type {string | undefined} */
  34. this.buildTimeInitCode = buildTimeInitCode;
  35. }
  36. /**
  37. * Serializes this instance into the provided serializer context.
  38. * @param {ObjectSerializerContext} context context
  39. */
  40. serialize(context) {
  41. context
  42. .write(this.initCode)
  43. .write(this.runtimeRequirements)
  44. .write(this.key)
  45. .write(this.buildTimeInitCode);
  46. super.serialize(context);
  47. }
  48. /**
  49. * Restores this instance from the provided deserializer context.
  50. * @param {ObjectDeserializerContext} context context
  51. */
  52. deserialize(context) {
  53. this.initCode = context.read();
  54. const c1 = context.rest;
  55. this.runtimeRequirements = c1.read();
  56. const c2 = c1.rest;
  57. this.key = c2.read();
  58. const c3 = c2.rest;
  59. this.buildTimeInitCode = c3.read();
  60. super.deserialize(c3.rest);
  61. }
  62. }
  63. makeSerializable(
  64. ModuleInitFragmentDependency,
  65. "webpack/lib/dependencies/ModuleInitFragmentDependency"
  66. );
  67. ModuleInitFragmentDependency.Template = class ModuleInitFragmentDependencyTemplate extends (
  68. NullDependency.Template
  69. ) {
  70. /**
  71. * Applies the plugin by registering its hooks on the compiler.
  72. * @param {Dependency} dependency the dependency for which the template should be applied
  73. * @param {ReplaceSource} source the current replace source which can be modified
  74. * @param {DependencyTemplateContext} templateContext the context object
  75. * @returns {void}
  76. */
  77. apply(
  78. dependency,
  79. source,
  80. { chunkGraph, initFragments, runtimeRequirements }
  81. ) {
  82. const dep = /** @type {ModuleInitFragmentDependency} */ (dependency);
  83. for (const req of dep.runtimeRequirements) {
  84. runtimeRequirements.add(req);
  85. }
  86. const initCode =
  87. chunkGraph.buildTimeExecution && dep.buildTimeInitCode !== undefined
  88. ? dep.buildTimeInitCode
  89. : dep.initCode;
  90. initFragments.push(
  91. new InitFragment(
  92. initCode,
  93. InitFragment.STAGE_CONSTANTS,
  94. 0,
  95. dep.key,
  96. undefined
  97. )
  98. );
  99. }
  100. };
  101. module.exports = ModuleInitFragmentDependency;