RawDataUrlModule.js 5.0 KB

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