ProvideSharedModule.js 5.9 KB

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