RawModule.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. * @param {string} source source code
  36. * @param {string} identifier unique identifier
  37. * @param {string=} readableIdentifier readable identifier
  38. * @param {ReadOnlyRuntimeRequirements=} runtimeRequirements runtime requirements needed for the source code
  39. */
  40. constructor(source, identifier, readableIdentifier, runtimeRequirements) {
  41. super(JAVASCRIPT_MODULE_TYPE_DYNAMIC, null);
  42. this.sourceStr = source;
  43. this.identifierStr = identifier || this.sourceStr;
  44. this.readableIdentifierStr = readableIdentifier || this.identifierStr;
  45. this.runtimeRequirements = runtimeRequirements || null;
  46. }
  47. /**
  48. * @returns {SourceTypes} types available (do not mutate)
  49. */
  50. getSourceTypes() {
  51. return JAVASCRIPT_TYPES;
  52. }
  53. /**
  54. * @returns {string} a unique identifier of the module
  55. */
  56. identifier() {
  57. return this.identifierStr;
  58. }
  59. /**
  60. * @param {string=} type the source type for which the size should be estimated
  61. * @returns {number} the estimated size of the module (must be non-zero)
  62. */
  63. size(type) {
  64. return Math.max(1, this.sourceStr.length);
  65. }
  66. /**
  67. * @param {RequestShortener} requestShortener the request shortener
  68. * @returns {string} a user readable identifier of the module
  69. */
  70. readableIdentifier(requestShortener) {
  71. return /** @type {string} */ (
  72. requestShortener.shorten(this.readableIdentifierStr)
  73. );
  74. }
  75. /**
  76. * @param {NeedBuildContext} context context info
  77. * @param {NeedBuildCallback} callback callback function, returns true, if the module needs a rebuild
  78. * @returns {void}
  79. */
  80. needBuild(context, callback) {
  81. return callback(null, !this.buildMeta);
  82. }
  83. /**
  84. * @param {WebpackOptions} options webpack options
  85. * @param {Compilation} compilation the compilation
  86. * @param {ResolverWithOptions} resolver the resolver
  87. * @param {InputFileSystem} fs the file system
  88. * @param {BuildCallback} callback callback function
  89. * @returns {void}
  90. */
  91. build(options, compilation, resolver, fs, callback) {
  92. this.buildMeta = {};
  93. this.buildInfo = {
  94. cacheable: true
  95. };
  96. callback();
  97. }
  98. /**
  99. * @param {ModuleGraph} moduleGraph the module graph
  100. * @returns {ConnectionState} how this module should be connected to referencing modules when consumed for side-effects only
  101. */
  102. getSideEffectsConnectionState(moduleGraph) {
  103. if (this.factoryMeta !== undefined) {
  104. if (this.factoryMeta.sideEffectFree) return false;
  105. if (this.factoryMeta.sideEffectFree === false) return true;
  106. }
  107. return true;
  108. }
  109. /**
  110. * @param {CodeGenerationContext} context context for code generation
  111. * @returns {CodeGenerationResult} result
  112. */
  113. codeGeneration(context) {
  114. /** @type {Sources} */
  115. const sources = new Map();
  116. if (this.useSourceMap || this.useSimpleSourceMap) {
  117. sources.set(
  118. JAVASCRIPT_TYPE,
  119. new OriginalSource(this.sourceStr, this.identifier())
  120. );
  121. } else {
  122. sources.set(JAVASCRIPT_TYPE, new RawSource(this.sourceStr));
  123. }
  124. return { sources, runtimeRequirements: this.runtimeRequirements };
  125. }
  126. /**
  127. * @param {Hash} hash the hash used to track dependencies
  128. * @param {UpdateHashContext} context context
  129. * @returns {void}
  130. */
  131. updateHash(hash, context) {
  132. hash.update(this.sourceStr);
  133. super.updateHash(hash, context);
  134. }
  135. /**
  136. * @param {ObjectSerializerContext} context context
  137. */
  138. serialize(context) {
  139. const { write } = context;
  140. write(this.sourceStr);
  141. write(this.identifierStr);
  142. write(this.readableIdentifierStr);
  143. write(this.runtimeRequirements);
  144. super.serialize(context);
  145. }
  146. /**
  147. * @param {ObjectDeserializerContext} context context
  148. */
  149. deserialize(context) {
  150. const { read } = context;
  151. this.sourceStr = read();
  152. this.identifierStr = read();
  153. this.readableIdentifierStr = read();
  154. this.runtimeRequirements = read();
  155. super.deserialize(context);
  156. }
  157. }
  158. makeSerializable(RawModule, "webpack/lib/RawModule");
  159. module.exports = RawModule;