CssExportsGenerator.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Sergey Melyukov @smelukov
  4. */
  5. "use strict";
  6. const { ReplaceSource, RawSource, ConcatSource } = require("webpack-sources");
  7. const { UsageState } = require("../ExportsInfo");
  8. const Generator = require("../Generator");
  9. const RuntimeGlobals = require("../RuntimeGlobals");
  10. const Template = require("../Template");
  11. /** @typedef {import("webpack-sources").Source} Source */
  12. /** @typedef {import("../Dependency")} Dependency */
  13. /** @typedef {import("../Generator").GenerateContext} GenerateContext */
  14. /** @typedef {import("../Generator").UpdateHashContext} UpdateHashContext */
  15. /** @typedef {import("../Module").ConcatenationBailoutReasonContext} ConcatenationBailoutReasonContext */
  16. /** @typedef {import("../NormalModule")} NormalModule */
  17. /** @typedef {import("../util/Hash")} Hash */
  18. const TYPES = new Set(["javascript"]);
  19. class CssExportsGenerator extends Generator {
  20. constructor() {
  21. super();
  22. }
  23. // TODO add getConcatenationBailoutReason to allow concatenation
  24. // but how to make it have a module id
  25. /**
  26. * @param {NormalModule} module module for which the code should be generated
  27. * @param {GenerateContext} generateContext context for generate
  28. * @returns {Source} generated code
  29. */
  30. generate(module, generateContext) {
  31. const source = new ReplaceSource(new RawSource(""));
  32. const initFragments = [];
  33. const cssExports = new Map();
  34. generateContext.runtimeRequirements.add(RuntimeGlobals.module);
  35. const runtimeRequirements = new Set();
  36. const templateContext = {
  37. runtimeTemplate: generateContext.runtimeTemplate,
  38. dependencyTemplates: generateContext.dependencyTemplates,
  39. moduleGraph: generateContext.moduleGraph,
  40. chunkGraph: generateContext.chunkGraph,
  41. module,
  42. runtime: generateContext.runtime,
  43. runtimeRequirements: runtimeRequirements,
  44. concatenationScope: generateContext.concatenationScope,
  45. codeGenerationResults: generateContext.codeGenerationResults,
  46. initFragments,
  47. cssExports
  48. };
  49. const handleDependency = dependency => {
  50. const constructor = /** @type {new (...args: any[]) => Dependency} */ (
  51. dependency.constructor
  52. );
  53. const template = generateContext.dependencyTemplates.get(constructor);
  54. if (!template) {
  55. throw new Error(
  56. "No template for dependency: " + dependency.constructor.name
  57. );
  58. }
  59. template.apply(dependency, source, templateContext);
  60. };
  61. module.dependencies.forEach(handleDependency);
  62. if (generateContext.concatenationScope) {
  63. const source = new ConcatSource();
  64. const usedIdentifiers = new Set();
  65. for (const [k, v] of cssExports) {
  66. let identifier = Template.toIdentifier(k);
  67. let i = 0;
  68. while (usedIdentifiers.has(identifier)) {
  69. identifier = Template.toIdentifier(k + i);
  70. }
  71. usedIdentifiers.add(identifier);
  72. generateContext.concatenationScope.registerExport(k, identifier);
  73. source.add(
  74. `${
  75. generateContext.runtimeTemplate.supportsConst ? "const" : "var"
  76. } ${identifier} = ${JSON.stringify(v)};\n`
  77. );
  78. }
  79. return source;
  80. } else {
  81. const otherUsed =
  82. generateContext.moduleGraph
  83. .getExportsInfo(module)
  84. .otherExportsInfo.getUsed(generateContext.runtime) !==
  85. UsageState.Unused;
  86. if (otherUsed) {
  87. generateContext.runtimeRequirements.add(
  88. RuntimeGlobals.makeNamespaceObject
  89. );
  90. }
  91. return new RawSource(
  92. `${otherUsed ? `${RuntimeGlobals.makeNamespaceObject}(` : ""}${
  93. module.moduleArgument
  94. }.exports = {\n${Array.from(
  95. cssExports,
  96. ([k, v]) => `\t${JSON.stringify(k)}: ${JSON.stringify(v)}`
  97. ).join(",\n")}\n}${otherUsed ? ")" : ""};`
  98. );
  99. }
  100. }
  101. /**
  102. * @param {NormalModule} module fresh module
  103. * @returns {Set<string>} available types (do not mutate)
  104. */
  105. getTypes(module) {
  106. return TYPES;
  107. }
  108. /**
  109. * @param {NormalModule} module the module
  110. * @param {string=} type source type
  111. * @returns {number} estimate size of the module
  112. */
  113. getSize(module, type) {
  114. return 42;
  115. }
  116. /**
  117. * @param {Hash} hash hash that will be modified
  118. * @param {UpdateHashContext} updateHashContext context for updating hash
  119. */
  120. updateHash(hash, { module }) {}
  121. }
  122. module.exports = CssExportsGenerator;