RemoteModule.js 5.8 KB

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