AssetSourceGenerator.js 4.4 KB

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