ModuleInitFragmentDependency.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. * 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. */
  24. constructor(initCode, runtimeRequirements, key) {
  25. super();
  26. this.initCode = initCode;
  27. this.runtimeRequirements = runtimeRequirements;
  28. this.key = key;
  29. }
  30. /**
  31. * Serializes this instance into the provided serializer context.
  32. * @param {ObjectSerializerContext} context context
  33. */
  34. serialize(context) {
  35. const { write } = context;
  36. write(this.initCode);
  37. write(this.runtimeRequirements);
  38. write(this.key);
  39. super.serialize(context);
  40. }
  41. /**
  42. * Restores this instance from the provided deserializer context.
  43. * @param {ObjectDeserializerContext} context context
  44. */
  45. deserialize(context) {
  46. const { read } = context;
  47. this.initCode = read();
  48. this.runtimeRequirements = read();
  49. this.key = read();
  50. super.deserialize(context);
  51. }
  52. }
  53. makeSerializable(
  54. ModuleInitFragmentDependency,
  55. "webpack/lib/dependencies/ModuleInitFragmentDependency"
  56. );
  57. ModuleInitFragmentDependency.Template = class ModuleInitFragmentDependencyTemplate extends (
  58. NullDependency.Template
  59. ) {
  60. /**
  61. * Applies the plugin by registering its hooks on the compiler.
  62. * @param {Dependency} dependency the dependency for which the template should be applied
  63. * @param {ReplaceSource} source the current replace source which can be modified
  64. * @param {DependencyTemplateContext} templateContext the context object
  65. * @returns {void}
  66. */
  67. apply(dependency, source, { initFragments, runtimeRequirements }) {
  68. const dep = /** @type {ModuleInitFragmentDependency} */ (dependency);
  69. for (const req of dep.runtimeRequirements) {
  70. runtimeRequirements.add(req);
  71. }
  72. initFragments.push(
  73. new InitFragment(
  74. dep.initCode,
  75. InitFragment.STAGE_CONSTANTS,
  76. 0,
  77. dep.key,
  78. undefined
  79. )
  80. );
  81. }
  82. };
  83. module.exports = ModuleInitFragmentDependency;