RawModule.js 5.8 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 { JS_TYPES } = require("./ModuleSourceTypesConstants");
  9. const { JAVASCRIPT_MODULE_TYPE_DYNAMIC } = require("./ModuleTypeConstants");
  10. const makeSerializable = require("./util/makeSerializable");
  11. /** @typedef {import("webpack-sources").Source} Source */
  12. /** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
  13. /** @typedef {import("./ChunkGraph")} ChunkGraph */
  14. /** @typedef {import("./Compilation")} Compilation */
  15. /** @typedef {import("./Dependency").UpdateHashContext} UpdateHashContext */
  16. /** @typedef {import("./DependencyTemplates")} DependencyTemplates */
  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("./ModuleGraph")} ModuleGraph */
  25. /** @typedef {import("./ModuleGraphConnection").ConnectionState} ConnectionState */
  26. /** @typedef {import("./RequestShortener")} RequestShortener */
  27. /** @typedef {import("./ResolverFactory").ResolverWithOptions} ResolverWithOptions */
  28. /** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
  29. /** @typedef {import("./WebpackError")} WebpackError */
  30. /** @typedef {import("./serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  31. /** @typedef {import("./serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  32. /** @typedef {import("./util/Hash")} Hash */
  33. /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
  34. class RawModule extends Module {
  35. /**
  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 {SourceTypes} types available (do not mutate)
  50. */
  51. getSourceTypes() {
  52. return JS_TYPES;
  53. }
  54. /**
  55. * @returns {string} a unique identifier of the module
  56. */
  57. identifier() {
  58. return this.identifierStr;
  59. }
  60. /**
  61. * @param {string=} type the source type for which the size should be estimated
  62. * @returns {number} the estimated size of the module (must be non-zero)
  63. */
  64. size(type) {
  65. return Math.max(1, this.sourceStr.length);
  66. }
  67. /**
  68. * @param {RequestShortener} requestShortener the request shortener
  69. * @returns {string} a user readable identifier of the module
  70. */
  71. readableIdentifier(requestShortener) {
  72. return /** @type {string} */ (
  73. requestShortener.shorten(this.readableIdentifierStr)
  74. );
  75. }
  76. /**
  77. * @param {NeedBuildContext} context context info
  78. * @param {NeedBuildCallback} callback callback function, returns true, if the module needs a rebuild
  79. * @returns {void}
  80. */
  81. needBuild(context, callback) {
  82. return callback(null, !this.buildMeta);
  83. }
  84. /**
  85. * @param {WebpackOptions} options webpack options
  86. * @param {Compilation} compilation the compilation
  87. * @param {ResolverWithOptions} resolver the resolver
  88. * @param {InputFileSystem} fs the file system
  89. * @param {BuildCallback} callback callback function
  90. * @returns {void}
  91. */
  92. build(options, compilation, resolver, fs, callback) {
  93. this.buildMeta = {};
  94. this.buildInfo = {
  95. cacheable: true
  96. };
  97. callback();
  98. }
  99. /**
  100. * @param {ModuleGraph} moduleGraph the module graph
  101. * @returns {ConnectionState} how this module should be connected to referencing modules when consumed for side-effects only
  102. */
  103. getSideEffectsConnectionState(moduleGraph) {
  104. if (this.factoryMeta !== undefined) {
  105. if (this.factoryMeta.sideEffectFree) return false;
  106. if (this.factoryMeta.sideEffectFree === false) return true;
  107. }
  108. return true;
  109. }
  110. /**
  111. * @param {CodeGenerationContext} context context for code generation
  112. * @returns {CodeGenerationResult} result
  113. */
  114. codeGeneration(context) {
  115. const sources = new Map();
  116. if (this.useSourceMap || this.useSimpleSourceMap) {
  117. sources.set(
  118. "javascript",
  119. new OriginalSource(this.sourceStr, this.identifier())
  120. );
  121. } else {
  122. sources.set("javascript", 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;