CssGenerator.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 = module.originalSource();
  28. const source = new ReplaceSource(originalSource);
  29. const initFragments = [];
  30. const cssExports = new Map();
  31. generateContext.runtimeRequirements.add(RuntimeGlobals.hasCssModules);
  32. const templateContext = {
  33. runtimeTemplate: generateContext.runtimeTemplate,
  34. dependencyTemplates: generateContext.dependencyTemplates,
  35. moduleGraph: generateContext.moduleGraph,
  36. chunkGraph: generateContext.chunkGraph,
  37. module,
  38. runtime: generateContext.runtime,
  39. runtimeRequirements: generateContext.runtimeRequirements,
  40. concatenationScope: generateContext.concatenationScope,
  41. codeGenerationResults: generateContext.codeGenerationResults,
  42. initFragments,
  43. cssExports
  44. };
  45. const handleDependency = dependency => {
  46. const constructor = /** @type {new (...args: any[]) => Dependency} */ (
  47. dependency.constructor
  48. );
  49. const template = generateContext.dependencyTemplates.get(constructor);
  50. if (!template) {
  51. throw new Error(
  52. "No template for dependency: " + dependency.constructor.name
  53. );
  54. }
  55. template.apply(dependency, source, templateContext);
  56. };
  57. module.dependencies.forEach(handleDependency);
  58. if (module.presentationalDependencies !== undefined)
  59. module.presentationalDependencies.forEach(handleDependency);
  60. if (cssExports.size > 0) {
  61. const data = generateContext.getData();
  62. data.set("css-exports", cssExports);
  63. }
  64. return InitFragment.addToSource(source, initFragments, generateContext);
  65. }
  66. /**
  67. * @param {NormalModule} module fresh module
  68. * @returns {Set<string>} available types (do not mutate)
  69. */
  70. getTypes(module) {
  71. return TYPES;
  72. }
  73. /**
  74. * @param {NormalModule} module the module
  75. * @param {string=} type source type
  76. * @returns {number} estimate size of the module
  77. */
  78. getSize(module, type) {
  79. const originalSource = module.originalSource();
  80. if (!originalSource) {
  81. return 0;
  82. }
  83. return originalSource.size();
  84. }
  85. /**
  86. * @param {Hash} hash hash that will be modified
  87. * @param {UpdateHashContext} updateHashContext context for updating hash
  88. */
  89. updateHash(hash, { module }) {}
  90. }
  91. module.exports = CssGenerator;