DllModule.js 5.0 KB

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