ModuleDecoratorDependency.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. this._hashUpdate = undefined;
  30. }
  31. /**
  32. * @returns {string} a display name for the type of dependency
  33. */
  34. get type() {
  35. return "module decorator";
  36. }
  37. get category() {
  38. return "self";
  39. }
  40. /**
  41. * @returns {string | null} an identifier to merge equal requests
  42. */
  43. getResourceIdentifier() {
  44. return "self";
  45. }
  46. /**
  47. * Returns list of exports referenced by this dependency
  48. * @param {ModuleGraph} moduleGraph module graph
  49. * @param {RuntimeSpec} runtime the runtime for which the module is analysed
  50. * @returns {ReferencedExports} referenced exports
  51. */
  52. getReferencedExports(moduleGraph, runtime) {
  53. return this.allowExportsAccess
  54. ? Dependency.EXPORTS_OBJECT_REFERENCED
  55. : Dependency.NO_EXPORTS_REFERENCED;
  56. }
  57. /**
  58. * Update the hash
  59. * @param {Hash} hash hash to be updated
  60. * @param {UpdateHashContext} context context
  61. * @returns {void}
  62. */
  63. updateHash(hash, context) {
  64. if (this._hashUpdate === undefined) {
  65. this._hashUpdate = `${this.decorator}${this.allowExportsAccess}`;
  66. }
  67. hash.update(this._hashUpdate);
  68. }
  69. /**
  70. * @param {ObjectSerializerContext} context context
  71. */
  72. serialize(context) {
  73. const { write } = context;
  74. write(this.decorator);
  75. write(this.allowExportsAccess);
  76. super.serialize(context);
  77. }
  78. /**
  79. * @param {ObjectDeserializerContext} context context
  80. */
  81. deserialize(context) {
  82. const { read } = context;
  83. this.decorator = read();
  84. this.allowExportsAccess = read();
  85. super.deserialize(context);
  86. }
  87. }
  88. makeSerializable(
  89. ModuleDecoratorDependency,
  90. "webpack/lib/dependencies/ModuleDecoratorDependency"
  91. );
  92. ModuleDecoratorDependency.Template = class ModuleDecoratorDependencyTemplate extends (
  93. NullDependency.Template
  94. ) {
  95. /**
  96. * @param {Dependency} dependency the dependency for which the template should be applied
  97. * @param {ReplaceSource} source the current replace source which can be modified
  98. * @param {DependencyTemplateContext} templateContext the context object
  99. * @returns {void}
  100. */
  101. apply(
  102. dependency,
  103. source,
  104. { module, chunkGraph, initFragments, runtimeRequirements }
  105. ) {
  106. const dep = /** @type {ModuleDecoratorDependency} */ (dependency);
  107. runtimeRequirements.add(RuntimeGlobals.moduleLoaded);
  108. runtimeRequirements.add(RuntimeGlobals.moduleId);
  109. runtimeRequirements.add(RuntimeGlobals.module);
  110. runtimeRequirements.add(dep.decorator);
  111. initFragments.push(
  112. new InitFragment(
  113. `/* module decorator */ ${module.moduleArgument} = ${dep.decorator}(${module.moduleArgument});\n`,
  114. InitFragment.STAGE_PROVIDES,
  115. 0,
  116. `module decorator ${chunkGraph.getModuleId(module)}`
  117. )
  118. );
  119. }
  120. };
  121. module.exports = ModuleDecoratorDependency;