RawDataUrlModule.js 5.1 KB

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