ProvideSharedModule.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 AsyncDependenciesBlock = require("../AsyncDependenciesBlock");
  7. const Module = require("../Module");
  8. const { WEBPACK_MODULE_TYPE_PROVIDE } = require("../ModuleTypeConstants");
  9. const RuntimeGlobals = require("../RuntimeGlobals");
  10. const makeSerializable = require("../util/makeSerializable");
  11. const ProvideForSharedDependency = require("./ProvideForSharedDependency");
  12. /** @typedef {import("../../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
  13. /** @typedef {import("../Chunk")} Chunk */
  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(["share-init"]);
  29. class ProvideSharedModule extends Module {
  30. /**
  31. * @param {string} shareScope shared scope name
  32. * @param {string} name shared key
  33. * @param {string | false} version version
  34. * @param {string} request request to the provided module
  35. * @param {boolean} eager include the module in sync way
  36. */
  37. constructor(shareScope, name, version, request, eager) {
  38. super(WEBPACK_MODULE_TYPE_PROVIDE);
  39. this._shareScope = shareScope;
  40. this._name = name;
  41. this._version = version;
  42. this._request = request;
  43. this._eager = eager;
  44. }
  45. /**
  46. * @returns {string} a unique identifier of the module
  47. */
  48. identifier() {
  49. return `provide module (${this._shareScope}) ${this._name}@${this._version} = ${this._request}`;
  50. }
  51. /**
  52. * @param {RequestShortener} requestShortener the request shortener
  53. * @returns {string} a user readable identifier of the module
  54. */
  55. readableIdentifier(requestShortener) {
  56. return `provide shared module (${this._shareScope}) ${this._name}@${
  57. this._version
  58. } = ${requestShortener.shorten(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/sharing/provide/${
  66. this._shareScope
  67. }/${this._name}`;
  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. const dep = new ProvideForSharedDependency(this._request);
  92. if (this._eager) {
  93. this.addDependency(dep);
  94. } else {
  95. const block = new AsyncDependenciesBlock({});
  96. block.addDependency(dep);
  97. this.addBlock(block);
  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 42;
  107. }
  108. /**
  109. * @returns {Set<string>} types available (do not mutate)
  110. */
  111. getSourceTypes() {
  112. return TYPES;
  113. }
  114. /**
  115. * @param {CodeGenerationContext} context context for code generation
  116. * @returns {CodeGenerationResult} result
  117. */
  118. codeGeneration({ runtimeTemplate, moduleGraph, chunkGraph }) {
  119. const runtimeRequirements = new Set([RuntimeGlobals.initializeSharing]);
  120. const code = `register(${JSON.stringify(this._name)}, ${JSON.stringify(
  121. this._version || "0"
  122. )}, ${
  123. this._eager
  124. ? runtimeTemplate.syncModuleFactory({
  125. dependency: this.dependencies[0],
  126. chunkGraph,
  127. request: this._request,
  128. runtimeRequirements
  129. })
  130. : runtimeTemplate.asyncModuleFactory({
  131. block: this.blocks[0],
  132. chunkGraph,
  133. request: this._request,
  134. runtimeRequirements
  135. })
  136. }${this._eager ? ", 1" : ""});`;
  137. const sources = new Map();
  138. const data = new Map();
  139. data.set("share-init", [
  140. {
  141. shareScope: this._shareScope,
  142. initStage: 10,
  143. init: code
  144. }
  145. ]);
  146. return { sources, data, runtimeRequirements };
  147. }
  148. /**
  149. * @param {ObjectSerializerContext} context context
  150. */
  151. serialize(context) {
  152. const { write } = context;
  153. write(this._shareScope);
  154. write(this._name);
  155. write(this._version);
  156. write(this._request);
  157. write(this._eager);
  158. super.serialize(context);
  159. }
  160. /**
  161. * @param {ObjectDeserializerContext} context context
  162. * @returns {ProvideSharedModule} deserialize fallback dependency
  163. */
  164. static deserialize(context) {
  165. const { read } = context;
  166. const obj = new ProvideSharedModule(read(), read(), read(), read(), read());
  167. obj.deserialize(context);
  168. return obj;
  169. }
  170. }
  171. makeSerializable(
  172. ProvideSharedModule,
  173. "webpack/lib/sharing/ProvideSharedModule"
  174. );
  175. module.exports = ProvideSharedModule;