CssGenerator.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Sergey Melyukov @smelukov
  4. */
  5. "use strict";
  6. const { ReplaceSource } = require("webpack-sources");
  7. const Generator = require("../Generator");
  8. const InitFragment = require("../InitFragment");
  9. const RuntimeGlobals = require("../RuntimeGlobals");
  10. const { cssExportConvention } = require("../util/conventions");
  11. /** @typedef {import("webpack-sources").Source} Source */
  12. /** @typedef {import("../../declarations/WebpackOptions").CssGeneratorExportsConvention} CssGeneratorExportsConvention */
  13. /** @typedef {import("../../declarations/WebpackOptions").CssGeneratorLocalIdentName} CssGeneratorLocalIdentName */
  14. /** @typedef {import("../Dependency")} Dependency */
  15. /** @typedef {import("../Generator").GenerateContext} GenerateContext */
  16. /** @typedef {import("../Generator").UpdateHashContext} UpdateHashContext */
  17. /** @typedef {import("../NormalModule")} NormalModule */
  18. /** @typedef {import("../util/Hash")} Hash */
  19. const TYPES = new Set(["css"]);
  20. class CssGenerator extends Generator {
  21. /**
  22. * @param {CssGeneratorExportsConvention} convention the convention of the exports name
  23. * @param {CssGeneratorLocalIdentName | undefined} localIdentName css export local ident name
  24. */
  25. constructor(convention, localIdentName) {
  26. super();
  27. /** @type {CssGeneratorExportsConvention} */
  28. this.convention = convention;
  29. /** @type {CssGeneratorLocalIdentName | undefined} */
  30. this.localIdentName = localIdentName;
  31. }
  32. /**
  33. * @param {NormalModule} module module for which the code should be generated
  34. * @param {GenerateContext} generateContext context for generate
  35. * @returns {Source} generated code
  36. */
  37. generate(module, generateContext) {
  38. const originalSource = /** @type {Source} */ (module.originalSource());
  39. const source = new ReplaceSource(originalSource);
  40. /** @type {InitFragment[]} */
  41. const initFragments = [];
  42. /** @type {Map<string, string>} */
  43. const cssExports = new Map();
  44. generateContext.runtimeRequirements.add(RuntimeGlobals.hasCssModules);
  45. let chunkInitFragments;
  46. const templateContext = {
  47. runtimeTemplate: generateContext.runtimeTemplate,
  48. dependencyTemplates: generateContext.dependencyTemplates,
  49. moduleGraph: generateContext.moduleGraph,
  50. chunkGraph: generateContext.chunkGraph,
  51. module,
  52. runtime: generateContext.runtime,
  53. runtimeRequirements: generateContext.runtimeRequirements,
  54. concatenationScope: generateContext.concatenationScope,
  55. codeGenerationResults: generateContext.codeGenerationResults,
  56. initFragments,
  57. cssExports,
  58. get chunkInitFragments() {
  59. if (!chunkInitFragments) {
  60. const data = generateContext.getData();
  61. chunkInitFragments = data.get("chunkInitFragments");
  62. if (!chunkInitFragments) {
  63. chunkInitFragments = [];
  64. data.set("chunkInitFragments", chunkInitFragments);
  65. }
  66. }
  67. return chunkInitFragments;
  68. }
  69. };
  70. /**
  71. * @param {Dependency} dependency dependency
  72. */
  73. const handleDependency = dependency => {
  74. const constructor = /** @type {new (...args: any[]) => Dependency} */ (
  75. dependency.constructor
  76. );
  77. const template = generateContext.dependencyTemplates.get(constructor);
  78. if (!template) {
  79. throw new Error(
  80. "No template for dependency: " + dependency.constructor.name
  81. );
  82. }
  83. template.apply(dependency, source, templateContext);
  84. };
  85. module.dependencies.forEach(handleDependency);
  86. if (module.presentationalDependencies !== undefined)
  87. module.presentationalDependencies.forEach(handleDependency);
  88. if (cssExports.size > 0) {
  89. const newCssExports = new Map();
  90. for (let [name, v] of cssExports) {
  91. for (let newName of cssExportConvention(name, this.convention)) {
  92. newCssExports.set(newName, v);
  93. }
  94. }
  95. const data = generateContext.getData();
  96. data.set("css-exports", newCssExports);
  97. }
  98. return InitFragment.addToSource(source, initFragments, generateContext);
  99. }
  100. /**
  101. * @param {NormalModule} module fresh module
  102. * @returns {Set<string>} available types (do not mutate)
  103. */
  104. getTypes(module) {
  105. return TYPES;
  106. }
  107. /**
  108. * @param {NormalModule} module the module
  109. * @param {string=} type source type
  110. * @returns {number} estimate size of the module
  111. */
  112. getSize(module, type) {
  113. const originalSource = module.originalSource();
  114. if (!originalSource) {
  115. return 0;
  116. }
  117. return originalSource.size();
  118. }
  119. /**
  120. * @param {Hash} hash hash that will be modified
  121. * @param {UpdateHashContext} updateHashContext context for updating hash
  122. */
  123. updateHash(hash, { module }) {}
  124. }
  125. module.exports = CssGenerator;