RawDataUrlModule.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { RawSource } = require("webpack-sources");
  7. const Module = require("../Module");
  8. const {
  9. JAVASCRIPT_TYPE,
  10. JAVASCRIPT_TYPES
  11. } = require("../ModuleSourceTypeConstants");
  12. const { ASSET_MODULE_TYPE_RAW_DATA_URL } = require("../ModuleTypeConstants");
  13. const RuntimeGlobals = require("../RuntimeGlobals");
  14. const makeSerializable = require("../util/makeSerializable");
  15. /** @typedef {import("../config/defaults").WebpackOptionsNormalizedWithDefaults} WebpackOptions */
  16. /** @typedef {import("../Compilation")} Compilation */
  17. /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
  18. /** @typedef {import("../Module").BuildCallback} BuildCallback */
  19. /** @typedef {import("../Module").RuntimeRequirements} RuntimeRequirements */
  20. /** @typedef {import("../Module").CodeGenerationContext} CodeGenerationContext */
  21. /** @typedef {import("../Module").CodeGenerationResult} CodeGenerationResult */
  22. /** @typedef {import("../Module").CodeGenerationResultData} CodeGenerationResultData */
  23. /** @typedef {import("../Module").NeedBuildCallback} NeedBuildCallback */
  24. /** @typedef {import("../Module").NeedBuildContext} NeedBuildContext */
  25. /** @typedef {import("../Module").Sources} Sources */
  26. /** @typedef {import("../Module").SourceTypes} SourceTypes */
  27. /** @typedef {import("../RequestShortener")} RequestShortener */
  28. /** @typedef {import("../ResolverFactory").ResolverWithOptions} ResolverWithOptions */
  29. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  30. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  31. /** @typedef {import("../util/Hash")} Hash */
  32. /** @typedef {import("../util/fs").InputFileSystem} InputFileSystem */
  33. class RawDataUrlModule extends Module {
  34. /**
  35. * Creates an instance of RawDataUrlModule.
  36. * @param {string} url raw url
  37. * @param {string} identifier unique identifier
  38. * @param {string=} readableIdentifier readable identifier
  39. */
  40. constructor(url, identifier, readableIdentifier) {
  41. super(ASSET_MODULE_TYPE_RAW_DATA_URL, null);
  42. /** @type {string} */
  43. this.url = url;
  44. /** @type {Buffer | undefined} */
  45. this.urlBuffer = url ? Buffer.from(url) : undefined;
  46. /** @type {string} */
  47. this.identifierStr = identifier;
  48. /** @type {string} */
  49. this.readableIdentifierStr = readableIdentifier || this.identifierStr;
  50. }
  51. /**
  52. * Returns the source types this module can generate.
  53. * @returns {SourceTypes} types available (do not mutate)
  54. */
  55. getSourceTypes() {
  56. return JAVASCRIPT_TYPES;
  57. }
  58. /**
  59. * Returns the unique identifier used to reference this module.
  60. * @returns {string} a unique identifier of the module
  61. */
  62. identifier() {
  63. return this.identifierStr;
  64. }
  65. /**
  66. * Returns the estimated size for the requested source type.
  67. * @param {string=} type the source type for which the size should be estimated
  68. * @returns {number} the estimated size of the module (must be non-zero)
  69. */
  70. size(type) {
  71. if (this.url === undefined) {
  72. this.url = /** @type {Buffer} */ (this.urlBuffer).toString();
  73. }
  74. return Math.max(1, this.url.length);
  75. }
  76. /**
  77. * Returns a human-readable identifier for this module.
  78. * @param {RequestShortener} requestShortener the request shortener
  79. * @returns {string} a user readable identifier of the module
  80. */
  81. readableIdentifier(requestShortener) {
  82. return /** @type {string} */ (
  83. requestShortener.shorten(this.readableIdentifierStr)
  84. );
  85. }
  86. /**
  87. * Checks whether the module needs to be rebuilt for the current build state.
  88. * @param {NeedBuildContext} context context info
  89. * @param {NeedBuildCallback} callback callback function, returns true, if the module needs a rebuild
  90. * @returns {void}
  91. */
  92. needBuild(context, callback) {
  93. return callback(null, !this.buildMeta);
  94. }
  95. /**
  96. * Builds the module using the provided compilation context.
  97. * @param {WebpackOptions} options webpack options
  98. * @param {Compilation} compilation the compilation
  99. * @param {ResolverWithOptions} resolver the resolver
  100. * @param {InputFileSystem} fs the file system
  101. * @param {BuildCallback} callback callback function
  102. * @returns {void}
  103. */
  104. build(options, compilation, resolver, fs, callback) {
  105. this.buildMeta = {};
  106. this.buildInfo = {
  107. cacheable: true
  108. };
  109. callback();
  110. }
  111. /**
  112. * Generates code and runtime requirements for this module.
  113. * @param {CodeGenerationContext} context context for code generation
  114. * @returns {CodeGenerationResult} result
  115. */
  116. codeGeneration(context) {
  117. if (this.url === undefined) {
  118. this.url = /** @type {Buffer} */ (this.urlBuffer).toString();
  119. }
  120. /** @type {Sources} */
  121. const sources = new Map();
  122. sources.set(
  123. JAVASCRIPT_TYPE,
  124. new RawSource(`module.exports = ${JSON.stringify(this.url)};`)
  125. );
  126. /** @type {CodeGenerationResultData} */
  127. const data = new Map();
  128. data.set("url", {
  129. javascript: this.url
  130. });
  131. /** @type {RuntimeRequirements} */
  132. const runtimeRequirements = new Set();
  133. runtimeRequirements.add(RuntimeGlobals.module);
  134. return { sources, runtimeRequirements, data };
  135. }
  136. /**
  137. * Updates the hash with the data contributed by this instance.
  138. * @param {Hash} hash the hash used to track dependencies
  139. * @param {UpdateHashContext} context context
  140. * @returns {void}
  141. */
  142. updateHash(hash, context) {
  143. hash.update(/** @type {Buffer} */ (this.urlBuffer));
  144. super.updateHash(hash, context);
  145. }
  146. /**
  147. * Serializes this instance into the provided serializer context.
  148. * @param {ObjectSerializerContext} context context
  149. */
  150. serialize(context) {
  151. const { write } = context;
  152. write(this.urlBuffer);
  153. write(this.identifierStr);
  154. write(this.readableIdentifierStr);
  155. super.serialize(context);
  156. }
  157. /**
  158. * Restores this instance from the provided deserializer context.
  159. * @param {ObjectDeserializerContext} context context
  160. */
  161. deserialize(context) {
  162. const { read } = context;
  163. this.urlBuffer = read();
  164. this.identifierStr = read();
  165. this.readableIdentifierStr = read();
  166. super.deserialize(context);
  167. }
  168. }
  169. makeSerializable(RawDataUrlModule, "webpack/lib/asset/RawDataUrlModule");
  170. module.exports = RawDataUrlModule;