RawModule.js 4.5 KB

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