DllModule.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { RawSource } = require("webpack-sources");
  7. const Module = require("../Module");
  8. const {
  9. JAVASCRIPT_TYPE,
  10. JAVASCRIPT_TYPES
  11. } = require("../ModuleSourceTypeConstants");
  12. const { JAVASCRIPT_MODULE_TYPE_DYNAMIC } = require("../ModuleTypeConstants");
  13. const RuntimeGlobals = require("../RuntimeGlobals");
  14. const makeSerializable = require("../util/makeSerializable");
  15. /**
  16. * @import {
  17. * WebpackOptionsNormalizedWithDefaults as WebpackOptions
  18. * } from "../config/defaults"
  19. */
  20. /** @import Compilation from "../Compilation" */
  21. /** @import Dependency, { UpdateHashContext } from "../Dependency" */
  22. /**
  23. * @import {
  24. * BuildCallback,
  25. * CodeGenerationContext,
  26. * CodeGenerationResult,
  27. * NeedBuildCallback,
  28. * NeedBuildContext,
  29. * Sources,
  30. * SourceTypes
  31. * } from "../Module"
  32. */
  33. /** @import RequestShortener from "../RequestShortener" */
  34. /** @import { ResolverWithOptions } from "../ResolverFactory" */
  35. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext<[string]>} ObjectDeserializerContext */
  36. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext<[string]>} ObjectSerializerContext */
  37. /** @import Hash from "../util/Hash" */
  38. /** @import { InputFileSystem } from "../util/fs" */
  39. const RUNTIME_REQUIREMENTS = new Set([
  40. RuntimeGlobals.require,
  41. RuntimeGlobals.module
  42. ]);
  43. class DllModule extends Module {
  44. /**
  45. * Creates an instance of DllModule.
  46. * @param {string} context context path
  47. * @param {Dependency[]} dependencies dependencies
  48. * @param {string} name name
  49. */
  50. constructor(context, dependencies, name) {
  51. super(JAVASCRIPT_MODULE_TYPE_DYNAMIC, context);
  52. // Info from Factory
  53. /** @type {Dependency[]} */
  54. this.dependencies = dependencies;
  55. /** @type {string} */
  56. this.name = name;
  57. }
  58. /**
  59. * Returns the source types this module can generate.
  60. * @returns {SourceTypes} types available (do not mutate)
  61. */
  62. getSourceTypes() {
  63. return JAVASCRIPT_TYPES;
  64. }
  65. /**
  66. * Returns the unique identifier used to reference this module.
  67. * @returns {string} a unique identifier of the module
  68. */
  69. identifier() {
  70. return `dll ${this.name}`;
  71. }
  72. /**
  73. * Returns a human-readable identifier for this module.
  74. * @param {RequestShortener} requestShortener the request shortener
  75. * @returns {string} a user readable identifier of the module
  76. */
  77. readableIdentifier(requestShortener) {
  78. return `dll ${this.name}`;
  79. }
  80. /**
  81. * Builds the module using the provided compilation context.
  82. * @param {WebpackOptions} options webpack options
  83. * @param {Compilation} compilation the compilation
  84. * @param {ResolverWithOptions} resolver the resolver
  85. * @param {InputFileSystem} fs the file system
  86. * @param {BuildCallback} callback callback function
  87. * @returns {void}
  88. */
  89. build(options, compilation, resolver, fs, callback) {
  90. this.buildMeta = {};
  91. this.buildInfo = {};
  92. return callback();
  93. }
  94. /**
  95. * Generates code and runtime requirements for this module.
  96. * @param {CodeGenerationContext} context context for code generation
  97. * @returns {CodeGenerationResult} result
  98. */
  99. codeGeneration(context) {
  100. /** @type {Sources} */
  101. const sources = new Map();
  102. sources.set(
  103. JAVASCRIPT_TYPE,
  104. new RawSource(`module.exports = ${RuntimeGlobals.require};`)
  105. );
  106. return {
  107. sources,
  108. runtimeRequirements: RUNTIME_REQUIREMENTS
  109. };
  110. }
  111. /**
  112. * Checks whether the module needs to be rebuilt for the current build state.
  113. * @param {NeedBuildContext} context context info
  114. * @param {NeedBuildCallback} callback callback function, returns true, if the module needs a rebuild
  115. * @returns {void}
  116. */
  117. needBuild(context, callback) {
  118. return callback(null, !this.buildMeta);
  119. }
  120. /**
  121. * Returns the estimated size for the requested source type.
  122. * @param {string=} type the source type for which the size should be estimated
  123. * @returns {number} the estimated size of the module (must be non-zero)
  124. */
  125. size(type) {
  126. return 12;
  127. }
  128. /**
  129. * Updates the hash with the data contributed by this instance.
  130. * @param {Hash} hash the hash used to track dependencies
  131. * @param {UpdateHashContext} context context
  132. * @returns {void}
  133. */
  134. updateHash(hash, context) {
  135. hash.update(`dll module${this.name || ""}`);
  136. super.updateHash(hash, context);
  137. }
  138. /**
  139. * Serializes this instance into the provided serializer context.
  140. * @param {ObjectSerializerContext} context context
  141. */
  142. serialize(context) {
  143. context.write(this.name);
  144. super.serialize(context);
  145. }
  146. /**
  147. * Restores this instance from the provided deserializer context.
  148. * @param {ObjectDeserializerContext} context context
  149. */
  150. deserialize(context) {
  151. this.name = context.read();
  152. super.deserialize(context);
  153. }
  154. /**
  155. * Assuming this module is in the cache. Update the (cached) module with
  156. * the fresh module from the factory. Usually updates internal references
  157. * and properties.
  158. * @param {Module} module fresh module
  159. * @returns {void}
  160. */
  161. updateCacheModule(module) {
  162. super.updateCacheModule(module);
  163. this.dependencies = module.dependencies;
  164. }
  165. /**
  166. * Assuming this module is in the cache. Remove internal references to allow freeing some memory.
  167. */
  168. cleanupForCache() {
  169. super.cleanupForCache();
  170. this.dependencies = /** @type {EXPECTED_ANY} */ (undefined);
  171. }
  172. }
  173. makeSerializable(DllModule, "webpack/lib/dll/DllModule");
  174. module.exports = DllModule;