AssetBytesGenerator.js 4.6 KB

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