CssExportsGenerator.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. const { cssExportConvention } = require("../util/conventions");
  12. /** @typedef {import("webpack-sources").Source} Source */
  13. /** @typedef {import("../../declarations/WebpackOptions").CssGeneratorExportsConvention} CssGeneratorExportsConvention */
  14. /** @typedef {import("../../declarations/WebpackOptions").CssGeneratorLocalIdentName} CssGeneratorLocalIdentName */
  15. /** @typedef {import("../Dependency")} Dependency */
  16. /** @typedef {import("../Generator").GenerateContext} GenerateContext */
  17. /** @typedef {import("../Generator").UpdateHashContext} UpdateHashContext */
  18. /** @typedef {import("../Module").ConcatenationBailoutReasonContext} ConcatenationBailoutReasonContext */
  19. /** @typedef {import("../NormalModule")} NormalModule */
  20. /** @typedef {import("../util/Hash")} Hash */
  21. /**
  22. * @template T
  23. * @typedef {import("../InitFragment")<T>} InitFragment
  24. */
  25. const TYPES = new Set(["javascript"]);
  26. class CssExportsGenerator extends Generator {
  27. /**
  28. * @param {CssGeneratorExportsConvention} convention the convention of the exports name
  29. * @param {CssGeneratorLocalIdentName | undefined} localIdentName css export local ident name
  30. */
  31. constructor(convention, localIdentName) {
  32. super();
  33. /** @type {CssGeneratorExportsConvention} */
  34. this.convention = convention;
  35. /** @type {CssGeneratorLocalIdentName | undefined} */
  36. this.localIdentName = localIdentName;
  37. }
  38. // TODO add getConcatenationBailoutReason to allow concatenation
  39. // but how to make it have a module id
  40. /**
  41. * @param {NormalModule} module module for which the code should be generated
  42. * @param {GenerateContext} generateContext context for generate
  43. * @returns {Source} generated code
  44. */
  45. generate(module, generateContext) {
  46. const source = new ReplaceSource(new RawSource(""));
  47. /** @type {InitFragment<TODO>[]} */
  48. const initFragments = [];
  49. /** @type {Map<string, string>} */
  50. const cssExports = new Map();
  51. generateContext.runtimeRequirements.add(RuntimeGlobals.module);
  52. let chunkInitFragments;
  53. const runtimeRequirements = new Set();
  54. const templateContext = {
  55. runtimeTemplate: generateContext.runtimeTemplate,
  56. dependencyTemplates: generateContext.dependencyTemplates,
  57. moduleGraph: generateContext.moduleGraph,
  58. chunkGraph: generateContext.chunkGraph,
  59. module,
  60. runtime: generateContext.runtime,
  61. runtimeRequirements: runtimeRequirements,
  62. concatenationScope: generateContext.concatenationScope,
  63. codeGenerationResults: generateContext.codeGenerationResults,
  64. initFragments,
  65. cssExports,
  66. get chunkInitFragments() {
  67. if (!chunkInitFragments) {
  68. const data = generateContext.getData();
  69. chunkInitFragments = data.get("chunkInitFragments");
  70. if (!chunkInitFragments) {
  71. chunkInitFragments = [];
  72. data.set("chunkInitFragments", chunkInitFragments);
  73. }
  74. }
  75. return chunkInitFragments;
  76. }
  77. };
  78. /**
  79. * @param {Dependency} dependency the dependency
  80. */
  81. const handleDependency = dependency => {
  82. const constructor = /** @type {new (...args: any[]) => Dependency} */ (
  83. dependency.constructor
  84. );
  85. const template = generateContext.dependencyTemplates.get(constructor);
  86. if (!template) {
  87. throw new Error(
  88. "No template for dependency: " + dependency.constructor.name
  89. );
  90. }
  91. template.apply(dependency, source, templateContext);
  92. };
  93. module.dependencies.forEach(handleDependency);
  94. if (generateContext.concatenationScope) {
  95. const source = new ConcatSource();
  96. const usedIdentifiers = new Set();
  97. for (const [name, v] of cssExports) {
  98. for (let k of cssExportConvention(name, this.convention)) {
  99. let identifier = Template.toIdentifier(k);
  100. let i = 0;
  101. while (usedIdentifiers.has(identifier)) {
  102. identifier = Template.toIdentifier(k + i);
  103. }
  104. usedIdentifiers.add(identifier);
  105. generateContext.concatenationScope.registerExport(k, identifier);
  106. source.add(
  107. `${
  108. generateContext.runtimeTemplate.supportsConst() ? "const" : "var"
  109. } ${identifier} = ${JSON.stringify(v)};\n`
  110. );
  111. }
  112. }
  113. return source;
  114. } else {
  115. const otherUsed =
  116. generateContext.moduleGraph
  117. .getExportsInfo(module)
  118. .otherExportsInfo.getUsed(generateContext.runtime) !==
  119. UsageState.Unused;
  120. if (otherUsed) {
  121. generateContext.runtimeRequirements.add(
  122. RuntimeGlobals.makeNamespaceObject
  123. );
  124. }
  125. const newCssExports = [];
  126. for (let [k, v] of cssExports) {
  127. for (let name of cssExportConvention(k, this.convention)) {
  128. newCssExports.push(`\t${JSON.stringify(name)}: ${JSON.stringify(v)}`);
  129. }
  130. }
  131. return new RawSource(
  132. `${otherUsed ? `${RuntimeGlobals.makeNamespaceObject}(` : ""}${
  133. module.moduleArgument
  134. }.exports = {\n${newCssExports.join(",\n")}\n}${otherUsed ? ")" : ""};`
  135. );
  136. }
  137. }
  138. /**
  139. * @param {NormalModule} module fresh module
  140. * @returns {Set<string>} available types (do not mutate)
  141. */
  142. getTypes(module) {
  143. return TYPES;
  144. }
  145. /**
  146. * @param {NormalModule} module the module
  147. * @param {string=} type source type
  148. * @returns {number} estimate size of the module
  149. */
  150. getSize(module, type) {
  151. return 42;
  152. }
  153. /**
  154. * @param {Hash} hash hash that will be modified
  155. * @param {UpdateHashContext} updateHashContext context for updating hash
  156. */
  157. updateHash(hash, { module }) {}
  158. }
  159. module.exports = CssExportsGenerator;