RawModule.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 { JAVASCRIPT_MODULE_TYPE_DYNAMIC } = require("./ModuleTypeConstants");
  9. const makeSerializable = require("./util/makeSerializable");
  10. /** @typedef {import("webpack-sources").Source} Source */
  11. /** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
  12. /** @typedef {import("./ChunkGraph")} ChunkGraph */
  13. /** @typedef {import("./Compilation")} Compilation */
  14. /** @typedef {import("./Dependency").UpdateHashContext} UpdateHashContext */
  15. /** @typedef {import("./DependencyTemplates")} DependencyTemplates */
  16. /** @typedef {import("./Module").CodeGenerationContext} CodeGenerationContext */
  17. /** @typedef {import("./Module").CodeGenerationResult} CodeGenerationResult */
  18. /** @typedef {import("./Module").NeedBuildContext} NeedBuildContext */
  19. /** @typedef {import("./Module").ReadOnlyRuntimeRequirements} ReadOnlyRuntimeRequirements */
  20. /** @typedef {import("./Module").SourceTypes} SourceTypes */
  21. /** @typedef {import("./RequestShortener")} RequestShortener */
  22. /** @typedef {import("./ResolverFactory").ResolverWithOptions} ResolverWithOptions */
  23. /** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
  24. /** @typedef {import("./WebpackError")} WebpackError */
  25. /** @typedef {import("./serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  26. /** @typedef {import("./serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  27. /** @typedef {import("./util/Hash")} Hash */
  28. /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
  29. const TYPES = new Set(["javascript"]);
  30. class RawModule extends Module {
  31. /**
  32. * @param {string} source source code
  33. * @param {string} identifier unique identifier
  34. * @param {string=} readableIdentifier readable identifier
  35. * @param {ReadOnlyRuntimeRequirements=} runtimeRequirements runtime requirements needed for the source code
  36. */
  37. constructor(source, identifier, readableIdentifier, runtimeRequirements) {
  38. super(JAVASCRIPT_MODULE_TYPE_DYNAMIC, null);
  39. this.sourceStr = source;
  40. this.identifierStr = identifier || this.sourceStr;
  41. this.readableIdentifierStr = readableIdentifier || this.identifierStr;
  42. this.runtimeRequirements = runtimeRequirements || null;
  43. }
  44. /**
  45. * @returns {SourceTypes} types available (do not mutate)
  46. */
  47. getSourceTypes() {
  48. return TYPES;
  49. }
  50. /**
  51. * @returns {string} a unique identifier of the module
  52. */
  53. identifier() {
  54. return this.identifierStr;
  55. }
  56. /**
  57. * @param {string=} type the source type for which the size should be estimated
  58. * @returns {number} the estimated size of the module (must be non-zero)
  59. */
  60. size(type) {
  61. return Math.max(1, this.sourceStr.length);
  62. }
  63. /**
  64. * @param {RequestShortener} requestShortener the request shortener
  65. * @returns {string} a user readable identifier of the module
  66. */
  67. readableIdentifier(requestShortener) {
  68. return /** @type {string} */ (
  69. requestShortener.shorten(this.readableIdentifierStr)
  70. );
  71. }
  72. /**
  73. * @param {NeedBuildContext} context context info
  74. * @param {function((WebpackError | null)=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
  75. * @returns {void}
  76. */
  77. needBuild(context, callback) {
  78. return callback(null, !this.buildMeta);
  79. }
  80. /**
  81. * @param {WebpackOptions} options webpack options
  82. * @param {Compilation} compilation the compilation
  83. * @param {ResolverWithOptions} resolver the resolver
  84. * @param {InputFileSystem} fs the file system
  85. * @param {function(WebpackError=): void} callback callback function
  86. * @returns {void}
  87. */
  88. build(options, compilation, resolver, fs, callback) {
  89. this.buildMeta = {};
  90. this.buildInfo = {
  91. cacheable: true
  92. };
  93. callback();
  94. }
  95. /**
  96. * @param {CodeGenerationContext} context context for code generation
  97. * @returns {CodeGenerationResult} result
  98. */
  99. codeGeneration(context) {
  100. const sources = new Map();
  101. if (this.useSourceMap || this.useSimpleSourceMap) {
  102. sources.set(
  103. "javascript",
  104. new OriginalSource(this.sourceStr, this.identifier())
  105. );
  106. } else {
  107. sources.set("javascript", new RawSource(this.sourceStr));
  108. }
  109. return { sources, runtimeRequirements: this.runtimeRequirements };
  110. }
  111. /**
  112. * @param {Hash} hash the hash used to track dependencies
  113. * @param {UpdateHashContext} context context
  114. * @returns {void}
  115. */
  116. updateHash(hash, context) {
  117. hash.update(this.sourceStr);
  118. super.updateHash(hash, context);
  119. }
  120. /**
  121. * @param {ObjectSerializerContext} context context
  122. */
  123. serialize(context) {
  124. const { write } = context;
  125. write(this.sourceStr);
  126. write(this.identifierStr);
  127. write(this.readableIdentifierStr);
  128. write(this.runtimeRequirements);
  129. super.serialize(context);
  130. }
  131. /**
  132. * @param {ObjectDeserializerContext} context context
  133. */
  134. deserialize(context) {
  135. const { read } = context;
  136. this.sourceStr = read();
  137. this.identifierStr = read();
  138. this.readableIdentifierStr = read();
  139. this.runtimeRequirements = read();
  140. super.deserialize(context);
  141. }
  142. }
  143. makeSerializable(RawModule, "webpack/lib/RawModule");
  144. module.exports = RawModule;