RawModule.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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("./RequestShortener")} RequestShortener */
  25. /** @typedef {import("./ResolverFactory").ResolverWithOptions} ResolverWithOptions */
  26. /** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
  27. /** @typedef {import("./WebpackError")} WebpackError */
  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 JS_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 {CodeGenerationContext} context context for code generation
  99. * @returns {CodeGenerationResult} result
  100. */
  101. codeGeneration(context) {
  102. const sources = new Map();
  103. if (this.useSourceMap || this.useSimpleSourceMap) {
  104. sources.set(
  105. "javascript",
  106. new OriginalSource(this.sourceStr, this.identifier())
  107. );
  108. } else {
  109. sources.set("javascript", new RawSource(this.sourceStr));
  110. }
  111. return { sources, runtimeRequirements: this.runtimeRequirements };
  112. }
  113. /**
  114. * @param {Hash} hash the hash used to track dependencies
  115. * @param {UpdateHashContext} context context
  116. * @returns {void}
  117. */
  118. updateHash(hash, context) {
  119. hash.update(this.sourceStr);
  120. super.updateHash(hash, context);
  121. }
  122. /**
  123. * @param {ObjectSerializerContext} context context
  124. */
  125. serialize(context) {
  126. const { write } = context;
  127. write(this.sourceStr);
  128. write(this.identifierStr);
  129. write(this.readableIdentifierStr);
  130. write(this.runtimeRequirements);
  131. super.serialize(context);
  132. }
  133. /**
  134. * @param {ObjectDeserializerContext} context context
  135. */
  136. deserialize(context) {
  137. const { read } = context;
  138. this.sourceStr = read();
  139. this.identifierStr = read();
  140. this.readableIdentifierStr = read();
  141. this.runtimeRequirements = read();
  142. super.deserialize(context);
  143. }
  144. }
  145. makeSerializable(RawModule, "webpack/lib/RawModule");
  146. module.exports = RawModule;