ProvideSharedModule.js 5.4 KB

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