DllModule.js 5.1 KB

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