RawDataUrlModule.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. * @param {string} url raw url
  36. * @param {string} identifier unique identifier
  37. * @param {string=} readableIdentifier readable identifier
  38. */
  39. constructor(url, identifier, readableIdentifier) {
  40. super(ASSET_MODULE_TYPE_RAW_DATA_URL, null);
  41. /** @type {string} */
  42. this.url = url;
  43. /** @type {Buffer | undefined} */
  44. this.urlBuffer = url ? Buffer.from(url) : undefined;
  45. /** @type {string} */
  46. this.identifierStr = identifier;
  47. /** @type {string} */
  48. this.readableIdentifierStr = readableIdentifier || this.identifierStr;
  49. }
  50. /**
  51. * @returns {SourceTypes} types available (do not mutate)
  52. */
  53. getSourceTypes() {
  54. return JAVASCRIPT_TYPES;
  55. }
  56. /**
  57. * @returns {string} a unique identifier of the module
  58. */
  59. identifier() {
  60. return this.identifierStr;
  61. }
  62. /**
  63. * @param {string=} type the source type for which the size should be estimated
  64. * @returns {number} the estimated size of the module (must be non-zero)
  65. */
  66. size(type) {
  67. if (this.url === undefined) {
  68. this.url = /** @type {Buffer} */ (this.urlBuffer).toString();
  69. }
  70. return Math.max(1, this.url.length);
  71. }
  72. /**
  73. * @param {RequestShortener} requestShortener the request shortener
  74. * @returns {string} a user readable identifier of the module
  75. */
  76. readableIdentifier(requestShortener) {
  77. return /** @type {string} */ (
  78. requestShortener.shorten(this.readableIdentifierStr)
  79. );
  80. }
  81. /**
  82. * @param {NeedBuildContext} context context info
  83. * @param {NeedBuildCallback} callback callback function, returns true, if the module needs a rebuild
  84. * @returns {void}
  85. */
  86. needBuild(context, callback) {
  87. return callback(null, !this.buildMeta);
  88. }
  89. /**
  90. * @param {WebpackOptions} options webpack options
  91. * @param {Compilation} compilation the compilation
  92. * @param {ResolverWithOptions} resolver the resolver
  93. * @param {InputFileSystem} fs the file system
  94. * @param {BuildCallback} callback callback function
  95. * @returns {void}
  96. */
  97. build(options, compilation, resolver, fs, callback) {
  98. this.buildMeta = {};
  99. this.buildInfo = {
  100. cacheable: true
  101. };
  102. callback();
  103. }
  104. /**
  105. * @param {CodeGenerationContext} context context for code generation
  106. * @returns {CodeGenerationResult} result
  107. */
  108. codeGeneration(context) {
  109. if (this.url === undefined) {
  110. this.url = /** @type {Buffer} */ (this.urlBuffer).toString();
  111. }
  112. /** @type {Sources} */
  113. const sources = new Map();
  114. sources.set(
  115. JAVASCRIPT_TYPE,
  116. new RawSource(`module.exports = ${JSON.stringify(this.url)};`)
  117. );
  118. /** @type {CodeGenerationResultData} */
  119. const data = new Map();
  120. data.set("url", {
  121. javascript: this.url
  122. });
  123. /** @type {RuntimeRequirements} */
  124. const runtimeRequirements = new Set();
  125. runtimeRequirements.add(RuntimeGlobals.module);
  126. return { sources, runtimeRequirements, data };
  127. }
  128. /**
  129. * @param {Hash} hash the hash used to track dependencies
  130. * @param {UpdateHashContext} context context
  131. * @returns {void}
  132. */
  133. updateHash(hash, context) {
  134. hash.update(/** @type {Buffer} */ (this.urlBuffer));
  135. super.updateHash(hash, context);
  136. }
  137. /**
  138. * @param {ObjectSerializerContext} context context
  139. */
  140. serialize(context) {
  141. const { write } = context;
  142. write(this.urlBuffer);
  143. write(this.identifierStr);
  144. write(this.readableIdentifierStr);
  145. super.serialize(context);
  146. }
  147. /**
  148. * @param {ObjectDeserializerContext} context context
  149. */
  150. deserialize(context) {
  151. const { read } = context;
  152. this.urlBuffer = read();
  153. this.identifierStr = read();
  154. this.readableIdentifierStr = read();
  155. super.deserialize(context);
  156. }
  157. }
  158. makeSerializable(RawDataUrlModule, "webpack/lib/asset/RawDataUrlModule");
  159. module.exports = RawDataUrlModule;