RawModule.js 5.6 KB

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