RemoteModule.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra and Zackary Jackson @ScriptedAlchemy
  4. */
  5. "use strict";
  6. const { RawSource } = require("webpack-sources");
  7. const Module = require("../Module");
  8. const {
  9. JAVASCRIPT_TYPES,
  10. REMOTE_AND_SHARE_INIT_TYPES
  11. } = require("../ModuleSourceTypeConstants");
  12. const { WEBPACK_MODULE_TYPE_REMOTE } = require("../ModuleTypeConstants");
  13. const RuntimeGlobals = require("../RuntimeGlobals");
  14. const makeSerializable = require("../util/makeSerializable");
  15. const FallbackDependency = require("./FallbackDependency");
  16. const RemoteToExternalDependency = require("./RemoteToExternalDependency");
  17. /** @typedef {import("../config/defaults").WebpackOptionsNormalizedWithDefaults} WebpackOptions */
  18. /** @typedef {import("../Compilation")} Compilation */
  19. /** @typedef {import("../Module").BuildCallback} BuildCallback */
  20. /** @typedef {import("../Module").CodeGenerationContext} CodeGenerationContext */
  21. /** @typedef {import("../Module").CodeGenerationResultData} CodeGenerationResultData */
  22. /** @typedef {import("../Module").CodeGenerationResult} CodeGenerationResult */
  23. /** @typedef {import("../Module").LibIdentOptions} LibIdentOptions */
  24. /** @typedef {import("../Module").LibIdent} LibIdent */
  25. /** @typedef {import("../Module").NameForCondition} NameForCondition */
  26. /** @typedef {import("../Module").NeedBuildCallback} NeedBuildCallback */
  27. /** @typedef {import("../Module").NeedBuildContext} NeedBuildContext */
  28. /** @typedef {import("../Module").Sources} Sources */
  29. /** @typedef {import("../Module").SourceTypes} SourceTypes */
  30. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  31. /** @typedef {import("../Module").ExportsType} ExportsType */
  32. /** @typedef {import("../RequestShortener")} RequestShortener */
  33. /** @typedef {import("../ResolverFactory").ResolverWithOptions} ResolverWithOptions */
  34. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  35. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  36. /** @typedef {import("../util/fs").InputFileSystem} InputFileSystem */
  37. /** @typedef {import("../Module").BasicSourceTypes} BasicSourceTypes */
  38. const RUNTIME_REQUIREMENTS = new Set([RuntimeGlobals.module]);
  39. /** @typedef {string[]} ExternalRequests */
  40. class RemoteModule extends Module {
  41. /**
  42. * Creates an instance of RemoteModule.
  43. * @param {string} request request string
  44. * @param {ExternalRequests} externalRequests list of external requests to containers
  45. * @param {string} internalRequest name of exposed module in container
  46. * @param {string} shareScope the used share scope name
  47. */
  48. constructor(request, externalRequests, internalRequest, shareScope) {
  49. super(WEBPACK_MODULE_TYPE_REMOTE);
  50. /** @type {string} */
  51. this.request = request;
  52. /** @type {ExternalRequests} */
  53. this.externalRequests = externalRequests;
  54. /** @type {string} */
  55. this.internalRequest = internalRequest;
  56. /** @type {string} */
  57. this.shareScope = shareScope;
  58. /** @type {string} */
  59. this._identifier = `remote (${shareScope}) ${this.externalRequests.join(
  60. " "
  61. )} ${this.internalRequest}`;
  62. }
  63. /**
  64. * Returns the unique identifier used to reference this module.
  65. * @returns {string} a unique identifier of the module
  66. */
  67. identifier() {
  68. return this._identifier;
  69. }
  70. /**
  71. * Returns a human-readable identifier for this module.
  72. * @param {RequestShortener} requestShortener the request shortener
  73. * @returns {string} a user readable identifier of the module
  74. */
  75. readableIdentifier(requestShortener) {
  76. return `remote ${this.request}`;
  77. }
  78. /**
  79. * Gets the library identifier.
  80. * @param {LibIdentOptions} options options
  81. * @returns {LibIdent | null} an identifier for library inclusion
  82. */
  83. libIdent(options) {
  84. return `${this.layer ? `(${this.layer})/` : ""}webpack/container/remote/${
  85. this.request
  86. }`;
  87. }
  88. /**
  89. * Checks whether the module needs to be rebuilt for the current build state.
  90. * @param {NeedBuildContext} context context info
  91. * @param {NeedBuildCallback} callback callback function, returns true, if the module needs a rebuild
  92. * @returns {void}
  93. */
  94. needBuild(context, callback) {
  95. callback(null, !this.buildInfo);
  96. }
  97. /**
  98. * Builds the module using the provided compilation context.
  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 {BuildCallback} callback callback function
  104. * @returns {void}
  105. */
  106. build(options, compilation, resolver, fs, callback) {
  107. this.buildMeta = {};
  108. this.buildInfo = {
  109. strict: true
  110. };
  111. this.clearDependenciesAndBlocks();
  112. if (this.externalRequests.length === 1) {
  113. this.addDependency(
  114. new RemoteToExternalDependency(this.externalRequests[0])
  115. );
  116. } else {
  117. this.addDependency(new FallbackDependency(this.externalRequests));
  118. }
  119. callback();
  120. }
  121. /**
  122. * Returns the estimated size for the requested source type.
  123. * @param {string=} type the source type for which the size should be estimated
  124. * @returns {number} the estimated size of the module (must be non-zero)
  125. */
  126. size(type) {
  127. return 6;
  128. }
  129. /**
  130. * Returns the source types this module can generate.
  131. * @returns {SourceTypes} types available (do not mutate)
  132. */
  133. getSourceTypes() {
  134. return REMOTE_AND_SHARE_INIT_TYPES;
  135. }
  136. /**
  137. * Basic source types are high-level categories like javascript, css, webassembly, etc.
  138. * We only have built-in knowledge about the javascript basic type here; other basic types may be
  139. * added or changed over time by generators and do not need to be handled or detected here.
  140. *
  141. * Some modules, e.g. RemoteModule, may return non-basic source types like "remote" and "share-init"
  142. * from getSourceTypes(), but their generated output is still JavaScript, i.e. their basic type is JS.
  143. * @returns {BasicSourceTypes} types available (do not mutate)
  144. */
  145. getSourceBasicTypes() {
  146. return JAVASCRIPT_TYPES;
  147. }
  148. /**
  149. * Returns export type.
  150. * @param {ModuleGraph} moduleGraph the module graph
  151. * @param {boolean | undefined} strict the importing module is strict
  152. * @returns {ExportsType} export type
  153. * "namespace": Exports is already a namespace object. namespace = exports.
  154. * "dynamic": Check at runtime if __esModule is set. When set: namespace = { ...exports, default: exports }. When not set: namespace = { default: exports }.
  155. * "default-only": Provide a namespace object with only default export. namespace = { default: exports }
  156. * "default-with-named": Provide a namespace object with named and default export. namespace = { ...exports, default: exports }
  157. */
  158. getExportsType(moduleGraph, strict) {
  159. return "dynamic";
  160. }
  161. /**
  162. * Returns the path used when matching this module against rule conditions.
  163. * @returns {NameForCondition | null} absolute path which should be used for condition matching (usually the resource path)
  164. */
  165. nameForCondition() {
  166. return this.request;
  167. }
  168. /**
  169. * Generates code and runtime requirements for this module.
  170. * @param {CodeGenerationContext} context context for code generation
  171. * @returns {CodeGenerationResult} result
  172. */
  173. codeGeneration({ moduleGraph, chunkGraph }) {
  174. const module = moduleGraph.getModule(this.dependencies[0]);
  175. const id = module && chunkGraph.getModuleId(module);
  176. /** @type {Sources} */
  177. const sources = new Map();
  178. sources.set("remote", new RawSource(""));
  179. /** @type {CodeGenerationResultData} */
  180. const data = new Map();
  181. data.set("share-init", [
  182. {
  183. shareScope: this.shareScope,
  184. initStage: 20,
  185. init: id === undefined ? "" : `initExternal(${JSON.stringify(id)});`
  186. }
  187. ]);
  188. return { sources, data, runtimeRequirements: RUNTIME_REQUIREMENTS };
  189. }
  190. /**
  191. * Serializes this instance into the provided serializer context.
  192. * @param {ObjectSerializerContext} context context
  193. */
  194. serialize(context) {
  195. const { write } = context;
  196. write(this.request);
  197. write(this.externalRequests);
  198. write(this.internalRequest);
  199. write(this.shareScope);
  200. super.serialize(context);
  201. }
  202. /**
  203. * Restores this instance from the provided deserializer context.
  204. * @param {ObjectDeserializerContext} context context
  205. * @returns {RemoteModule} deserialized module
  206. */
  207. static deserialize(context) {
  208. const { read } = context;
  209. const obj = new RemoteModule(read(), read(), read(), read());
  210. obj.deserialize(context);
  211. return obj;
  212. }
  213. }
  214. makeSerializable(RemoteModule, "webpack/lib/container/RemoteModule");
  215. module.exports = RemoteModule;