| 
									
										
										
										
											2021-11-30 19:55:51 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Sergey Melyukov @smelukov | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-30 20:46:42 +08:00
										 |  |  | const { ReplaceSource } = require("webpack-sources"); | 
					
						
							| 
									
										
										
										
											2021-11-30 19:55:51 +08:00
										 |  |  | const Generator = require("../Generator"); | 
					
						
							| 
									
										
										
										
											2021-11-30 20:46:42 +08:00
										 |  |  | const InitFragment = require("../InitFragment"); | 
					
						
							| 
									
										
										
										
											2022-01-13 08:02:15 +08:00
										 |  |  | const RuntimeGlobals = require("../RuntimeGlobals"); | 
					
						
							| 
									
										
										
										
											2024-02-21 16:00:24 +08:00
										 |  |  | const { cssExportConvention } = require("../util/conventions"); | 
					
						
							| 
									
										
										
										
											2021-11-30 19:55:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** @typedef {import("webpack-sources").Source} Source */ | 
					
						
							| 
									
										
										
										
											2024-02-21 16:00:24 +08:00
										 |  |  | /** @typedef {import("../../declarations/WebpackOptions").CssGeneratorExportsConvention} CssGeneratorExportsConvention */ | 
					
						
							| 
									
										
										
										
											2024-02-22 19:58:59 +08:00
										 |  |  | /** @typedef {import("../../declarations/WebpackOptions").CssGeneratorLocalIdentName} CssGeneratorLocalIdentName */ | 
					
						
							| 
									
										
										
										
											2021-11-30 20:46:42 +08:00
										 |  |  | /** @typedef {import("../Dependency")} Dependency */ | 
					
						
							| 
									
										
										
										
											2021-11-30 19:55:51 +08:00
										 |  |  | /** @typedef {import("../Generator").GenerateContext} GenerateContext */ | 
					
						
							|  |  |  | /** @typedef {import("../Generator").UpdateHashContext} UpdateHashContext */ | 
					
						
							|  |  |  | /** @typedef {import("../NormalModule")} NormalModule */ | 
					
						
							|  |  |  | /** @typedef {import("../util/Hash")} Hash */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const TYPES = new Set(["css"]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class CssGenerator extends Generator { | 
					
						
							| 
									
										
										
										
											2024-02-21 16:00:24 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {CssGeneratorExportsConvention} convention the convention of the exports name | 
					
						
							| 
									
										
										
										
											2024-02-22 19:58:59 +08:00
										 |  |  | 	 * @param {CssGeneratorLocalIdentName | undefined} localIdentName css export local ident name | 
					
						
							| 
									
										
										
										
											2024-02-21 16:00:24 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2024-02-22 19:58:59 +08:00
										 |  |  | 	constructor(convention, localIdentName) { | 
					
						
							| 
									
										
										
										
											2021-11-30 19:55:51 +08:00
										 |  |  | 		super(); | 
					
						
							| 
									
										
										
										
											2024-02-22 19:58:59 +08:00
										 |  |  | 		/** @type {CssGeneratorExportsConvention} */ | 
					
						
							| 
									
										
										
										
											2024-02-21 16:00:24 +08:00
										 |  |  | 		this.convention = convention; | 
					
						
							| 
									
										
										
										
											2024-02-22 19:58:59 +08:00
										 |  |  | 		/** @type {CssGeneratorLocalIdentName | undefined} */ | 
					
						
							|  |  |  | 		this.localIdentName = localIdentName; | 
					
						
							| 
									
										
										
										
											2021-11-30 19:55:51 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {NormalModule} module module for which the code should be generated | 
					
						
							|  |  |  | 	 * @param {GenerateContext} generateContext context for generate | 
					
						
							|  |  |  | 	 * @returns {Source} generated code | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2021-11-30 20:46:42 +08:00
										 |  |  | 	generate(module, generateContext) { | 
					
						
							| 
									
										
										
										
											2023-06-04 01:52:25 +08:00
										 |  |  | 		const originalSource = /** @type {Source} */ (module.originalSource()); | 
					
						
							| 
									
										
										
										
											2021-11-30 20:46:42 +08:00
										 |  |  | 		const source = new ReplaceSource(originalSource); | 
					
						
							| 
									
										
										
										
											2023-04-29 02:50:41 +08:00
										 |  |  | 		/** @type {InitFragment[]} */ | 
					
						
							| 
									
										
										
										
											2021-11-30 20:46:42 +08:00
										 |  |  | 		const initFragments = []; | 
					
						
							| 
									
										
										
										
											2024-02-22 19:58:59 +08:00
										 |  |  | 		/** @type {Map<string, string>} */ | 
					
						
							| 
									
										
										
										
											2021-12-14 23:02:26 +08:00
										 |  |  | 		const cssExports = new Map(); | 
					
						
							| 
									
										
										
										
											2021-11-30 20:46:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-13 08:02:15 +08:00
										 |  |  | 		generateContext.runtimeRequirements.add(RuntimeGlobals.hasCssModules); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-05 14:26:24 +08:00
										 |  |  | 		let chunkInitFragments; | 
					
						
							| 
									
										
										
										
											2021-12-14 23:02:26 +08:00
										 |  |  | 		const templateContext = { | 
					
						
							|  |  |  | 			runtimeTemplate: generateContext.runtimeTemplate, | 
					
						
							|  |  |  | 			dependencyTemplates: generateContext.dependencyTemplates, | 
					
						
							|  |  |  | 			moduleGraph: generateContext.moduleGraph, | 
					
						
							|  |  |  | 			chunkGraph: generateContext.chunkGraph, | 
					
						
							|  |  |  | 			module, | 
					
						
							|  |  |  | 			runtime: generateContext.runtime, | 
					
						
							|  |  |  | 			runtimeRequirements: generateContext.runtimeRequirements, | 
					
						
							|  |  |  | 			concatenationScope: generateContext.concatenationScope, | 
					
						
							|  |  |  | 			codeGenerationResults: generateContext.codeGenerationResults, | 
					
						
							|  |  |  | 			initFragments, | 
					
						
							| 
									
										
										
										
											2022-04-05 14:26:24 +08:00
										 |  |  | 			cssExports, | 
					
						
							|  |  |  | 			get chunkInitFragments() { | 
					
						
							|  |  |  | 				if (!chunkInitFragments) { | 
					
						
							|  |  |  | 					const data = generateContext.getData(); | 
					
						
							|  |  |  | 					chunkInitFragments = data.get("chunkInitFragments"); | 
					
						
							|  |  |  | 					if (!chunkInitFragments) { | 
					
						
							|  |  |  | 						chunkInitFragments = []; | 
					
						
							|  |  |  | 						data.set("chunkInitFragments", chunkInitFragments); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				return chunkInitFragments; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-12-14 23:02:26 +08:00
										 |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-29 02:50:41 +08:00
										 |  |  | 		/** | 
					
						
							|  |  |  | 		 * @param {Dependency} dependency dependency | 
					
						
							|  |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2021-12-14 23:02:26 +08:00
										 |  |  | 		const handleDependency = dependency => { | 
					
						
							| 
									
										
										
										
											2021-11-30 20:46:42 +08:00
										 |  |  | 			const constructor = /** @type {new (...args: any[]) => Dependency} */ ( | 
					
						
							|  |  |  | 				dependency.constructor | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 			const template = generateContext.dependencyTemplates.get(constructor); | 
					
						
							|  |  |  | 			if (!template) { | 
					
						
							|  |  |  | 				throw new Error( | 
					
						
							|  |  |  | 					"No template for dependency: " + dependency.constructor.name | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			template.apply(dependency, source, templateContext); | 
					
						
							| 
									
										
										
										
											2021-12-14 23:02:26 +08:00
										 |  |  | 		}; | 
					
						
							|  |  |  | 		module.dependencies.forEach(handleDependency); | 
					
						
							|  |  |  | 		if (module.presentationalDependencies !== undefined) | 
					
						
							|  |  |  | 			module.presentationalDependencies.forEach(handleDependency); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (cssExports.size > 0) { | 
					
						
							| 
									
										
										
										
											2024-02-21 16:00:24 +08:00
										 |  |  | 			const newCssExports = new Map(); | 
					
						
							|  |  |  | 			for (let [name, v] of cssExports) { | 
					
						
							|  |  |  | 				for (let newName of cssExportConvention(name, this.convention)) { | 
					
						
							|  |  |  | 					newCssExports.set(newName, v); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-12-14 23:02:26 +08:00
										 |  |  | 			const data = generateContext.getData(); | 
					
						
							| 
									
										
										
										
											2024-02-21 16:00:24 +08:00
										 |  |  | 			data.set("css-exports", newCssExports); | 
					
						
							| 
									
										
										
										
											2021-11-30 20:46:42 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-12-14 23:02:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-01 16:50:13 +08:00
										 |  |  | 		return InitFragment.addToSource(source, initFragments, generateContext); | 
					
						
							| 
									
										
										
										
											2021-11-30 19:55:51 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {NormalModule} module fresh module | 
					
						
							|  |  |  | 	 * @returns {Set<string>} available types (do not mutate) | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	getTypes(module) { | 
					
						
							|  |  |  | 		return TYPES; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {NormalModule} module the module | 
					
						
							|  |  |  | 	 * @param {string=} type source type | 
					
						
							|  |  |  | 	 * @returns {number} estimate size of the module | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	getSize(module, type) { | 
					
						
							|  |  |  | 		const originalSource = module.originalSource(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (!originalSource) { | 
					
						
							|  |  |  | 			return 0; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return originalSource.size(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {Hash} hash hash that will be modified | 
					
						
							|  |  |  | 	 * @param {UpdateHashContext} updateHashContext context for updating hash | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	updateHash(hash, { module }) {} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = CssGenerator; |