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