1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const { ReplaceSource } = require("webpack-sources");
|
9 | const Generator = require("../Generator");
|
10 | const InitFragment = require("../InitFragment");
|
11 | const RuntimeGlobals = require("../RuntimeGlobals");
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | const TYPES = new Set(["css"]);
|
21 |
|
22 | class CssGenerator extends Generator {
|
23 | constructor() {
|
24 | super();
|
25 | }
|
26 |
|
27 | |
28 |
|
29 |
|
30 |
|
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 |
|
81 |
|
82 |
|
83 | getTypes(module) {
|
84 | return TYPES;
|
85 | }
|
86 |
|
87 | |
88 |
|
89 |
|
90 |
|
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 |
|
104 |
|
105 |
|
106 | updateHash(hash, { module }) {}
|
107 | }
|
108 |
|
109 | module.exports = CssGenerator;
|