RemoteModule.js 5.7 KB

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