RawDataUrlModule.js 4.8 KB

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