source: imaps-frontend/node_modules/webpack/lib/css/CssGenerator.js

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 8.0 KB
Line 
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, RawSource, ConcatSource } = require("webpack-sources");
9const { UsageState } = require("../ExportsInfo");
10const Generator = require("../Generator");
11const InitFragment = require("../InitFragment");
12const {
13 JS_AND_CSS_EXPORT_TYPES,
14 JS_AND_CSS_TYPES
15} = require("../ModuleSourceTypesConstants");
16const RuntimeGlobals = require("../RuntimeGlobals");
17const Template = require("../Template");
18
19/** @typedef {import("webpack-sources").Source} Source */
20/** @typedef {import("../../declarations/WebpackOptions").CssAutoGeneratorOptions} CssAutoGeneratorOptions */
21/** @typedef {import("../../declarations/WebpackOptions").CssGlobalGeneratorOptions} CssGlobalGeneratorOptions */
22/** @typedef {import("../../declarations/WebpackOptions").CssModuleGeneratorOptions} CssModuleGeneratorOptions */
23/** @typedef {import("../CodeGenerationResults")} CodeGenerationResults */
24/** @typedef {import("../Dependency")} Dependency */
25/** @typedef {import("../DependencyTemplate").CssData} CssData */
26/** @typedef {import("../DependencyTemplate").CssDependencyTemplateContext} DependencyTemplateContext */
27/** @typedef {import("../Generator").GenerateContext} GenerateContext */
28/** @typedef {import("../Generator").UpdateHashContext} UpdateHashContext */
29/** @typedef {import("../Module").ConcatenationBailoutReasonContext} ConcatenationBailoutReasonContext */
30/** @typedef {import("../Module").SourceTypes} SourceTypes */
31/** @typedef {import("../NormalModule")} NormalModule */
32/** @typedef {import("../util/Hash")} Hash */
33
34class CssGenerator extends Generator {
35 /**
36 * @param {CssAutoGeneratorOptions | CssGlobalGeneratorOptions | CssModuleGeneratorOptions} options options
37 */
38 constructor(options) {
39 super();
40 this.convention = options.exportsConvention;
41 this.localIdentName = options.localIdentName;
42 this.exportsOnly = options.exportsOnly;
43 this.esModule = options.esModule;
44 }
45
46 /**
47 * @param {NormalModule} module module for which the bailout reason should be determined
48 * @param {ConcatenationBailoutReasonContext} context context
49 * @returns {string | undefined} reason why this module can't be concatenated, undefined when it can be concatenated
50 */
51 getConcatenationBailoutReason(module, context) {
52 if (!this.esModule) {
53 return "Module is not an ECMAScript module";
54 }
55
56 return undefined;
57 }
58
59 /**
60 * @param {NormalModule} module module for which the code should be generated
61 * @param {GenerateContext} generateContext context for generate
62 * @returns {Source | null} generated code
63 */
64 generate(module, generateContext) {
65 const source =
66 generateContext.type === "javascript"
67 ? new ReplaceSource(new RawSource(""))
68 : new ReplaceSource(/** @type {Source} */ (module.originalSource()));
69
70 /** @type {InitFragment<GenerateContext>[]} */
71 const initFragments = [];
72 /** @type {CssData} */
73 const cssData = {
74 esModule: this.esModule,
75 exports: new Map()
76 };
77
78 /** @type {InitFragment<GenerateContext>[] | undefined} */
79 let chunkInitFragments;
80 /** @type {DependencyTemplateContext} */
81 const templateContext = {
82 runtimeTemplate: generateContext.runtimeTemplate,
83 dependencyTemplates: generateContext.dependencyTemplates,
84 moduleGraph: generateContext.moduleGraph,
85 chunkGraph: generateContext.chunkGraph,
86 module,
87 runtime: generateContext.runtime,
88 runtimeRequirements: generateContext.runtimeRequirements,
89 concatenationScope: generateContext.concatenationScope,
90 codeGenerationResults:
91 /** @type {CodeGenerationResults} */
92 (generateContext.codeGenerationResults),
93 initFragments,
94 cssData,
95 get chunkInitFragments() {
96 if (!chunkInitFragments) {
97 const data =
98 /** @type {NonNullable<GenerateContext["getData"]>} */
99 (generateContext.getData)();
100 chunkInitFragments = data.get("chunkInitFragments");
101 if (!chunkInitFragments) {
102 chunkInitFragments = [];
103 data.set("chunkInitFragments", chunkInitFragments);
104 }
105 }
106
107 return chunkInitFragments;
108 }
109 };
110
111 /**
112 * @param {Dependency} dependency dependency
113 */
114 const handleDependency = dependency => {
115 const constructor =
116 /** @type {new (...args: EXPECTED_ANY[]) => Dependency} */
117 (dependency.constructor);
118 const template = generateContext.dependencyTemplates.get(constructor);
119 if (!template) {
120 throw new Error(
121 `No template for dependency: ${dependency.constructor.name}`
122 );
123 }
124
125 template.apply(dependency, source, templateContext);
126 };
127
128 for (const dependency of module.dependencies) {
129 handleDependency(dependency);
130 }
131
132 switch (generateContext.type) {
133 case "javascript": {
134 module.buildInfo.cssData = cssData;
135
136 generateContext.runtimeRequirements.add(RuntimeGlobals.module);
137
138 if (generateContext.concatenationScope) {
139 const source = new ConcatSource();
140 const usedIdentifiers = new Set();
141 for (const [name, v] of cssData.exports) {
142 const usedName = generateContext.moduleGraph
143 .getExportInfo(module, name)
144 .getUsedName(name, generateContext.runtime);
145 if (!usedName) {
146 continue;
147 }
148 let identifier = Template.toIdentifier(usedName);
149 const { RESERVED_IDENTIFIER } = require("../util/propertyName");
150 if (RESERVED_IDENTIFIER.has(identifier)) {
151 identifier = `_${identifier}`;
152 }
153 const i = 0;
154 while (usedIdentifiers.has(identifier)) {
155 identifier = Template.toIdentifier(name + i);
156 }
157 usedIdentifiers.add(identifier);
158 generateContext.concatenationScope.registerExport(name, identifier);
159 source.add(
160 `${
161 generateContext.runtimeTemplate.supportsConst()
162 ? "const"
163 : "var"
164 } ${identifier} = ${JSON.stringify(v)};\n`
165 );
166 }
167 return source;
168 }
169
170 const needNsObj =
171 this.esModule &&
172 generateContext.moduleGraph
173 .getExportsInfo(module)
174 .otherExportsInfo.getUsed(generateContext.runtime) !==
175 UsageState.Unused;
176
177 if (needNsObj) {
178 generateContext.runtimeRequirements.add(
179 RuntimeGlobals.makeNamespaceObject
180 );
181 }
182
183 const exports = [];
184
185 for (const [name, v] of cssData.exports) {
186 exports.push(`\t${JSON.stringify(name)}: ${JSON.stringify(v)}`);
187 }
188
189 return new RawSource(
190 `${needNsObj ? `${RuntimeGlobals.makeNamespaceObject}(` : ""}${
191 module.moduleArgument
192 }.exports = {\n${exports.join(",\n")}\n}${needNsObj ? ")" : ""};`
193 );
194 }
195 case "css": {
196 if (module.presentationalDependencies !== undefined) {
197 for (const dependency of module.presentationalDependencies) {
198 handleDependency(dependency);
199 }
200 }
201
202 generateContext.runtimeRequirements.add(RuntimeGlobals.hasCssModules);
203
204 return InitFragment.addToSource(source, initFragments, generateContext);
205 }
206 }
207 }
208
209 /**
210 * @param {NormalModule} module fresh module
211 * @returns {SourceTypes} available types (do not mutate)
212 */
213 getTypes(module) {
214 // TODO, find a better way to prevent the original module from being removed after concatenation, maybe it is a bug
215 return this.exportsOnly ? JS_AND_CSS_EXPORT_TYPES : JS_AND_CSS_TYPES;
216 }
217
218 /**
219 * @param {NormalModule} module the module
220 * @param {string=} type source type
221 * @returns {number} estimate size of the module
222 */
223 getSize(module, type) {
224 switch (type) {
225 case "javascript": {
226 if (!module.buildInfo.cssData) {
227 return 42;
228 }
229
230 const exports = module.buildInfo.cssData.exports;
231 const stringifiedExports = JSON.stringify(
232 Array.from(exports).reduce((obj, [key, value]) => {
233 obj[key] = value;
234 return obj;
235 }, {})
236 );
237
238 return stringifiedExports.length + 42;
239 }
240 case "css": {
241 const originalSource = module.originalSource();
242
243 if (!originalSource) {
244 return 0;
245 }
246
247 return originalSource.size();
248 }
249 }
250 }
251
252 /**
253 * @param {Hash} hash hash that will be modified
254 * @param {UpdateHashContext} updateHashContext context for updating hash
255 */
256 updateHash(hash, { module }) {
257 hash.update(this.esModule.toString());
258 }
259}
260
261module.exports = CssGenerator;
Note: See TracBrowser for help on using the repository browser.