RawDataUrlModule.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 { ASSET_MODULE_TYPE_RAW_DATA_URL } = require("../ModuleTypeConstants");
  9. const RuntimeGlobals = require("../RuntimeGlobals");
  10. const makeSerializable = require("../util/makeSerializable");
  11. /** @typedef {import("../../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
  12. /** @typedef {import("../Compilation")} Compilation */
  13. /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
  14. /** @typedef {import("../Module").CodeGenerationContext} CodeGenerationContext */
  15. /** @typedef {import("../Module").CodeGenerationResult} CodeGenerationResult */
  16. /** @typedef {import("../Module").NeedBuildContext} NeedBuildContext */
  17. /** @typedef {import("../Module").SourceTypes} SourceTypes */
  18. /** @typedef {import("../RequestShortener")} RequestShortener */
  19. /** @typedef {import("../ResolverFactory").ResolverWithOptions} ResolverWithOptions */
  20. /** @typedef {import("../WebpackError")} WebpackError */
  21. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  22. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  23. /** @typedef {import("../util/Hash")} Hash */
  24. /** @typedef {import("../util/fs").InputFileSystem} InputFileSystem */
  25. const TYPES = new Set(["javascript"]);
  26. class RawDataUrlModule extends Module {
  27. /**
  28. * @param {string} url raw url
  29. * @param {string} identifier unique identifier
  30. * @param {string=} readableIdentifier readable identifier
  31. */
  32. constructor(url, identifier, readableIdentifier) {
  33. super(ASSET_MODULE_TYPE_RAW_DATA_URL, null);
  34. this.url = url;
  35. this.urlBuffer = url ? Buffer.from(url) : undefined;
  36. this.identifierStr = identifier || this.url;
  37. this.readableIdentifierStr = readableIdentifier || this.identifierStr;
  38. }
  39. /**
  40. * @returns {SourceTypes} 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. if (this.url === undefined)
  57. this.url = /** @type {Buffer} */ (this.urlBuffer).toString();
  58. return Math.max(1, this.url.length);
  59. }
  60. /**
  61. * @param {RequestShortener} requestShortener the request shortener
  62. * @returns {string} a user readable identifier of the module
  63. */
  64. readableIdentifier(requestShortener) {
  65. return /** @type {string} */ (
  66. requestShortener.shorten(this.readableIdentifierStr)
  67. );
  68. }
  69. /**
  70. * @param {NeedBuildContext} context context info
  71. * @param {function((WebpackError | null)=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
  72. * @returns {void}
  73. */
  74. needBuild(context, callback) {
  75. return callback(null, !this.buildMeta);
  76. }
  77. /**
  78. * @param {WebpackOptions} options webpack options
  79. * @param {Compilation} compilation the compilation
  80. * @param {ResolverWithOptions} resolver the resolver
  81. * @param {InputFileSystem} fs the file system
  82. * @param {function(WebpackError=): void} callback callback function
  83. * @returns {void}
  84. */
  85. build(options, compilation, resolver, fs, callback) {
  86. this.buildMeta = {};
  87. this.buildInfo = {
  88. cacheable: true
  89. };
  90. callback();
  91. }
  92. /**
  93. * @param {CodeGenerationContext} context context for code generation
  94. * @returns {CodeGenerationResult} result
  95. */
  96. codeGeneration(context) {
  97. if (this.url === undefined)
  98. this.url = /** @type {Buffer} */ (this.urlBuffer).toString();
  99. const sources = new Map();
  100. sources.set(
  101. "javascript",
  102. new RawSource(`module.exports = ${JSON.stringify(this.url)};`)
  103. );
  104. const data = new Map();
  105. data.set("url", this.urlBuffer);
  106. const runtimeRequirements = new Set();
  107. runtimeRequirements.add(RuntimeGlobals.module);
  108. return { sources, runtimeRequirements, data };
  109. }
  110. /**
  111. * @param {Hash} hash the hash used to track dependencies
  112. * @param {UpdateHashContext} context context
  113. * @returns {void}
  114. */
  115. updateHash(hash, context) {
  116. hash.update(/** @type {Buffer} */ (this.urlBuffer));
  117. super.updateHash(hash, context);
  118. }
  119. /**
  120. * @param {ObjectSerializerContext} context context
  121. */
  122. serialize(context) {
  123. const { write } = context;
  124. write(this.urlBuffer);
  125. write(this.identifierStr);
  126. write(this.readableIdentifierStr);
  127. super.serialize(context);
  128. }
  129. /**
  130. * @param {ObjectDeserializerContext} context context
  131. */
  132. deserialize(context) {
  133. const { read } = context;
  134. this.urlBuffer = read();
  135. this.identifierStr = read();
  136. this.readableIdentifierStr = read();
  137. super.deserialize(context);
  138. }
  139. }
  140. makeSerializable(RawDataUrlModule, "webpack/lib/asset/RawDataUrlModule");
  141. module.exports = RawDataUrlModule;