AMDRequireArrayDependency.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const DependencyTemplate = require("../DependencyTemplate");
  7. const makeSerializable = require("../util/makeSerializable");
  8. const NullDependency = require("./NullDependency");
  9. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  10. /** @typedef {import("../Dependency")} Dependency */
  11. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  12. /** @typedef {import("../javascript/JavascriptParser").Range} Range */
  13. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  14. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  15. /** @typedef {import("./AMDRequireItemDependency")} AMDRequireItemDependency */
  16. /** @typedef {import("./LocalModuleDependency")} LocalModuleDependency */
  17. class AMDRequireArrayDependency extends NullDependency {
  18. /**
  19. * @param {(string | LocalModuleDependency | AMDRequireItemDependency)[]} depsArray deps array
  20. * @param {Range} range range
  21. */
  22. constructor(depsArray, range) {
  23. super();
  24. this.depsArray = depsArray;
  25. this.range = range;
  26. }
  27. get type() {
  28. return "amd require array";
  29. }
  30. get category() {
  31. return "amd";
  32. }
  33. /**
  34. * @param {ObjectSerializerContext} context context
  35. */
  36. serialize(context) {
  37. const { write } = context;
  38. write(this.depsArray);
  39. write(this.range);
  40. super.serialize(context);
  41. }
  42. /**
  43. * @param {ObjectDeserializerContext} context context
  44. */
  45. deserialize(context) {
  46. const { read } = context;
  47. this.depsArray = read();
  48. this.range = read();
  49. super.deserialize(context);
  50. }
  51. }
  52. makeSerializable(
  53. AMDRequireArrayDependency,
  54. "webpack/lib/dependencies/AMDRequireArrayDependency"
  55. );
  56. AMDRequireArrayDependency.Template = class AMDRequireArrayDependencyTemplate extends (
  57. DependencyTemplate
  58. ) {
  59. /**
  60. * @param {Dependency} dependency the dependency for which the template should be applied
  61. * @param {ReplaceSource} source the current replace source which can be modified
  62. * @param {DependencyTemplateContext} templateContext the context object
  63. * @returns {void}
  64. */
  65. apply(dependency, source, templateContext) {
  66. const dep = /** @type {AMDRequireArrayDependency} */ (dependency);
  67. const content = this.getContent(dep, templateContext);
  68. source.replace(dep.range[0], dep.range[1] - 1, content);
  69. }
  70. /**
  71. * @param {AMDRequireArrayDependency} dep the dependency for which the template should be applied
  72. * @param {DependencyTemplateContext} templateContext the context object
  73. * @returns {string} content
  74. */
  75. getContent(dep, templateContext) {
  76. const requires = dep.depsArray.map(dependency => {
  77. return this.contentForDependency(dependency, templateContext);
  78. });
  79. return `[${requires.join(", ")}]`;
  80. }
  81. /**
  82. * @param {TODO} dep the dependency for which the template should be applied
  83. * @param {DependencyTemplateContext} templateContext the context object
  84. * @returns {string} content
  85. */
  86. contentForDependency(
  87. dep,
  88. { runtimeTemplate, moduleGraph, chunkGraph, runtimeRequirements }
  89. ) {
  90. if (typeof dep === "string") {
  91. return dep;
  92. }
  93. if (dep.localModule) {
  94. return dep.localModule.variableName();
  95. } else {
  96. return runtimeTemplate.moduleExports({
  97. module: moduleGraph.getModule(dep),
  98. chunkGraph,
  99. request: dep.request,
  100. runtimeRequirements
  101. });
  102. }
  103. }
  104. };
  105. module.exports = AMDRequireArrayDependency;