CommonJsFullRequireDependency.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const Template = require("../Template");
  7. const { equals } = require("../util/ArrayHelpers");
  8. const { getTrimmedIdsAndRange } = require("../util/chainedImports");
  9. const makeSerializable = require("../util/makeSerializable");
  10. const propertyAccess = require("../util/propertyAccess");
  11. const ModuleDependency = require("./ModuleDependency");
  12. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  13. /** @typedef {import("../Dependency")} Dependency */
  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. class CommonJsFullRequireDependency extends ModuleDependency {
  22. /**
  23. * @param {string} request the request string
  24. * @param {Range} range location in source code
  25. * @param {string[]} names accessed properties on module
  26. * @param {Range[]=} idRanges ranges for members of ids; the two arrays are right-aligned
  27. */
  28. constructor(
  29. request,
  30. range,
  31. names,
  32. idRanges /* TODO webpack 6 make this non-optional. It must always be set to properly trim ids. */
  33. ) {
  34. super(request);
  35. this.range = range;
  36. this.names = names;
  37. this.idRanges = idRanges;
  38. this.call = false;
  39. this.asiSafe = undefined;
  40. }
  41. /**
  42. * Returns list of exports referenced by this dependency
  43. * @param {ModuleGraph} moduleGraph module graph
  44. * @param {RuntimeSpec} runtime the runtime for which the module is analysed
  45. * @returns {(string[] | ReferencedExport)[]} referenced exports
  46. */
  47. getReferencedExports(moduleGraph, runtime) {
  48. if (this.call) {
  49. const importedModule = moduleGraph.getModule(this);
  50. if (
  51. !importedModule ||
  52. importedModule.getExportsType(moduleGraph, false) !== "namespace"
  53. ) {
  54. return [this.names.slice(0, -1)];
  55. }
  56. }
  57. return [this.names];
  58. }
  59. /**
  60. * @param {ObjectSerializerContext} context context
  61. */
  62. serialize(context) {
  63. const { write } = context;
  64. write(this.names);
  65. write(this.idRanges);
  66. write(this.call);
  67. write(this.asiSafe);
  68. super.serialize(context);
  69. }
  70. /**
  71. * @param {ObjectDeserializerContext} context context
  72. */
  73. deserialize(context) {
  74. const { read } = context;
  75. this.names = read();
  76. this.idRanges = read();
  77. this.call = read();
  78. this.asiSafe = read();
  79. super.deserialize(context);
  80. }
  81. get type() {
  82. return "cjs full require";
  83. }
  84. get category() {
  85. return "commonjs";
  86. }
  87. }
  88. CommonJsFullRequireDependency.Template = class CommonJsFullRequireDependencyTemplate extends (
  89. ModuleDependency.Template
  90. ) {
  91. /**
  92. * @param {Dependency} dependency the dependency for which the template should be applied
  93. * @param {ReplaceSource} source the current replace source which can be modified
  94. * @param {DependencyTemplateContext} templateContext the context object
  95. * @returns {void}
  96. */
  97. apply(
  98. dependency,
  99. source,
  100. {
  101. module,
  102. runtimeTemplate,
  103. moduleGraph,
  104. chunkGraph,
  105. runtimeRequirements,
  106. runtime,
  107. initFragments
  108. }
  109. ) {
  110. const dep = /** @type {CommonJsFullRequireDependency} */ (dependency);
  111. if (!dep.range) return;
  112. const importedModule = moduleGraph.getModule(dep);
  113. let requireExpr = runtimeTemplate.moduleExports({
  114. module: importedModule,
  115. chunkGraph,
  116. request: dep.request,
  117. weak: dep.weak,
  118. runtimeRequirements
  119. });
  120. const {
  121. trimmedRange: [trimmedRangeStart, trimmedRangeEnd],
  122. trimmedIds
  123. } = getTrimmedIdsAndRange(
  124. dep.names,
  125. dep.range,
  126. dep.idRanges,
  127. moduleGraph,
  128. dep
  129. );
  130. if (importedModule) {
  131. const usedImported = moduleGraph
  132. .getExportsInfo(importedModule)
  133. .getUsedName(trimmedIds, runtime);
  134. if (usedImported) {
  135. const comment = equals(usedImported, trimmedIds)
  136. ? ""
  137. : Template.toNormalComment(propertyAccess(trimmedIds)) + " ";
  138. const access = `${comment}${propertyAccess(usedImported)}`;
  139. requireExpr =
  140. dep.asiSafe === true
  141. ? `(${requireExpr}${access})`
  142. : `${requireExpr}${access}`;
  143. }
  144. }
  145. source.replace(trimmedRangeStart, trimmedRangeEnd - 1, requireExpr);
  146. }
  147. };
  148. makeSerializable(
  149. CommonJsFullRequireDependency,
  150. "webpack/lib/dependencies/CommonJsFullRequireDependency"
  151. );
  152. module.exports = CommonJsFullRequireDependency;