CommonJsSelfReferenceDependency.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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("../util/runtime").RuntimeSpec} RuntimeSpec */
  18. class CommonJsSelfReferenceDependency extends NullDependency {
  19. constructor(range, base, names, call) {
  20. super();
  21. this.range = range;
  22. this.base = base;
  23. this.names = names;
  24. this.call = call;
  25. }
  26. get type() {
  27. return "cjs self exports reference";
  28. }
  29. get category() {
  30. return "self";
  31. }
  32. /**
  33. * @returns {string | null} an identifier to merge equal requests
  34. */
  35. getResourceIdentifier() {
  36. return `self`;
  37. }
  38. /**
  39. * Returns list of exports referenced by this dependency
  40. * @param {ModuleGraph} moduleGraph module graph
  41. * @param {RuntimeSpec} runtime the runtime for which the module is analysed
  42. * @returns {(string[] | ReferencedExport)[]} referenced exports
  43. */
  44. getReferencedExports(moduleGraph, runtime) {
  45. return [this.call ? this.names.slice(0, -1) : this.names];
  46. }
  47. serialize(context) {
  48. const { write } = context;
  49. write(this.range);
  50. write(this.base);
  51. write(this.names);
  52. write(this.call);
  53. super.serialize(context);
  54. }
  55. deserialize(context) {
  56. const { read } = context;
  57. this.range = read();
  58. this.base = read();
  59. this.names = read();
  60. this.call = read();
  61. super.deserialize(context);
  62. }
  63. }
  64. makeSerializable(
  65. CommonJsSelfReferenceDependency,
  66. "webpack/lib/dependencies/CommonJsSelfReferenceDependency"
  67. );
  68. CommonJsSelfReferenceDependency.Template = class CommonJsSelfReferenceDependencyTemplate extends (
  69. NullDependency.Template
  70. ) {
  71. /**
  72. * @param {Dependency} dependency the dependency for which the template should be applied
  73. * @param {ReplaceSource} source the current replace source which can be modified
  74. * @param {DependencyTemplateContext} templateContext the context object
  75. * @returns {void}
  76. */
  77. apply(
  78. dependency,
  79. source,
  80. { module, moduleGraph, runtime, runtimeRequirements }
  81. ) {
  82. const dep = /** @type {CommonJsSelfReferenceDependency} */ (dependency);
  83. let used;
  84. if (dep.names.length === 0) {
  85. used = dep.names;
  86. } else {
  87. used = moduleGraph.getExportsInfo(module).getUsedName(dep.names, runtime);
  88. }
  89. if (!used) {
  90. throw new Error(
  91. "Self-reference dependency has unused export name: This should not happen"
  92. );
  93. }
  94. let base = undefined;
  95. switch (dep.base) {
  96. case "exports":
  97. runtimeRequirements.add(RuntimeGlobals.exports);
  98. base = module.exportsArgument;
  99. break;
  100. case "module.exports":
  101. runtimeRequirements.add(RuntimeGlobals.module);
  102. base = `${module.moduleArgument}.exports`;
  103. break;
  104. case "this":
  105. runtimeRequirements.add(RuntimeGlobals.thisAsExports);
  106. base = "this";
  107. break;
  108. default:
  109. throw new Error(`Unsupported base ${dep.base}`);
  110. }
  111. if (base === dep.base && equals(used, dep.names)) {
  112. // Nothing has to be changed
  113. // We don't use a replacement for compat reasons
  114. // for plugins that update `module._source` which they
  115. // shouldn't do!
  116. return;
  117. }
  118. source.replace(
  119. dep.range[0],
  120. dep.range[1] - 1,
  121. `${base}${propertyAccess(used)}`
  122. );
  123. }
  124. };
  125. module.exports = CommonJsSelfReferenceDependency;