RawModule.js 5.0 KB

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