RawDataUrlModule.js 4.3 KB

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