RawDataUrlModule.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 requestShortener.shorten(this.readableIdentifierStr);
  66. }
  67. /**
  68. * @param {NeedBuildContext} context context info
  69. * @param {function((WebpackError | null)=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
  70. * @returns {void}
  71. */
  72. needBuild(context, callback) {
  73. return callback(null, !this.buildMeta);
  74. }
  75. /**
  76. * @param {WebpackOptions} options webpack options
  77. * @param {Compilation} compilation the compilation
  78. * @param {ResolverWithOptions} resolver the resolver
  79. * @param {InputFileSystem} fs the file system
  80. * @param {function(WebpackError=): void} callback callback function
  81. * @returns {void}
  82. */
  83. build(options, compilation, resolver, fs, callback) {
  84. this.buildMeta = {};
  85. this.buildInfo = {
  86. cacheable: true
  87. };
  88. callback();
  89. }
  90. /**
  91. * @param {CodeGenerationContext} context context for code generation
  92. * @returns {CodeGenerationResult} result
  93. */
  94. codeGeneration(context) {
  95. if (this.url === undefined)
  96. this.url = /** @type {Buffer} */ (this.urlBuffer).toString();
  97. const sources = new Map();
  98. sources.set(
  99. "javascript",
  100. new RawSource(`module.exports = ${JSON.stringify(this.url)};`)
  101. );
  102. const data = new Map();
  103. data.set("url", this.urlBuffer);
  104. const runtimeRequirements = new Set();
  105. runtimeRequirements.add(RuntimeGlobals.module);
  106. return { sources, runtimeRequirements, data };
  107. }
  108. /**
  109. * @param {Hash} hash the hash used to track dependencies
  110. * @param {UpdateHashContext} context context
  111. * @returns {void}
  112. */
  113. updateHash(hash, context) {
  114. hash.update(/** @type {Buffer} */ (this.urlBuffer));
  115. super.updateHash(hash, context);
  116. }
  117. /**
  118. * @param {ObjectSerializerContext} context context
  119. */
  120. serialize(context) {
  121. const { write } = context;
  122. write(this.urlBuffer);
  123. write(this.identifierStr);
  124. write(this.readableIdentifierStr);
  125. super.serialize(context);
  126. }
  127. /**
  128. * @param {ObjectDeserializerContext} context context
  129. */
  130. deserialize(context) {
  131. const { read } = context;
  132. this.urlBuffer = read();
  133. this.identifierStr = read();
  134. this.readableIdentifierStr = read();
  135. super.deserialize(context);
  136. }
  137. }
  138. makeSerializable(RawDataUrlModule, "webpack/lib/asset/RawDataUrlModule");
  139. module.exports = RawDataUrlModule;