RawDataUrlModule.js 5.0 KB

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