RemoteModule.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. REMOTE_AND_SHARE_INIT_TYPES
  10. } = require("../ModuleSourceTypesConstants");
  11. const { WEBPACK_MODULE_TYPE_REMOTE } = require("../ModuleTypeConstants");
  12. const RuntimeGlobals = require("../RuntimeGlobals");
  13. const makeSerializable = require("../util/makeSerializable");
  14. const FallbackDependency = require("./FallbackDependency");
  15. const RemoteToExternalDependency = require("./RemoteToExternalDependency");
  16. /** @typedef {import("../../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
  17. /** @typedef {import("../ChunkGraph")} ChunkGraph */
  18. /** @typedef {import("../ChunkGroup")} ChunkGroup */
  19. /** @typedef {import("../Compilation")} Compilation */
  20. /** @typedef {import("../Module").BuildCallback} BuildCallback */
  21. /** @typedef {import("../Module").CodeGenerationContext} CodeGenerationContext */
  22. /** @typedef {import("../Module").CodeGenerationResult} CodeGenerationResult */
  23. /** @typedef {import("../Module").LibIdentOptions} LibIdentOptions */
  24. /** @typedef {import("../Module").NeedBuildCallback} NeedBuildCallback */
  25. /** @typedef {import("../Module").NeedBuildContext} NeedBuildContext */
  26. /** @typedef {import("../Module").SourceTypes} SourceTypes */
  27. /** @typedef {import("../RequestShortener")} RequestShortener */
  28. /** @typedef {import("../ResolverFactory").ResolverWithOptions} ResolverWithOptions */
  29. /** @typedef {import("../WebpackError")} WebpackError */
  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 RUNTIME_REQUIREMENTS = new Set([RuntimeGlobals.module]);
  35. class RemoteModule extends Module {
  36. /**
  37. * @param {string} request request string
  38. * @param {string[]} externalRequests list of external requests to containers
  39. * @param {string} internalRequest name of exposed module in container
  40. * @param {string} shareScope the used share scope name
  41. */
  42. constructor(request, externalRequests, internalRequest, shareScope) {
  43. super(WEBPACK_MODULE_TYPE_REMOTE);
  44. this.request = request;
  45. this.externalRequests = externalRequests;
  46. this.internalRequest = internalRequest;
  47. this.shareScope = shareScope;
  48. this._identifier = `remote (${shareScope}) ${this.externalRequests.join(
  49. " "
  50. )} ${this.internalRequest}`;
  51. }
  52. /**
  53. * @returns {string} a unique identifier of the module
  54. */
  55. identifier() {
  56. return this._identifier;
  57. }
  58. /**
  59. * @param {RequestShortener} requestShortener the request shortener
  60. * @returns {string} a user readable identifier of the module
  61. */
  62. readableIdentifier(requestShortener) {
  63. return `remote ${this.request}`;
  64. }
  65. /**
  66. * @param {LibIdentOptions} options options
  67. * @returns {string | null} an identifier for library inclusion
  68. */
  69. libIdent(options) {
  70. return `${this.layer ? `(${this.layer})/` : ""}webpack/container/remote/${
  71. this.request
  72. }`;
  73. }
  74. /**
  75. * @param {NeedBuildContext} context context info
  76. * @param {NeedBuildCallback} callback callback function, returns true, if the module needs a rebuild
  77. * @returns {void}
  78. */
  79. needBuild(context, callback) {
  80. callback(null, !this.buildInfo);
  81. }
  82. /**
  83. * @param {WebpackOptions} options webpack options
  84. * @param {Compilation} compilation the compilation
  85. * @param {ResolverWithOptions} resolver the resolver
  86. * @param {InputFileSystem} fs the file system
  87. * @param {BuildCallback} callback callback function
  88. * @returns {void}
  89. */
  90. build(options, compilation, resolver, fs, callback) {
  91. this.buildMeta = {};
  92. this.buildInfo = {
  93. strict: true
  94. };
  95. this.clearDependenciesAndBlocks();
  96. if (this.externalRequests.length === 1) {
  97. this.addDependency(
  98. new RemoteToExternalDependency(this.externalRequests[0])
  99. );
  100. } else {
  101. this.addDependency(new FallbackDependency(this.externalRequests));
  102. }
  103. callback();
  104. }
  105. /**
  106. * @param {string=} type the source type for which the size should be estimated
  107. * @returns {number} the estimated size of the module (must be non-zero)
  108. */
  109. size(type) {
  110. return 6;
  111. }
  112. /**
  113. * @returns {SourceTypes} types available (do not mutate)
  114. */
  115. getSourceTypes() {
  116. return REMOTE_AND_SHARE_INIT_TYPES;
  117. }
  118. /**
  119. * @returns {string | null} absolute path which should be used for condition matching (usually the resource path)
  120. */
  121. nameForCondition() {
  122. return this.request;
  123. }
  124. /**
  125. * @param {CodeGenerationContext} context context for code generation
  126. * @returns {CodeGenerationResult} result
  127. */
  128. codeGeneration({ runtimeTemplate, moduleGraph, chunkGraph }) {
  129. const module = moduleGraph.getModule(this.dependencies[0]);
  130. const id = module && chunkGraph.getModuleId(module);
  131. const sources = new Map();
  132. sources.set("remote", new RawSource(""));
  133. const data = new Map();
  134. data.set("share-init", [
  135. {
  136. shareScope: this.shareScope,
  137. initStage: 20,
  138. init: id === undefined ? "" : `initExternal(${JSON.stringify(id)});`
  139. }
  140. ]);
  141. return { sources, data, runtimeRequirements: RUNTIME_REQUIREMENTS };
  142. }
  143. /**
  144. * @param {ObjectSerializerContext} context context
  145. */
  146. serialize(context) {
  147. const { write } = context;
  148. write(this.request);
  149. write(this.externalRequests);
  150. write(this.internalRequest);
  151. write(this.shareScope);
  152. super.serialize(context);
  153. }
  154. /**
  155. * @param {ObjectDeserializerContext} context context
  156. * @returns {RemoteModule} deserialized module
  157. */
  158. static deserialize(context) {
  159. const { read } = context;
  160. const obj = new RemoteModule(read(), read(), read(), read());
  161. obj.deserialize(context);
  162. return obj;
  163. }
  164. }
  165. makeSerializable(RemoteModule, "webpack/lib/container/RemoteModule");
  166. module.exports = RemoteModule;