RawModule.js 4.6 KB

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