DllModule.js 4.4 KB

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