提交学习笔记专用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
4.9 KiB

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