ModuleDecoratorDependency.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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("../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 {(string[] | ReferencedExport)[]} 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. serialize(context) {
  70. const { write } = context;
  71. write(this.decorator);
  72. write(this.allowExportsAccess);
  73. super.serialize(context);
  74. }
  75. deserialize(context) {
  76. const { read } = context;
  77. this.decorator = read();
  78. this.allowExportsAccess = read();
  79. super.deserialize(context);
  80. }
  81. }
  82. makeSerializable(
  83. ModuleDecoratorDependency,
  84. "webpack/lib/dependencies/ModuleDecoratorDependency"
  85. );
  86. ModuleDecoratorDependency.Template = class ModuleDecoratorDependencyTemplate extends (
  87. NullDependency.Template
  88. ) {
  89. /**
  90. * @param {Dependency} dependency the dependency for which the template should be applied
  91. * @param {ReplaceSource} source the current replace source which can be modified
  92. * @param {DependencyTemplateContext} templateContext the context object
  93. * @returns {void}
  94. */
  95. apply(
  96. dependency,
  97. source,
  98. { module, chunkGraph, initFragments, runtimeRequirements }
  99. ) {
  100. const dep = /** @type {ModuleDecoratorDependency} */ (dependency);
  101. runtimeRequirements.add(RuntimeGlobals.moduleLoaded);
  102. runtimeRequirements.add(RuntimeGlobals.moduleId);
  103. runtimeRequirements.add(RuntimeGlobals.module);
  104. runtimeRequirements.add(dep.decorator);
  105. initFragments.push(
  106. new InitFragment(
  107. `/* module decorator */ ${module.moduleArgument} = ${dep.decorator}(${module.moduleArgument});\n`,
  108. InitFragment.STAGE_PROVIDES,
  109. 0,
  110. `module decorator ${chunkGraph.getModuleId(module)}`
  111. )
  112. );
  113. }
  114. };
  115. module.exports = ModuleDecoratorDependency;