AMDRequireArrayDependency.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const Dependency = require("../Dependency");
  7. const webpackMissingModuleModule = require("./WebpackMissingModule").module;
  8. class AMDRequireArrayDependency extends Dependency {
  9. constructor(depsArray, range) {
  10. super();
  11. this.depsArray = depsArray;
  12. this.range = range;
  13. }
  14. get type() {
  15. return "amd require array";
  16. }
  17. }
  18. AMDRequireArrayDependency.Template = class AMDRequireArrayDependencyTemplate {
  19. apply(dep, source, outputOptions, requestShortener) {
  20. const content = this.getContent(dep, outputOptions, requestShortener);
  21. source.replace(dep.range[0], dep.range[1] - 1, content);
  22. }
  23. getContent(dep, outputOptions, requestShortener) {
  24. const requires = dep.depsArray.map((dependency) => {
  25. const optionalComment = this.optionalComment(outputOptions.pathinfo, requestShortener.shorten(dependency.request));
  26. return this.contentForDependency(dependency, optionalComment);
  27. });
  28. return `[${requires.join(", ")}]`;
  29. }
  30. optionalComment(pathInfo, shortenedRequest) {
  31. if(!pathInfo) {
  32. return "";
  33. }
  34. return `/*! ${shortenedRequest} */ `;
  35. }
  36. contentForDependency(dep, comment) {
  37. if(typeof dep === "string") {
  38. return dep;
  39. }
  40. if(dep.module) {
  41. const stringifiedId = JSON.stringify(dep.module.id);
  42. return `__webpack_require__(${comment}${stringifiedId})`;
  43. } else if(dep.localModule) {
  44. return dep.localModule.variableName();
  45. }
  46. return webpackMissingModuleModule(dep.request);
  47. }
  48. };
  49. module.exports = AMDRequireArrayDependency;