DllModule.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. /** @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 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 {function(WebpackError=): void} 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. const sources = new Map();
  87. sources.set(
  88. "javascript",
  89. new RawSource(`module.exports = ${RuntimeGlobals.require};`)
  90. );
  91. return {
  92. sources,
  93. runtimeRequirements: RUNTIME_REQUIREMENTS
  94. };
  95. }
  96. /**
  97. * @param {NeedBuildContext} context context info
  98. * @param {function((WebpackError | null)=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
  99. * @returns {void}
  100. */
  101. needBuild(context, callback) {
  102. return callback(null, !this.buildMeta);
  103. }
  104. /**
  105. * @param {string=} type the source type for which the size should be estimated
  106. * @returns {number} the estimated size of the module (must be non-zero)
  107. */
  108. size(type) {
  109. return 12;
  110. }
  111. /**
  112. * @param {Hash} hash the hash used to track dependencies
  113. * @param {UpdateHashContext} context context
  114. * @returns {void}
  115. */
  116. updateHash(hash, context) {
  117. hash.update(`dll module${this.name || ""}`);
  118. super.updateHash(hash, context);
  119. }
  120. /**
  121. * @param {ObjectSerializerContext} context context
  122. */
  123. serialize(context) {
  124. context.write(this.name);
  125. super.serialize(context);
  126. }
  127. /**
  128. * @param {ObjectDeserializerContext} context context
  129. */
  130. deserialize(context) {
  131. this.name = context.read();
  132. super.deserialize(context);
  133. }
  134. /**
  135. * Assuming this module is in the cache. Update the (cached) module with
  136. * the fresh module from the factory. Usually updates internal references
  137. * and properties.
  138. * @param {Module} module fresh module
  139. * @returns {void}
  140. */
  141. updateCacheModule(module) {
  142. super.updateCacheModule(module);
  143. this.dependencies = module.dependencies;
  144. }
  145. /**
  146. * Assuming this module is in the cache. Remove internal references to allow freeing some memory.
  147. */
  148. cleanupForCache() {
  149. super.cleanupForCache();
  150. this.dependencies = undefined;
  151. }
  152. }
  153. makeSerializable(DllModule, "webpack/lib/DllModule");
  154. module.exports = DllModule;