AssetSourceGenerator.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 {NormalModule} module module for which the bailout reason should be determined
  83. * @param {ConcatenationBailoutReasonContext} context context
  84. * @returns {string | undefined} reason why this module can't be concatenated, undefined when it can be concatenated
  85. */
  86. getConcatenationBailoutReason(module, context) {
  87. return undefined;
  88. }
  89. /**
  90. * @param {NormalModule} module fresh module
  91. * @returns {SourceTypes} available types (do not mutate)
  92. */
  93. getTypes(module) {
  94. const sourceTypes = new Set();
  95. const connections = this._moduleGraph.getIncomingConnections(module);
  96. for (const connection of connections) {
  97. if (!connection.originModule) {
  98. continue;
  99. }
  100. sourceTypes.add(connection.originModule.type.split("/")[0]);
  101. }
  102. if (sourceTypes.has("javascript") && sourceTypes.has("css")) {
  103. return JS_AND_CSS_URL_TYPES;
  104. } else if (sourceTypes.has("javascript")) {
  105. return JS_TYPES;
  106. } else if (sourceTypes.has("css")) {
  107. return CSS_URL_TYPES;
  108. }
  109. return NO_TYPES;
  110. }
  111. /**
  112. * @param {NormalModule} module the module
  113. * @param {string=} type source type
  114. * @returns {number} estimate size of the module
  115. */
  116. getSize(module, type) {
  117. const originalSource = module.originalSource();
  118. if (!originalSource) {
  119. return 0;
  120. }
  121. // Example: m.exports="abcd"
  122. return originalSource.size() + 12;
  123. }
  124. }
  125. module.exports = AssetSourceGenerator;