CssGenerator.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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("../Dependency")} Dependency */
  12. /** @typedef {import("../Generator").GenerateContext} GenerateContext */
  13. /** @typedef {import("../Generator").UpdateHashContext} UpdateHashContext */
  14. /** @typedef {import("../NormalModule")} NormalModule */
  15. /** @typedef {import("../util/Hash")} Hash */
  16. const TYPES = new Set(["css"]);
  17. class CssGenerator extends Generator {
  18. constructor() {
  19. super();
  20. }
  21. /**
  22. * @param {NormalModule} module module for which the code should be generated
  23. * @param {GenerateContext} generateContext context for generate
  24. * @returns {Source} generated code
  25. */
  26. generate(module, generateContext) {
  27. const originalSource = /** @type {Source} */ (module.originalSource());
  28. const source = new ReplaceSource(originalSource);
  29. /** @type {InitFragment[]} */
  30. const initFragments = [];
  31. const cssExports = new Map();
  32. generateContext.runtimeRequirements.add(RuntimeGlobals.hasCssModules);
  33. const templateContext = {
  34. runtimeTemplate: generateContext.runtimeTemplate,
  35. dependencyTemplates: generateContext.dependencyTemplates,
  36. moduleGraph: generateContext.moduleGraph,
  37. chunkGraph: generateContext.chunkGraph,
  38. module,
  39. runtime: generateContext.runtime,
  40. runtimeRequirements: generateContext.runtimeRequirements,
  41. concatenationScope: generateContext.concatenationScope,
  42. codeGenerationResults: generateContext.codeGenerationResults,
  43. initFragments,
  44. cssExports
  45. };
  46. /**
  47. * @param {Dependency} dependency dependency
  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 (module.presentationalDependencies !== undefined)
  63. module.presentationalDependencies.forEach(handleDependency);
  64. if (cssExports.size > 0) {
  65. const data = generateContext.getData();
  66. data.set("css-exports", cssExports);
  67. }
  68. return InitFragment.addToSource(source, initFragments, generateContext);
  69. }
  70. /**
  71. * @param {NormalModule} module fresh module
  72. * @returns {Set<string>} available types (do not mutate)
  73. */
  74. getTypes(module) {
  75. return TYPES;
  76. }
  77. /**
  78. * @param {NormalModule} module the module
  79. * @param {string=} type source type
  80. * @returns {number} estimate size of the module
  81. */
  82. getSize(module, type) {
  83. const originalSource = module.originalSource();
  84. if (!originalSource) {
  85. return 0;
  86. }
  87. return originalSource.size();
  88. }
  89. /**
  90. * @param {Hash} hash hash that will be modified
  91. * @param {UpdateHashContext} updateHashContext context for updating hash
  92. */
  93. updateHash(hash, { module }) {}
  94. }
  95. module.exports = CssGenerator;