UNPKG

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