DelegatedModule.js 7.1 KB

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