DelegatedModule.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 { JAVASCRIPT_MODULE_TYPE_DYNAMIC } = require("./ModuleTypeConstants");
  9. const RuntimeGlobals = require("./RuntimeGlobals");
  10. const DelegatedSourceDependency = require("./dependencies/DelegatedSourceDependency");
  11. const StaticExportsDependency = require("./dependencies/StaticExportsDependency");
  12. const makeSerializable = require("./util/makeSerializable");
  13. /** @typedef {import("webpack-sources").Source} Source */
  14. /** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
  15. /** @typedef {import("./ChunkGraph")} ChunkGraph */
  16. /** @typedef {import("./Compilation")} Compilation */
  17. /** @typedef {import("./Dependency").UpdateHashContext} UpdateHashContext */
  18. /** @typedef {import("./DependencyTemplates")} DependencyTemplates */
  19. /** @typedef {import("./LibManifestPlugin").ManifestModuleData} ManifestModuleData */
  20. /** @typedef {import("./Module").CodeGenerationContext} CodeGenerationContext */
  21. /** @typedef {import("./Module").CodeGenerationResult} CodeGenerationResult */
  22. /** @typedef {import("./Module").LibIdentOptions} LibIdentOptions */
  23. /** @typedef {import("./Module").NeedBuildContext} NeedBuildContext */
  24. /** @typedef {import("./Module").SourceContext} SourceContext */
  25. /** @typedef {import("./RequestShortener")} RequestShortener */
  26. /** @typedef {import("./ResolverFactory").ResolverWithOptions} ResolverWithOptions */
  27. /** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
  28. /** @typedef {import("./WebpackError")} WebpackError */
  29. /** @typedef {import("./dependencies/ModuleDependency")} ModuleDependency */
  30. /** @typedef {import("./util/Hash")} Hash */
  31. /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
  32. const TYPES = new Set(["javascript"]);
  33. const RUNTIME_REQUIREMENTS = new Set([
  34. RuntimeGlobals.module,
  35. RuntimeGlobals.require
  36. ]);
  37. class DelegatedModule extends Module {
  38. constructor(sourceRequest, data, type, userRequest, originalRequest) {
  39. super(JAVASCRIPT_MODULE_TYPE_DYNAMIC, null);
  40. // Info from Factory
  41. this.sourceRequest = sourceRequest;
  42. this.request = data.id;
  43. this.delegationType = type;
  44. this.userRequest = userRequest;
  45. this.originalRequest = originalRequest;
  46. /** @type {ManifestModuleData} */
  47. this.delegateData = data;
  48. // Build info
  49. this.delegatedSourceDependency = undefined;
  50. }
  51. /**
  52. * @returns {Set<string>} types available (do not mutate)
  53. */
  54. getSourceTypes() {
  55. return TYPES;
  56. }
  57. /**
  58. * @param {LibIdentOptions} options options
  59. * @returns {string | null} an identifier for library inclusion
  60. */
  61. libIdent(options) {
  62. return typeof this.originalRequest === "string"
  63. ? this.originalRequest
  64. : this.originalRequest.libIdent(options);
  65. }
  66. /**
  67. * @returns {string} a unique identifier of the module
  68. */
  69. identifier() {
  70. return `delegated ${JSON.stringify(this.request)} from ${
  71. this.sourceRequest
  72. }`;
  73. }
  74. /**
  75. * @param {RequestShortener} requestShortener the request shortener
  76. * @returns {string} a user readable identifier of the module
  77. */
  78. readableIdentifier(requestShortener) {
  79. return `delegated ${this.userRequest} from ${this.sourceRequest}`;
  80. }
  81. /**
  82. * @param {NeedBuildContext} context context info
  83. * @param {function((WebpackError | null)=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
  84. * @returns {void}
  85. */
  86. needBuild(context, callback) {
  87. return callback(null, !this.buildMeta);
  88. }
  89. /**
  90. * @param {WebpackOptions} options webpack options
  91. * @param {Compilation} compilation the compilation
  92. * @param {ResolverWithOptions} resolver the resolver
  93. * @param {InputFileSystem} fs the file system
  94. * @param {function(WebpackError=): void} callback callback function
  95. * @returns {void}
  96. */
  97. build(options, compilation, resolver, fs, callback) {
  98. this.buildMeta = { ...this.delegateData.buildMeta };
  99. this.buildInfo = {};
  100. this.dependencies.length = 0;
  101. this.delegatedSourceDependency = new DelegatedSourceDependency(
  102. this.sourceRequest
  103. );
  104. this.addDependency(this.delegatedSourceDependency);
  105. this.addDependency(
  106. new StaticExportsDependency(this.delegateData.exports || true, false)
  107. );
  108. callback();
  109. }
  110. /**
  111. * @param {CodeGenerationContext} context context for code generation
  112. * @returns {CodeGenerationResult} result
  113. */
  114. codeGeneration({ runtimeTemplate, moduleGraph, chunkGraph }) {
  115. const dep = /** @type {DelegatedSourceDependency} */ (this.dependencies[0]);
  116. const sourceModule = moduleGraph.getModule(dep);
  117. let str;
  118. if (!sourceModule) {
  119. str = runtimeTemplate.throwMissingModuleErrorBlock({
  120. request: this.sourceRequest
  121. });
  122. } else {
  123. str = `module.exports = (${runtimeTemplate.moduleExports({
  124. module: sourceModule,
  125. chunkGraph,
  126. request: dep.request,
  127. runtimeRequirements: new Set()
  128. })})`;
  129. switch (this.delegationType) {
  130. case "require":
  131. str += `(${JSON.stringify(this.request)})`;
  132. break;
  133. case "object":
  134. str += `[${JSON.stringify(this.request)}]`;
  135. break;
  136. }
  137. str += ";";
  138. }
  139. const sources = new Map();
  140. if (this.useSourceMap || this.useSimpleSourceMap) {
  141. sources.set("javascript", new OriginalSource(str, this.identifier()));
  142. } else {
  143. sources.set("javascript", new RawSource(str));
  144. }
  145. return {
  146. sources,
  147. runtimeRequirements: RUNTIME_REQUIREMENTS
  148. };
  149. }
  150. /**
  151. * @param {string=} type the source type for which the size should be estimated
  152. * @returns {number} the estimated size of the module (must be non-zero)
  153. */
  154. size(type) {
  155. return 42;
  156. }
  157. /**
  158. * @param {Hash} hash the hash used to track dependencies
  159. * @param {UpdateHashContext} context context
  160. * @returns {void}
  161. */
  162. updateHash(hash, context) {
  163. hash.update(this.delegationType);
  164. hash.update(JSON.stringify(this.request));
  165. super.updateHash(hash, context);
  166. }
  167. serialize(context) {
  168. const { write } = context;
  169. // constructor
  170. write(this.sourceRequest);
  171. write(this.delegateData);
  172. write(this.delegationType);
  173. write(this.userRequest);
  174. write(this.originalRequest);
  175. super.serialize(context);
  176. }
  177. static deserialize(context) {
  178. const { read } = context;
  179. const obj = new DelegatedModule(
  180. read(), // sourceRequest
  181. read(), // delegateData
  182. read(), // delegationType
  183. read(), // userRequest
  184. read() // originalRequest
  185. );
  186. obj.deserialize(context);
  187. return obj;
  188. }
  189. /**
  190. * Assuming this module is in the cache. Update the (cached) module with
  191. * the fresh module from the factory. Usually updates internal references
  192. * and properties.
  193. * @param {Module} module fresh module
  194. * @returns {void}
  195. */
  196. updateCacheModule(module) {
  197. super.updateCacheModule(module);
  198. const m = /** @type {DelegatedModule} */ (module);
  199. this.delegationType = m.delegationType;
  200. this.userRequest = m.userRequest;
  201. this.originalRequest = m.originalRequest;
  202. this.delegateData = m.delegateData;
  203. }
  204. /**
  205. * Assuming this module is in the cache. Remove internal references to allow freeing some memory.
  206. */
  207. cleanupForCache() {
  208. super.cleanupForCache();
  209. this.delegateData = undefined;
  210. }
  211. }
  212. makeSerializable(DelegatedModule, "webpack/lib/DelegatedModule");
  213. module.exports = DelegatedModule;