AMDRequireArrayDependency.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. class AMDRequireArrayDependency extends NullDependency {
  13. constructor(depsArray, range) {
  14. super();
  15. this.depsArray = depsArray;
  16. this.range = range;
  17. }
  18. get type() {
  19. return "amd require array";
  20. }
  21. get category() {
  22. return "amd";
  23. }
  24. serialize(context) {
  25. const { write } = context;
  26. write(this.depsArray);
  27. write(this.range);
  28. super.serialize(context);
  29. }
  30. deserialize(context) {
  31. const { read } = context;
  32. this.depsArray = read();
  33. this.range = read();
  34. super.deserialize(context);
  35. }
  36. }
  37. makeSerializable(
  38. AMDRequireArrayDependency,
  39. "webpack/lib/dependencies/AMDRequireArrayDependency"
  40. );
  41. AMDRequireArrayDependency.Template = class AMDRequireArrayDependencyTemplate extends (
  42. DependencyTemplate
  43. ) {
  44. /**
  45. * @param {Dependency} dependency the dependency for which the template should be applied
  46. * @param {ReplaceSource} source the current replace source which can be modified
  47. * @param {DependencyTemplateContext} templateContext the context object
  48. * @returns {void}
  49. */
  50. apply(dependency, source, templateContext) {
  51. const dep = /** @type {AMDRequireArrayDependency} */ (dependency);
  52. const content = this.getContent(dep, templateContext);
  53. source.replace(dep.range[0], dep.range[1] - 1, content);
  54. }
  55. getContent(dep, templateContext) {
  56. const requires = dep.depsArray.map(dependency => {
  57. return this.contentForDependency(dependency, templateContext);
  58. });
  59. return `[${requires.join(", ")}]`;
  60. }
  61. contentForDependency(
  62. dep,
  63. { runtimeTemplate, moduleGraph, chunkGraph, runtimeRequirements }
  64. ) {
  65. if (typeof dep === "string") {
  66. return dep;
  67. }
  68. if (dep.localModule) {
  69. return dep.localModule.variableName();
  70. } else {
  71. return runtimeTemplate.moduleExports({
  72. module: moduleGraph.getModule(dep),
  73. chunkGraph,
  74. request: dep.request,
  75. runtimeRequirements
  76. });
  77. }
  78. }
  79. };
  80. module.exports = AMDRequireArrayDependency;