DllModule.js 5.0 KB

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