RawModule.js 6.2 KB

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