CommonJsSelfReferenceDependency.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const RuntimeGlobals = require("../RuntimeGlobals");
  7. const { equals } = require("../util/ArrayHelpers");
  8. const makeSerializable = require("../util/makeSerializable");
  9. const propertyAccess = require("../util/propertyAccess");
  10. const NullDependency = require("./NullDependency");
  11. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  12. /** @typedef {import("../Dependency")} Dependency */
  13. /** @typedef {import("../Dependency").ExportsSpec} ExportsSpec */
  14. /** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
  15. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  16. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  17. /** @typedef {import("../javascript/JavascriptParser").Range} Range */
  18. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  19. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  20. /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
  21. /** @typedef {import("./CommonJsDependencyHelpers").CommonJSDependencyBaseKeywords} CommonJSDependencyBaseKeywords */
  22. class CommonJsSelfReferenceDependency extends NullDependency {
  23. /**
  24. * @param {Range} range range
  25. * @param {CommonJSDependencyBaseKeywords} base base
  26. * @param {string[]} names names
  27. * @param {boolean} call is a call
  28. */
  29. constructor(range, base, names, call) {
  30. super();
  31. this.range = range;
  32. this.base = base;
  33. this.names = names;
  34. this.call = call;
  35. }
  36. get type() {
  37. return "cjs self exports reference";
  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.call ? this.names.slice(0, -1) : this.names];
  56. }
  57. /**
  58. * @param {ObjectSerializerContext} context context
  59. */
  60. serialize(context) {
  61. const { write } = context;
  62. write(this.range);
  63. write(this.base);
  64. write(this.names);
  65. write(this.call);
  66. super.serialize(context);
  67. }
  68. /**
  69. * @param {ObjectDeserializerContext} context context
  70. */
  71. deserialize(context) {
  72. const { read } = context;
  73. this.range = read();
  74. this.base = read();
  75. this.names = read();
  76. this.call = read();
  77. super.deserialize(context);
  78. }
  79. }
  80. makeSerializable(
  81. CommonJsSelfReferenceDependency,
  82. "webpack/lib/dependencies/CommonJsSelfReferenceDependency"
  83. );
  84. CommonJsSelfReferenceDependency.Template = class CommonJsSelfReferenceDependencyTemplate extends (
  85. NullDependency.Template
  86. ) {
  87. /**
  88. * @param {Dependency} dependency the dependency for which the template should be applied
  89. * @param {ReplaceSource} source the current replace source which can be modified
  90. * @param {DependencyTemplateContext} templateContext the context object
  91. * @returns {void}
  92. */
  93. apply(
  94. dependency,
  95. source,
  96. { module, moduleGraph, runtime, runtimeRequirements }
  97. ) {
  98. const dep = /** @type {CommonJsSelfReferenceDependency} */ (dependency);
  99. let used;
  100. if (dep.names.length === 0) {
  101. used = dep.names;
  102. } else {
  103. used = moduleGraph.getExportsInfo(module).getUsedName(dep.names, runtime);
  104. }
  105. if (!used) {
  106. throw new Error(
  107. "Self-reference dependency has unused export name: This should not happen"
  108. );
  109. }
  110. let base = undefined;
  111. switch (dep.base) {
  112. case "exports":
  113. runtimeRequirements.add(RuntimeGlobals.exports);
  114. base = module.exportsArgument;
  115. break;
  116. case "module.exports":
  117. runtimeRequirements.add(RuntimeGlobals.module);
  118. base = `${module.moduleArgument}.exports`;
  119. break;
  120. case "this":
  121. runtimeRequirements.add(RuntimeGlobals.thisAsExports);
  122. base = "this";
  123. break;
  124. default:
  125. throw new Error(`Unsupported base ${dep.base}`);
  126. }
  127. if (base === dep.base && equals(used, dep.names)) {
  128. // Nothing has to be changed
  129. // We don't use a replacement for compat reasons
  130. // for plugins that update `module._source` which they
  131. // shouldn't do!
  132. return;
  133. }
  134. source.replace(
  135. dep.range[0],
  136. dep.range[1] - 1,
  137. `${base}${propertyAccess(used)}`
  138. );
  139. }
  140. };
  141. module.exports = CommonJsSelfReferenceDependency;