DelegatedModule.js 7.9 KB

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