RemoteModule.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. * @param {string} request request string
  43. * @param {ExternalRequests} externalRequests list of external requests to containers
  44. * @param {string} internalRequest name of exposed module in container
  45. * @param {string} shareScope the used share scope name
  46. */
  47. constructor(request, externalRequests, internalRequest, shareScope) {
  48. super(WEBPACK_MODULE_TYPE_REMOTE);
  49. /** @type {string} */
  50. this.request = request;
  51. /** @type {ExternalRequests} */
  52. this.externalRequests = externalRequests;
  53. /** @type {string} */
  54. this.internalRequest = internalRequest;
  55. /** @type {string} */
  56. this.shareScope = shareScope;
  57. /** @type {string} */
  58. this._identifier = `remote (${shareScope}) ${this.externalRequests.join(
  59. " "
  60. )} ${this.internalRequest}`;
  61. }
  62. /**
  63. * @returns {string} a unique identifier of the module
  64. */
  65. identifier() {
  66. return this._identifier;
  67. }
  68. /**
  69. * @param {RequestShortener} requestShortener the request shortener
  70. * @returns {string} a user readable identifier of the module
  71. */
  72. readableIdentifier(requestShortener) {
  73. return `remote ${this.request}`;
  74. }
  75. /**
  76. * @param {LibIdentOptions} options options
  77. * @returns {LibIdent | null} an identifier for library inclusion
  78. */
  79. libIdent(options) {
  80. return `${this.layer ? `(${this.layer})/` : ""}webpack/container/remote/${
  81. this.request
  82. }`;
  83. }
  84. /**
  85. * @param {NeedBuildContext} context context info
  86. * @param {NeedBuildCallback} callback callback function, returns true, if the module needs a rebuild
  87. * @returns {void}
  88. */
  89. needBuild(context, callback) {
  90. callback(null, !this.buildInfo);
  91. }
  92. /**
  93. * @param {WebpackOptions} options webpack options
  94. * @param {Compilation} compilation the compilation
  95. * @param {ResolverWithOptions} resolver the resolver
  96. * @param {InputFileSystem} fs the file system
  97. * @param {BuildCallback} callback callback function
  98. * @returns {void}
  99. */
  100. build(options, compilation, resolver, fs, callback) {
  101. this.buildMeta = {};
  102. this.buildInfo = {
  103. strict: true
  104. };
  105. this.clearDependenciesAndBlocks();
  106. if (this.externalRequests.length === 1) {
  107. this.addDependency(
  108. new RemoteToExternalDependency(this.externalRequests[0])
  109. );
  110. } else {
  111. this.addDependency(new FallbackDependency(this.externalRequests));
  112. }
  113. callback();
  114. }
  115. /**
  116. * @param {string=} type the source type for which the size should be estimated
  117. * @returns {number} the estimated size of the module (must be non-zero)
  118. */
  119. size(type) {
  120. return 6;
  121. }
  122. /**
  123. * @returns {SourceTypes} types available (do not mutate)
  124. */
  125. getSourceTypes() {
  126. return REMOTE_AND_SHARE_INIT_TYPES;
  127. }
  128. /**
  129. * Basic source types are high-level categories like javascript, css, webassembly, etc.
  130. * We only have built-in knowledge about the javascript basic type here; other basic types may be
  131. * added or changed over time by generators and do not need to be handled or detected here.
  132. *
  133. * Some modules, e.g. RemoteModule, may return non-basic source types like "remote" and "share-init"
  134. * from getSourceTypes(), but their generated output is still JavaScript, i.e. their basic type is JS.
  135. * @returns {BasicSourceTypes} types available (do not mutate)
  136. */
  137. getSourceBasicTypes() {
  138. return JAVASCRIPT_TYPES;
  139. }
  140. /**
  141. * @param {ModuleGraph} moduleGraph the module graph
  142. * @param {boolean | undefined} strict the importing module is strict
  143. * @returns {ExportsType} export type
  144. * "namespace": Exports is already a namespace object. namespace = exports.
  145. * "dynamic": Check at runtime if __esModule is set. When set: namespace = { ...exports, default: exports }. When not set: namespace = { default: exports }.
  146. * "default-only": Provide a namespace object with only default export. namespace = { default: exports }
  147. * "default-with-named": Provide a namespace object with named and default export. namespace = { ...exports, default: exports }
  148. */
  149. getExportsType(moduleGraph, strict) {
  150. return "dynamic";
  151. }
  152. /**
  153. * @returns {NameForCondition | null} absolute path which should be used for condition matching (usually the resource path)
  154. */
  155. nameForCondition() {
  156. return this.request;
  157. }
  158. /**
  159. * @param {CodeGenerationContext} context context for code generation
  160. * @returns {CodeGenerationResult} result
  161. */
  162. codeGeneration({ moduleGraph, chunkGraph }) {
  163. const module = moduleGraph.getModule(this.dependencies[0]);
  164. const id = module && chunkGraph.getModuleId(module);
  165. /** @type {Sources} */
  166. const sources = new Map();
  167. sources.set("remote", new RawSource(""));
  168. /** @type {CodeGenerationResultData} */
  169. const data = new Map();
  170. data.set("share-init", [
  171. {
  172. shareScope: this.shareScope,
  173. initStage: 20,
  174. init: id === undefined ? "" : `initExternal(${JSON.stringify(id)});`
  175. }
  176. ]);
  177. return { sources, data, runtimeRequirements: RUNTIME_REQUIREMENTS };
  178. }
  179. /**
  180. * @param {ObjectSerializerContext} context context
  181. */
  182. serialize(context) {
  183. const { write } = context;
  184. write(this.request);
  185. write(this.externalRequests);
  186. write(this.internalRequest);
  187. write(this.shareScope);
  188. super.serialize(context);
  189. }
  190. /**
  191. * @param {ObjectDeserializerContext} context context
  192. * @returns {RemoteModule} deserialized module
  193. */
  194. static deserialize(context) {
  195. const { read } = context;
  196. const obj = new RemoteModule(read(), read(), read(), read());
  197. obj.deserialize(context);
  198. return obj;
  199. }
  200. }
  201. makeSerializable(RemoteModule, "webpack/lib/container/RemoteModule");
  202. module.exports = RemoteModule;