DllModule.js 5.7 KB

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