AssetSourceGenerator.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Sergey Melyukov @smelukov
  4. */
  5. "use strict";
  6. const { RawSource } = require("webpack-sources");
  7. const ConcatenationScope = require("../ConcatenationScope");
  8. const Generator = require("../Generator");
  9. const {
  10. CSS_TYPE,
  11. CSS_URL_TYPE,
  12. CSS_URL_TYPES,
  13. JAVASCRIPT_AND_CSS_URL_TYPES,
  14. JAVASCRIPT_TYPE,
  15. JAVASCRIPT_TYPES,
  16. NO_TYPES
  17. } = require("../ModuleSourceTypeConstants");
  18. const RuntimeGlobals = require("../RuntimeGlobals");
  19. /** @typedef {import("webpack-sources").Source} Source */
  20. /** @typedef {import("../Generator").GenerateContext} GenerateContext */
  21. /** @typedef {import("../Module").ConcatenationBailoutReasonContext} ConcatenationBailoutReasonContext */
  22. /** @typedef {import("../Module").SourceType} SourceType */
  23. /** @typedef {import("../Module").SourceTypes} SourceTypes */
  24. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  25. /** @typedef {import("../NormalModule")} NormalModule */
  26. class AssetSourceGenerator extends Generator {
  27. /**
  28. * Creates an instance of AssetSourceGenerator.
  29. * @param {ModuleGraph} moduleGraph the module graph
  30. */
  31. constructor(moduleGraph) {
  32. super();
  33. this._moduleGraph = moduleGraph;
  34. }
  35. /**
  36. * Generates generated code for this runtime module.
  37. * @param {NormalModule} module module for which the code should be generated
  38. * @param {GenerateContext} generateContext context for generate
  39. * @returns {Source | null} generated code
  40. */
  41. generate(
  42. module,
  43. { type, concatenationScope, getData, runtimeTemplate, runtimeRequirements }
  44. ) {
  45. const originalSource = module.originalSource();
  46. const data = getData ? getData() : undefined;
  47. switch (type) {
  48. case JAVASCRIPT_TYPE: {
  49. if (!originalSource) {
  50. return new RawSource("");
  51. }
  52. const content = originalSource.source();
  53. const encodedSource =
  54. typeof content === "string" ? content : content.toString("utf8");
  55. /** @type {string} */
  56. let sourceContent;
  57. if (concatenationScope) {
  58. concatenationScope.registerNamespaceExport(
  59. ConcatenationScope.NAMESPACE_OBJECT_EXPORT
  60. );
  61. sourceContent = `${runtimeTemplate.renderConst()} ${
  62. ConcatenationScope.NAMESPACE_OBJECT_EXPORT
  63. } = ${JSON.stringify(encodedSource)};`;
  64. } else {
  65. runtimeRequirements.add(RuntimeGlobals.module);
  66. sourceContent = `${module.moduleArgument}.exports = ${JSON.stringify(
  67. encodedSource
  68. )};`;
  69. }
  70. return new RawSource(sourceContent);
  71. }
  72. case CSS_URL_TYPE: {
  73. if (!originalSource) {
  74. return null;
  75. }
  76. const content = originalSource.source();
  77. const encodedSource =
  78. typeof content === "string" ? content : content.toString("utf8");
  79. if (data) {
  80. data.set("url", { [type]: encodedSource });
  81. }
  82. return null;
  83. }
  84. default:
  85. return null;
  86. }
  87. }
  88. /**
  89. * Generates fallback output for the provided error condition.
  90. * @param {Error} error the error
  91. * @param {NormalModule} module module for which the code should be generated
  92. * @param {GenerateContext} generateContext context for generate
  93. * @returns {Source | null} generated code
  94. */
  95. generateError(error, module, generateContext) {
  96. switch (generateContext.type) {
  97. case JAVASCRIPT_TYPE: {
  98. return new RawSource(
  99. `throw new Error(${JSON.stringify(error.message)});`
  100. );
  101. }
  102. default:
  103. return null;
  104. }
  105. }
  106. /**
  107. * Returns the reason this module cannot be concatenated, when one exists.
  108. * @param {NormalModule} module module for which the bailout reason should be determined
  109. * @param {ConcatenationBailoutReasonContext} context context
  110. * @returns {string | undefined} reason why this module can't be concatenated, undefined when it can be concatenated
  111. */
  112. getConcatenationBailoutReason(module, context) {
  113. return undefined;
  114. }
  115. /**
  116. * Returns the source types available for this module.
  117. * @param {NormalModule} module fresh module
  118. * @returns {SourceTypes} available types (do not mutate)
  119. */
  120. getTypes(module) {
  121. /** @type {Set<string>} */
  122. const sourceTypes = new Set();
  123. const connections = this._moduleGraph.getIncomingConnections(module);
  124. for (const connection of connections) {
  125. if (!connection.originModule) {
  126. continue;
  127. }
  128. sourceTypes.add(connection.originModule.type.split("/")[0]);
  129. }
  130. if (sourceTypes.size > 0) {
  131. if (sourceTypes.has(JAVASCRIPT_TYPE) && sourceTypes.has(CSS_TYPE)) {
  132. return JAVASCRIPT_AND_CSS_URL_TYPES;
  133. } else if (sourceTypes.has(CSS_TYPE)) {
  134. return CSS_URL_TYPES;
  135. }
  136. return JAVASCRIPT_TYPES;
  137. }
  138. return NO_TYPES;
  139. }
  140. /**
  141. * Returns the estimated size for the requested source type.
  142. * @param {NormalModule} module the module
  143. * @param {SourceType=} type source type
  144. * @returns {number} estimate size of the module
  145. */
  146. getSize(module, type) {
  147. const originalSource = module.originalSource();
  148. if (!originalSource) {
  149. return 0;
  150. }
  151. // Example: m.exports="abcd"
  152. return originalSource.size() + 12;
  153. }
  154. }
  155. module.exports = AssetSourceGenerator;