RawModule.js 5.1 KB

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