AssetSourceGenerator.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. * @param {ModuleGraph} moduleGraph the module graph
  29. */
  30. constructor(moduleGraph) {
  31. super();
  32. this._moduleGraph = moduleGraph;
  33. }
  34. /**
  35. * @param {NormalModule} module module for which the code should be generated
  36. * @param {GenerateContext} generateContext context for generate
  37. * @returns {Source | null} generated code
  38. */
  39. generate(
  40. module,
  41. { type, concatenationScope, getData, runtimeTemplate, runtimeRequirements }
  42. ) {
  43. const originalSource = module.originalSource();
  44. const data = getData ? getData() : undefined;
  45. switch (type) {
  46. case JAVASCRIPT_TYPE: {
  47. if (!originalSource) {
  48. return new RawSource("");
  49. }
  50. const content = originalSource.source();
  51. const encodedSource =
  52. typeof content === "string" ? content : content.toString("utf8");
  53. /** @type {string} */
  54. let sourceContent;
  55. if (concatenationScope) {
  56. concatenationScope.registerNamespaceExport(
  57. ConcatenationScope.NAMESPACE_OBJECT_EXPORT
  58. );
  59. sourceContent = `${runtimeTemplate.renderConst()} ${
  60. ConcatenationScope.NAMESPACE_OBJECT_EXPORT
  61. } = ${JSON.stringify(encodedSource)};`;
  62. } else {
  63. runtimeRequirements.add(RuntimeGlobals.module);
  64. sourceContent = `${module.moduleArgument}.exports = ${JSON.stringify(
  65. encodedSource
  66. )};`;
  67. }
  68. return new RawSource(sourceContent);
  69. }
  70. case CSS_URL_TYPE: {
  71. if (!originalSource) {
  72. return null;
  73. }
  74. const content = originalSource.source();
  75. const encodedSource =
  76. typeof content === "string" ? content : content.toString("utf8");
  77. if (data) {
  78. data.set("url", { [type]: encodedSource });
  79. }
  80. return null;
  81. }
  82. default:
  83. return null;
  84. }
  85. }
  86. /**
  87. * @param {Error} error the error
  88. * @param {NormalModule} module module for which the code should be generated
  89. * @param {GenerateContext} generateContext context for generate
  90. * @returns {Source | null} generated code
  91. */
  92. generateError(error, module, generateContext) {
  93. switch (generateContext.type) {
  94. case JAVASCRIPT_TYPE: {
  95. return new RawSource(
  96. `throw new Error(${JSON.stringify(error.message)});`
  97. );
  98. }
  99. default:
  100. return null;
  101. }
  102. }
  103. /**
  104. * @param {NormalModule} module module for which the bailout reason should be determined
  105. * @param {ConcatenationBailoutReasonContext} context context
  106. * @returns {string | undefined} reason why this module can't be concatenated, undefined when it can be concatenated
  107. */
  108. getConcatenationBailoutReason(module, context) {
  109. return undefined;
  110. }
  111. /**
  112. * @param {NormalModule} module fresh module
  113. * @returns {SourceTypes} available types (do not mutate)
  114. */
  115. getTypes(module) {
  116. /** @type {Set<string>} */
  117. const sourceTypes = new Set();
  118. const connections = this._moduleGraph.getIncomingConnections(module);
  119. for (const connection of connections) {
  120. if (!connection.originModule) {
  121. continue;
  122. }
  123. sourceTypes.add(connection.originModule.type.split("/")[0]);
  124. }
  125. if (sourceTypes.size > 0) {
  126. if (sourceTypes.has(JAVASCRIPT_TYPE) && sourceTypes.has(CSS_TYPE)) {
  127. return JAVASCRIPT_AND_CSS_URL_TYPES;
  128. } else if (sourceTypes.has(CSS_TYPE)) {
  129. return CSS_URL_TYPES;
  130. }
  131. return JAVASCRIPT_TYPES;
  132. }
  133. return NO_TYPES;
  134. }
  135. /**
  136. * @param {NormalModule} module the module
  137. * @param {SourceType=} type source type
  138. * @returns {number} estimate size of the module
  139. */
  140. getSize(module, type) {
  141. const originalSource = module.originalSource();
  142. if (!originalSource) {
  143. return 0;
  144. }
  145. // Example: m.exports="abcd"
  146. return originalSource.size() + 12;
  147. }
  148. }
  149. module.exports = AssetSourceGenerator;