RemoteModule.js 5.2 KB

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