ModuleDecoratorDependency.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const Dependency = require("../Dependency");
  7. const InitFragment = require("../InitFragment");
  8. const RuntimeGlobals = require("../RuntimeGlobals");
  9. const makeSerializable = require("../util/makeSerializable");
  10. const NullDependency = require("./NullDependency");
  11. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  12. /** @typedef {import("../Dependency").ReferencedExports} ReferencedExports */
  13. /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
  14. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  15. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  16. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  17. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  18. /** @typedef {import("../util/Hash")} Hash */
  19. /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
  20. class ModuleDecoratorDependency extends NullDependency {
  21. /**
  22. * @param {string} decorator the decorator requirement
  23. * @param {boolean} allowExportsAccess allow to access exports from module
  24. */
  25. constructor(decorator, allowExportsAccess) {
  26. super();
  27. this.decorator = decorator;
  28. this.allowExportsAccess = allowExportsAccess;
  29. /** @type {undefined | string} */
  30. this._hashUpdate = undefined;
  31. }
  32. /**
  33. * @returns {string} a display name for the type of dependency
  34. */
  35. get type() {
  36. return "module decorator";
  37. }
  38. get category() {
  39. return "self";
  40. }
  41. /**
  42. * @returns {string | null} an identifier to merge equal requests
  43. */
  44. getResourceIdentifier() {
  45. return "self";
  46. }
  47. /**
  48. * Returns list of exports referenced by this dependency
  49. * @param {ModuleGraph} moduleGraph module graph
  50. * @param {RuntimeSpec} runtime the runtime for which the module is analysed
  51. * @returns {ReferencedExports} referenced exports
  52. */
  53. getReferencedExports(moduleGraph, runtime) {
  54. return this.allowExportsAccess
  55. ? Dependency.EXPORTS_OBJECT_REFERENCED
  56. : Dependency.NO_EXPORTS_REFERENCED;
  57. }
  58. /**
  59. * Update the hash
  60. * @param {Hash} hash hash to be updated
  61. * @param {UpdateHashContext} context context
  62. * @returns {void}
  63. */
  64. updateHash(hash, context) {
  65. if (this._hashUpdate === undefined) {
  66. this._hashUpdate = `${this.decorator}${this.allowExportsAccess}`;
  67. }
  68. hash.update(this._hashUpdate);
  69. }
  70. /**
  71. * @param {ObjectSerializerContext} context context
  72. */
  73. serialize(context) {
  74. const { write } = context;
  75. write(this.decorator);
  76. write(this.allowExportsAccess);
  77. super.serialize(context);
  78. }
  79. /**
  80. * @param {ObjectDeserializerContext} context context
  81. */
  82. deserialize(context) {
  83. const { read } = context;
  84. this.decorator = read();
  85. this.allowExportsAccess = read();
  86. super.deserialize(context);
  87. }
  88. }
  89. makeSerializable(
  90. ModuleDecoratorDependency,
  91. "webpack/lib/dependencies/ModuleDecoratorDependency"
  92. );
  93. ModuleDecoratorDependency.Template = class ModuleDecoratorDependencyTemplate extends (
  94. NullDependency.Template
  95. ) {
  96. /**
  97. * @param {Dependency} dependency the dependency for which the template should be applied
  98. * @param {ReplaceSource} source the current replace source which can be modified
  99. * @param {DependencyTemplateContext} templateContext the context object
  100. * @returns {void}
  101. */
  102. apply(
  103. dependency,
  104. source,
  105. { module, chunkGraph, initFragments, runtimeRequirements }
  106. ) {
  107. const dep = /** @type {ModuleDecoratorDependency} */ (dependency);
  108. runtimeRequirements.add(RuntimeGlobals.moduleLoaded);
  109. runtimeRequirements.add(RuntimeGlobals.moduleId);
  110. runtimeRequirements.add(RuntimeGlobals.module);
  111. runtimeRequirements.add(dep.decorator);
  112. initFragments.push(
  113. new InitFragment(
  114. `/* module decorator */ ${module.moduleArgument} = ${dep.decorator}(${module.moduleArgument});\n`,
  115. InitFragment.STAGE_PROVIDES,
  116. 0,
  117. `module decorator ${chunkGraph.getModuleId(module)}`
  118. )
  119. );
  120. }
  121. };
  122. module.exports = ModuleDecoratorDependency;