RawDataUrlModule.js 5.7 KB

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