AssetSourceGenerator.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 RuntimeGlobals = require("../RuntimeGlobals");
  10. /** @typedef {import("webpack-sources").Source} Source */
  11. /** @typedef {import("../Generator").GenerateContext} GenerateContext */
  12. /** @typedef {import("../Module").ConcatenationBailoutReasonContext} ConcatenationBailoutReasonContext */
  13. /** @typedef {import("../NormalModule")} NormalModule */
  14. const TYPES = new Set(["javascript"]);
  15. class AssetSourceGenerator extends Generator {
  16. /**
  17. * @param {NormalModule} module module for which the code should be generated
  18. * @param {GenerateContext} generateContext context for generate
  19. * @returns {Source} generated code
  20. */
  21. generate(
  22. module,
  23. { concatenationScope, chunkGraph, runtimeTemplate, runtimeRequirements }
  24. ) {
  25. const originalSource = module.originalSource();
  26. if (!originalSource) {
  27. return new RawSource("");
  28. }
  29. const content = originalSource.source();
  30. let encodedSource;
  31. if (typeof content === "string") {
  32. encodedSource = content;
  33. } else {
  34. encodedSource = content.toString("utf-8");
  35. }
  36. let sourceContent;
  37. if (concatenationScope) {
  38. concatenationScope.registerNamespaceExport(
  39. ConcatenationScope.NAMESPACE_OBJECT_EXPORT
  40. );
  41. sourceContent = `${runtimeTemplate.supportsConst() ? "const" : "var"} ${
  42. ConcatenationScope.NAMESPACE_OBJECT_EXPORT
  43. } = ${JSON.stringify(encodedSource)};`;
  44. } else {
  45. runtimeRequirements.add(RuntimeGlobals.module);
  46. sourceContent = `${RuntimeGlobals.module}.exports = ${JSON.stringify(
  47. encodedSource
  48. )};`;
  49. }
  50. return new RawSource(sourceContent);
  51. }
  52. /**
  53. * @param {NormalModule} module module for which the bailout reason should be determined
  54. * @param {ConcatenationBailoutReasonContext} context context
  55. * @returns {string | undefined} reason why this module can't be concatenated, undefined when it can be concatenated
  56. */
  57. getConcatenationBailoutReason(module, context) {
  58. return undefined;
  59. }
  60. /**
  61. * @param {NormalModule} module fresh module
  62. * @returns {Set<string>} available types (do not mutate)
  63. */
  64. getTypes(module) {
  65. return TYPES;
  66. }
  67. /**
  68. * @param {NormalModule} module the module
  69. * @param {string=} type source type
  70. * @returns {number} estimate size of the module
  71. */
  72. getSize(module, type) {
  73. const originalSource = module.originalSource();
  74. if (!originalSource) {
  75. return 0;
  76. }
  77. // Example: m.exports="abcd"
  78. return originalSource.size() + 12;
  79. }
  80. }
  81. module.exports = AssetSourceGenerator;