ProvideSharedModule.js 6.0 KB

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