DllModule.js 4.5 KB

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