DelegatedModule.js 7.9 KB

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