ProvideSharedModule.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 { SHARED_INIT_TYPES } = require("../ModuleSourceTypeConstants");
  9. const { WEBPACK_MODULE_TYPE_PROVIDE } = require("../ModuleTypeConstants");
  10. const RuntimeGlobals = require("../RuntimeGlobals");
  11. const makeSerializable = require("../util/makeSerializable");
  12. const ProvideForSharedDependency = require("./ProvideForSharedDependency");
  13. /** @typedef {import("../config/defaults").WebpackOptionsNormalizedWithDefaults} WebpackOptions */
  14. /** @typedef {import("../Compilation")} Compilation */
  15. /** @typedef {import("../Module").BuildCallback} BuildCallback */
  16. /** @typedef {import("../Module").CodeGenerationContext} CodeGenerationContext */
  17. /** @typedef {import("../Module").CodeGenerationResult} CodeGenerationResult */
  18. /** @typedef {import("../Module").LibIdentOptions} LibIdentOptions */
  19. /** @typedef {import("../Module").LibIdent} LibIdent */
  20. /** @typedef {import("../Module").NeedBuildCallback} NeedBuildCallback */
  21. /** @typedef {import("../Module").NeedBuildContext} NeedBuildContext */
  22. /** @typedef {import("../Module").Sources} Sources */
  23. /** @typedef {import("../Module").SourceTypes} SourceTypes */
  24. /** @typedef {import("../Module").CodeGenerationResultData} CodeGenerationResultData */
  25. /** @typedef {import("../RequestShortener")} RequestShortener */
  26. /** @typedef {import("../ResolverFactory").ResolverWithOptions} ResolverWithOptions */
  27. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  28. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  29. /** @typedef {import("../util/fs").InputFileSystem} InputFileSystem */
  30. class ProvideSharedModule extends Module {
  31. /**
  32. * Creates an instance of ProvideSharedModule.
  33. * @param {string} shareScope shared scope name
  34. * @param {string} name shared key
  35. * @param {string | false} version version
  36. * @param {string} request request to the provided module
  37. * @param {boolean} eager include the module in sync way
  38. */
  39. constructor(shareScope, name, version, request, eager) {
  40. super(WEBPACK_MODULE_TYPE_PROVIDE);
  41. this._shareScope = shareScope;
  42. this._name = name;
  43. this._version = version;
  44. this._request = request;
  45. this._eager = eager;
  46. }
  47. /**
  48. * Returns the unique identifier used to reference this module.
  49. * @returns {string} a unique identifier of the module
  50. */
  51. identifier() {
  52. return `provide module (${this._shareScope}) ${this._name}@${this._version} = ${this._request}`;
  53. }
  54. /**
  55. * Returns a human-readable identifier for this module.
  56. * @param {RequestShortener} requestShortener the request shortener
  57. * @returns {string} a user readable identifier of the module
  58. */
  59. readableIdentifier(requestShortener) {
  60. return `provide shared module (${this._shareScope}) ${this._name}@${
  61. this._version
  62. } = ${requestShortener.shorten(this._request)}`;
  63. }
  64. /**
  65. * Gets the library identifier.
  66. * @param {LibIdentOptions} options options
  67. * @returns {LibIdent | null} an identifier for library inclusion
  68. */
  69. libIdent(options) {
  70. return `${this.layer ? `(${this.layer})/` : ""}webpack/sharing/provide/${
  71. this._shareScope
  72. }/${this._name}`;
  73. }
  74. /**
  75. * Checks whether the module needs to be rebuilt for the current build state.
  76. * @param {NeedBuildContext} context context info
  77. * @param {NeedBuildCallback} callback callback function, returns true, if the module needs a rebuild
  78. * @returns {void}
  79. */
  80. needBuild(context, callback) {
  81. callback(null, !this.buildInfo);
  82. }
  83. /**
  84. * Builds the module using the provided compilation context.
  85. * @param {WebpackOptions} options webpack options
  86. * @param {Compilation} compilation the compilation
  87. * @param {ResolverWithOptions} resolver the resolver
  88. * @param {InputFileSystem} fs the file system
  89. * @param {BuildCallback} callback callback function
  90. * @returns {void}
  91. */
  92. build(options, compilation, resolver, fs, callback) {
  93. this.buildMeta = {};
  94. this.buildInfo = {
  95. strict: true
  96. };
  97. this.clearDependenciesAndBlocks();
  98. const dep = new ProvideForSharedDependency(this._request);
  99. if (this._eager) {
  100. this.addDependency(dep);
  101. } else {
  102. const block = new AsyncDependenciesBlock({});
  103. block.addDependency(dep);
  104. this.addBlock(block);
  105. }
  106. callback();
  107. }
  108. /**
  109. * Returns the estimated size for the requested source type.
  110. * @param {string=} type the source type for which the size should be estimated
  111. * @returns {number} the estimated size of the module (must be non-zero)
  112. */
  113. size(type) {
  114. return 42;
  115. }
  116. /**
  117. * Returns the source types this module can generate.
  118. * @returns {SourceTypes} types available (do not mutate)
  119. */
  120. getSourceTypes() {
  121. return SHARED_INIT_TYPES;
  122. }
  123. /**
  124. * Generates code and runtime requirements for this module.
  125. * @param {CodeGenerationContext} context context for code generation
  126. * @returns {CodeGenerationResult} result
  127. */
  128. codeGeneration({ runtimeTemplate, chunkGraph }) {
  129. const runtimeRequirements = new Set([RuntimeGlobals.initializeSharing]);
  130. const code = `register(${JSON.stringify(this._name)}, ${JSON.stringify(
  131. this._version || "0"
  132. )}, ${
  133. this._eager
  134. ? runtimeTemplate.syncModuleFactory({
  135. dependency: this.dependencies[0],
  136. chunkGraph,
  137. request: this._request,
  138. runtimeRequirements
  139. })
  140. : runtimeTemplate.asyncModuleFactory({
  141. block: this.blocks[0],
  142. chunkGraph,
  143. request: this._request,
  144. runtimeRequirements
  145. })
  146. }${this._eager ? ", 1" : ""});`;
  147. /** @type {Sources} */
  148. const sources = new Map();
  149. /** @type {CodeGenerationResultData} */
  150. const data = new Map();
  151. data.set("share-init", [
  152. {
  153. shareScope: this._shareScope,
  154. initStage: 10,
  155. init: code
  156. }
  157. ]);
  158. return { sources, data, runtimeRequirements };
  159. }
  160. /**
  161. * Serializes this instance into the provided serializer context.
  162. * @param {ObjectSerializerContext} context context
  163. */
  164. serialize(context) {
  165. const { write } = context;
  166. write(this._shareScope);
  167. write(this._name);
  168. write(this._version);
  169. write(this._request);
  170. write(this._eager);
  171. super.serialize(context);
  172. }
  173. /**
  174. * Restores this instance from the provided deserializer context.
  175. * @param {ObjectDeserializerContext} context context
  176. * @returns {ProvideSharedModule} deserialize fallback dependency
  177. */
  178. static deserialize(context) {
  179. const { read } = context;
  180. const obj = new ProvideSharedModule(read(), read(), read(), read(), read());
  181. obj.deserialize(context);
  182. return obj;
  183. }
  184. }
  185. makeSerializable(
  186. ProvideSharedModule,
  187. "webpack/lib/sharing/ProvideSharedModule"
  188. );
  189. module.exports = ProvideSharedModule;