ModuleInitFragmentDependency.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  10. /** @typedef {import("../Dependency")} Dependency */
  11. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  12. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  13. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  14. /**
  15. * A dependency that adds an init fragment to the module
  16. */
  17. class ModuleInitFragmentDependency extends NullDependency {
  18. /**
  19. * @param {string} initCode the initialization code
  20. * @param {string[]} runtimeRequirements runtime requirements
  21. * @param {string=} key unique key to avoid emitting the same initialization code twice
  22. */
  23. constructor(initCode, runtimeRequirements, key) {
  24. super();
  25. this.initCode = initCode;
  26. this.runtimeRequirements = runtimeRequirements;
  27. this.key = key;
  28. }
  29. /**
  30. * @param {ObjectSerializerContext} context context
  31. */
  32. serialize(context) {
  33. const { write } = context;
  34. write(this.initCode);
  35. write(this.runtimeRequirements);
  36. write(this.key);
  37. super.serialize(context);
  38. }
  39. /**
  40. * @param {ObjectDeserializerContext} context context
  41. */
  42. deserialize(context) {
  43. const { read } = context;
  44. this.initCode = read();
  45. this.runtimeRequirements = read();
  46. this.key = read();
  47. super.deserialize(context);
  48. }
  49. }
  50. makeSerializable(
  51. ModuleInitFragmentDependency,
  52. "webpack/lib/dependencies/ModuleInitFragmentDependency"
  53. );
  54. ModuleInitFragmentDependency.Template = class ModuleInitFragmentDependencyTemplate extends (
  55. NullDependency.Template
  56. ) {
  57. /**
  58. * @param {Dependency} dependency the dependency for which the template should be applied
  59. * @param {ReplaceSource} source the current replace source which can be modified
  60. * @param {DependencyTemplateContext} templateContext the context object
  61. * @returns {void}
  62. */
  63. apply(dependency, source, { initFragments, runtimeRequirements }) {
  64. const dep = /** @type {ModuleInitFragmentDependency} */ (dependency);
  65. for (const req of dep.runtimeRequirements) {
  66. runtimeRequirements.add(req);
  67. }
  68. initFragments.push(
  69. new InitFragment(
  70. dep.initCode,
  71. InitFragment.STAGE_CONSTANTS,
  72. 0,
  73. dep.key,
  74. undefined
  75. )
  76. );
  77. }
  78. };
  79. module.exports = ModuleInitFragmentDependency;