ProvideSharedModule.js 6.7 KB

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