DelegatedModule.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { OriginalSource, RawSource } = require("webpack-sources");
  7. const Module = require("./Module");
  8. const WebpackMissingModule = require("./dependencies/WebpackMissingModule");
  9. const DelegatedSourceDependency = require("./dependencies/DelegatedSourceDependency");
  10. const DelegatedExportsDependency = require("./dependencies/DelegatedExportsDependency");
  11. /** @typedef {import("./dependencies/ModuleDependency")} ModuleDependency */
  12. /** @typedef {import("./util/createHash").Hash} Hash */
  13. class DelegatedModule extends Module {
  14. constructor(sourceRequest, data, type, userRequest, originalRequest) {
  15. super("javascript/dynamic", null);
  16. // Info from Factory
  17. this.sourceRequest = sourceRequest;
  18. this.request = data.id;
  19. this.type = type;
  20. this.userRequest = userRequest;
  21. this.originalRequest = originalRequest;
  22. this.delegateData = data;
  23. // Build info
  24. this.delegatedSourceDependency = undefined;
  25. }
  26. libIdent(options) {
  27. return typeof this.originalRequest === "string"
  28. ? this.originalRequest
  29. : this.originalRequest.libIdent(options);
  30. }
  31. identifier() {
  32. return `delegated ${JSON.stringify(this.request)} from ${
  33. this.sourceRequest
  34. }`;
  35. }
  36. readableIdentifier() {
  37. return `delegated ${this.userRequest} from ${this.sourceRequest}`;
  38. }
  39. needRebuild() {
  40. return false;
  41. }
  42. build(options, compilation, resolver, fs, callback) {
  43. this.built = true;
  44. this.buildMeta = Object.assign({}, this.delegateData.buildMeta);
  45. this.buildInfo = {};
  46. this.delegatedSourceDependency = new DelegatedSourceDependency(
  47. this.sourceRequest
  48. );
  49. this.addDependency(this.delegatedSourceDependency);
  50. this.addDependency(
  51. new DelegatedExportsDependency(this, this.delegateData.exports || true)
  52. );
  53. callback();
  54. }
  55. source(depTemplates, runtime) {
  56. const dep = /** @type {DelegatedSourceDependency} */ (this.dependencies[0]);
  57. const sourceModule = dep.module;
  58. let str;
  59. if (!sourceModule) {
  60. str = WebpackMissingModule.moduleCode(this.sourceRequest);
  61. } else {
  62. str = `module.exports = (${runtime.moduleExports({
  63. module: sourceModule,
  64. request: dep.request
  65. })})`;
  66. switch (this.type) {
  67. case "require":
  68. str += `(${JSON.stringify(this.request)})`;
  69. break;
  70. case "object":
  71. str += `[${JSON.stringify(this.request)}]`;
  72. break;
  73. }
  74. str += ";";
  75. }
  76. if (this.useSourceMap) {
  77. return new OriginalSource(str, this.identifier());
  78. } else {
  79. return new RawSource(str);
  80. }
  81. }
  82. size() {
  83. return 42;
  84. }
  85. /**
  86. * @param {Hash} hash the hash used to track dependencies
  87. * @returns {void}
  88. */
  89. updateHash(hash) {
  90. hash.update(this.type);
  91. hash.update(JSON.stringify(this.request));
  92. super.updateHash(hash);
  93. }
  94. }
  95. module.exports = DelegatedModule;