RemoteModule.js 5.7 KB

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