DelegatedModule.js 8.0 KB

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