提交学习笔记专用
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.

175 lines
5.5 KiB

  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { OriginalSource, 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 makeSerializable = require("./util/makeSerializable");
  11. /** @typedef {import("./config/defaults").WebpackOptionsNormalizedWithDefaults} WebpackOptions */
  12. /** @typedef {import("./Compilation")} Compilation */
  13. /** @typedef {import("./Dependency").UpdateHashContext} UpdateHashContext */
  14. /** @typedef {import("./Generator").SourceTypes} SourceTypes */
  15. /** @typedef {import("./Module").BuildCallback} BuildCallback */
  16. /** @typedef {import("./Module").CodeGenerationContext} CodeGenerationContext */
  17. /** @typedef {import("./Module").CodeGenerationResult} CodeGenerationResult */
  18. /** @typedef {import("./Module").NeedBuildCallback} NeedBuildCallback */
  19. /** @typedef {import("./Module").NeedBuildContext} NeedBuildContext */
  20. /** @typedef {import("./Module").ReadOnlyRuntimeRequirements} ReadOnlyRuntimeRequirements */
  21. /** @typedef {import("./ModuleGraph")} ModuleGraph */
  22. /** @typedef {import("./ModuleGraphConnection").ConnectionState} ConnectionState */
  23. /** @typedef {import("./RequestShortener")} RequestShortener */
  24. /** @typedef {import("./ResolverFactory").ResolverWithOptions} ResolverWithOptions */
  25. /** @typedef {import("./serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  26. /** @typedef {import("./serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  27. /** @typedef {import("./util/Hash")} Hash */
  28. /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
  29. class RawModule extends Module {
  30. /**
  31. * @param {string} source source code
  32. * @param {string} identifier unique identifier
  33. * @param {string=} readableIdentifier readable identifier
  34. * @param {ReadOnlyRuntimeRequirements=} runtimeRequirements runtime requirements needed for the source code
  35. */
  36. constructor(source, identifier, readableIdentifier, runtimeRequirements) {
  37. super(JAVASCRIPT_MODULE_TYPE_DYNAMIC, null);
  38. this.sourceStr = source;
  39. this.identifierStr = identifier || this.sourceStr;
  40. this.readableIdentifierStr = readableIdentifier || this.identifierStr;
  41. this.runtimeRequirements = runtimeRequirements || null;
  42. }
  43. /**
  44. * @returns {SourceTypes} types available (do not mutate)
  45. */
  46. getSourceTypes() {
  47. return JS_TYPES;
  48. }
  49. /**
  50. * @returns {string} a unique identifier of the module
  51. */
  52. identifier() {
  53. return this.identifierStr;
  54. }
  55. /**
  56. * @param {string=} type the source type for which the size should be estimated
  57. * @returns {number} the estimated size of the module (must be non-zero)
  58. */
  59. size(type) {
  60. return Math.max(1, this.sourceStr.length);
  61. }
  62. /**
  63. * @param {RequestShortener} requestShortener the request shortener
  64. * @returns {string} a user readable identifier of the module
  65. */
  66. readableIdentifier(requestShortener) {
  67. return /** @type {string} */ (
  68. requestShortener.shorten(this.readableIdentifierStr)
  69. );
  70. }
  71. /**
  72. * @param {NeedBuildContext} context context info
  73. * @param {NeedBuildCallback} callback callback function, returns true, if the module needs a rebuild
  74. * @returns {void}
  75. */
  76. needBuild(context, callback) {
  77. return callback(null, !this.buildMeta);
  78. }
  79. /**
  80. * @param {WebpackOptions} options webpack options
  81. * @param {Compilation} compilation the compilation
  82. * @param {ResolverWithOptions} resolver the resolver
  83. * @param {InputFileSystem} fs the file system
  84. * @param {BuildCallback} callback callback function
  85. * @returns {void}
  86. */
  87. build(options, compilation, resolver, fs, callback) {
  88. this.buildMeta = {};
  89. this.buildInfo = {
  90. cacheable: true
  91. };
  92. callback();
  93. }
  94. /**
  95. * @param {ModuleGraph} moduleGraph the module graph
  96. * @returns {ConnectionState} how this module should be connected to referencing modules when consumed for side-effects only
  97. */
  98. getSideEffectsConnectionState(moduleGraph) {
  99. if (this.factoryMeta !== undefined) {
  100. if (this.factoryMeta.sideEffectFree) return false;
  101. if (this.factoryMeta.sideEffectFree === false) return true;
  102. }
  103. return true;
  104. }
  105. /**
  106. * @param {CodeGenerationContext} context context for code generation
  107. * @returns {CodeGenerationResult} result
  108. */
  109. codeGeneration(context) {
  110. const sources = new Map();
  111. if (this.useSourceMap || this.useSimpleSourceMap) {
  112. sources.set(
  113. "javascript",
  114. new OriginalSource(this.sourceStr, this.identifier())
  115. );
  116. } else {
  117. sources.set("javascript", new RawSource(this.sourceStr));
  118. }
  119. return { sources, runtimeRequirements: this.runtimeRequirements };
  120. }
  121. /**
  122. * @param {Hash} hash the hash used to track dependencies
  123. * @param {UpdateHashContext} context context
  124. * @returns {void}
  125. */
  126. updateHash(hash, context) {
  127. hash.update(this.sourceStr);
  128. super.updateHash(hash, context);
  129. }
  130. /**
  131. * @param {ObjectSerializerContext} context context
  132. */
  133. serialize(context) {
  134. const { write } = context;
  135. write(this.sourceStr);
  136. write(this.identifierStr);
  137. write(this.readableIdentifierStr);
  138. write(this.runtimeRequirements);
  139. super.serialize(context);
  140. }
  141. /**
  142. * @param {ObjectDeserializerContext} context context
  143. */
  144. deserialize(context) {
  145. const { read } = context;
  146. this.sourceStr = read();
  147. this.identifierStr = read();
  148. this.readableIdentifierStr = read();
  149. this.runtimeRequirements = read();
  150. super.deserialize(context);
  151. }
  152. }
  153. makeSerializable(RawModule, "webpack/lib/RawModule");
  154. module.exports = RawModule;