CssGenerator.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. /** @typedef {import("webpack-sources").Source} Source */
  11. /** @typedef {import("../../declarations/WebpackOptions").CssGeneratorExportsConvention} CssGeneratorExportsConvention */
  12. /** @typedef {import("../../declarations/WebpackOptions").CssGeneratorLocalIdentName} CssGeneratorLocalIdentName */
  13. /** @typedef {import("../Dependency")} Dependency */
  14. /** @typedef {import("../DependencyTemplate").CssDependencyTemplateContext} DependencyTemplateContext */
  15. /** @typedef {import("../DependencyTemplate").CssExportsData} CssExportsData */
  16. /** @typedef {import("../Generator").GenerateContext} GenerateContext */
  17. /** @typedef {import("../Generator").UpdateHashContext} UpdateHashContext */
  18. /** @typedef {import("../NormalModule")} NormalModule */
  19. /** @typedef {import("../util/Hash")} Hash */
  20. const TYPES = new Set(["css"]);
  21. class CssGenerator extends Generator {
  22. /**
  23. * @param {CssGeneratorExportsConvention | undefined} convention the convention of the exports name
  24. * @param {CssGeneratorLocalIdentName | undefined} localIdentName css export local ident name
  25. * @param {boolean} esModule whether to use ES modules syntax
  26. */
  27. constructor(convention, localIdentName, esModule) {
  28. super();
  29. /** @type {CssGeneratorExportsConvention | undefined} */
  30. this.convention = convention;
  31. /** @type {CssGeneratorLocalIdentName | undefined} */
  32. this.localIdentName = localIdentName;
  33. /** @type {boolean} */
  34. this.esModule = esModule;
  35. }
  36. /**
  37. * @param {NormalModule} module module for which the code should be generated
  38. * @param {GenerateContext} generateContext context for generate
  39. * @returns {Source} generated code
  40. */
  41. generate(module, generateContext) {
  42. const originalSource = /** @type {Source} */ (module.originalSource());
  43. const source = new ReplaceSource(originalSource);
  44. /** @type {InitFragment[]} */
  45. const initFragments = [];
  46. /** @type {CssExportsData} */
  47. const cssExportsData = {
  48. esModule: this.esModule,
  49. exports: new Map()
  50. };
  51. generateContext.runtimeRequirements.add(RuntimeGlobals.hasCssModules);
  52. let chunkInitFragments;
  53. /** @type {DependencyTemplateContext} */
  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: generateContext.runtimeRequirements,
  62. concatenationScope: generateContext.concatenationScope,
  63. codeGenerationResults: generateContext.codeGenerationResults,
  64. initFragments,
  65. cssExportsData,
  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 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 (module.presentationalDependencies !== undefined)
  95. module.presentationalDependencies.forEach(handleDependency);
  96. const data = generateContext.getData();
  97. data.set("css-exports", cssExportsData);
  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. hash.update(this.esModule.toString());
  125. }
  126. }
  127. module.exports = CssGenerator;