RawModule.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 {
  9. JAVASCRIPT_TYPE,
  10. JAVASCRIPT_TYPES
  11. } = require("./ModuleSourceTypeConstants");
  12. const { JAVASCRIPT_MODULE_TYPE_DYNAMIC } = require("./ModuleTypeConstants");
  13. const makeSerializable = require("./util/makeSerializable");
  14. /** @typedef {import("./config/defaults").WebpackOptionsNormalizedWithDefaults} WebpackOptions */
  15. /** @typedef {import("./Compilation")} Compilation */
  16. /** @typedef {import("./Dependency").UpdateHashContext} UpdateHashContext */
  17. /** @typedef {import("./Generator").SourceTypes} SourceTypes */
  18. /** @typedef {import("./Module").BuildCallback} BuildCallback */
  19. /** @typedef {import("./Module").CodeGenerationContext} CodeGenerationContext */
  20. /** @typedef {import("./Module").CodeGenerationResult} CodeGenerationResult */
  21. /** @typedef {import("./Module").NeedBuildCallback} NeedBuildCallback */
  22. /** @typedef {import("./Module").NeedBuildContext} NeedBuildContext */
  23. /** @typedef {import("./Module").ReadOnlyRuntimeRequirements} ReadOnlyRuntimeRequirements */
  24. /** @typedef {import("./Module").Sources} Sources */
  25. /** @typedef {import("./ModuleGraph")} ModuleGraph */
  26. /** @typedef {import("./ModuleGraphConnection").ConnectionState} ConnectionState */
  27. /** @typedef {import("./RequestShortener")} RequestShortener */
  28. /** @typedef {import("./ResolverFactory").ResolverWithOptions} ResolverWithOptions */
  29. /** @typedef {import("./serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  30. /** @typedef {import("./serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  31. /** @typedef {import("./util/Hash")} Hash */
  32. /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
  33. class RawModule extends Module {
  34. /**
  35. * Creates an instance of RawModule.
  36. * @param {string} source source code
  37. * @param {string} identifier unique identifier
  38. * @param {string=} readableIdentifier readable identifier
  39. * @param {ReadOnlyRuntimeRequirements=} runtimeRequirements runtime requirements needed for the source code
  40. */
  41. constructor(source, identifier, readableIdentifier, runtimeRequirements) {
  42. super(JAVASCRIPT_MODULE_TYPE_DYNAMIC, null);
  43. this.sourceStr = source;
  44. this.identifierStr = identifier || this.sourceStr;
  45. this.readableIdentifierStr = readableIdentifier || this.identifierStr;
  46. this.runtimeRequirements = runtimeRequirements || null;
  47. }
  48. /**
  49. * Returns the source types this module can generate.
  50. * @returns {SourceTypes} types available (do not mutate)
  51. */
  52. getSourceTypes() {
  53. return JAVASCRIPT_TYPES;
  54. }
  55. /**
  56. * Returns the unique identifier used to reference this module.
  57. * @returns {string} a unique identifier of the module
  58. */
  59. identifier() {
  60. return this.identifierStr;
  61. }
  62. /**
  63. * Returns the estimated size for the requested source type.
  64. * @param {string=} type the source type for which the size should be estimated
  65. * @returns {number} the estimated size of the module (must be non-zero)
  66. */
  67. size(type) {
  68. return Math.max(1, this.sourceStr.length);
  69. }
  70. /**
  71. * Returns a human-readable identifier for this module.
  72. * @param {RequestShortener} requestShortener the request shortener
  73. * @returns {string} a user readable identifier of the module
  74. */
  75. readableIdentifier(requestShortener) {
  76. return /** @type {string} */ (
  77. requestShortener.shorten(this.readableIdentifierStr)
  78. );
  79. }
  80. /**
  81. * Checks whether the module needs to be rebuilt for the current build state.
  82. * @param {NeedBuildContext} context context info
  83. * @param {NeedBuildCallback} callback callback function, returns true, if the module needs a rebuild
  84. * @returns {void}
  85. */
  86. needBuild(context, callback) {
  87. return callback(null, !this.buildMeta);
  88. }
  89. /**
  90. * Builds the module using the provided compilation context.
  91. * @param {WebpackOptions} options webpack options
  92. * @param {Compilation} compilation the compilation
  93. * @param {ResolverWithOptions} resolver the resolver
  94. * @param {InputFileSystem} fs the file system
  95. * @param {BuildCallback} callback callback function
  96. * @returns {void}
  97. */
  98. build(options, compilation, resolver, fs, callback) {
  99. this.buildMeta = {};
  100. this.buildInfo = {
  101. cacheable: true
  102. };
  103. callback();
  104. }
  105. /**
  106. * Gets side effects connection state.
  107. * @param {ModuleGraph} moduleGraph the module graph
  108. * @returns {ConnectionState} how this module should be connected to referencing modules when consumed for side-effects only
  109. */
  110. getSideEffectsConnectionState(moduleGraph) {
  111. if (this.factoryMeta !== undefined) {
  112. if (this.factoryMeta.sideEffectFree) return false;
  113. if (this.factoryMeta.sideEffectFree === false) return true;
  114. }
  115. return true;
  116. }
  117. /**
  118. * Generates code and runtime requirements for this module.
  119. * @param {CodeGenerationContext} context context for code generation
  120. * @returns {CodeGenerationResult} result
  121. */
  122. codeGeneration(context) {
  123. /** @type {Sources} */
  124. const sources = new Map();
  125. if (this.useSourceMap || this.useSimpleSourceMap) {
  126. sources.set(
  127. JAVASCRIPT_TYPE,
  128. new OriginalSource(this.sourceStr, this.identifier())
  129. );
  130. } else {
  131. sources.set(JAVASCRIPT_TYPE, new RawSource(this.sourceStr));
  132. }
  133. return { sources, runtimeRequirements: this.runtimeRequirements };
  134. }
  135. /**
  136. * Updates the hash with the data contributed by this instance.
  137. * @param {Hash} hash the hash used to track dependencies
  138. * @param {UpdateHashContext} context context
  139. * @returns {void}
  140. */
  141. updateHash(hash, context) {
  142. hash.update(this.sourceStr);
  143. super.updateHash(hash, context);
  144. }
  145. /**
  146. * Serializes this instance into the provided serializer context.
  147. * @param {ObjectSerializerContext} context context
  148. */
  149. serialize(context) {
  150. const { write } = context;
  151. write(this.sourceStr);
  152. write(this.identifierStr);
  153. write(this.readableIdentifierStr);
  154. write(this.runtimeRequirements);
  155. super.serialize(context);
  156. }
  157. /**
  158. * Restores this instance from the provided deserializer context.
  159. * @param {ObjectDeserializerContext} context context
  160. */
  161. deserialize(context) {
  162. const { read } = context;
  163. this.sourceStr = read();
  164. this.identifierStr = read();
  165. this.readableIdentifierStr = read();
  166. this.runtimeRequirements = read();
  167. super.deserialize(context);
  168. }
  169. }
  170. makeSerializable(RawModule, "webpack/lib/RawModule");
  171. module.exports = RawModule;