| 
									
										
										
										
											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"); | 
					
						
							| 
									
										
										
										
											2021-11-30 19:55:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** @typedef {import("webpack-sources").Source} Source */ | 
					
						
							| 
									
										
										
										
											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 { | 
					
						
							|  |  |  | 	constructor() { | 
					
						
							|  |  |  | 		super(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @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) { | 
					
						
							|  |  |  | 		const originalSource = module.originalSource(); | 
					
						
							|  |  |  | 		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 = []; | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							|  |  |  | 			cssExports | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							|  |  |  | 			const data = generateContext.getData(); | 
					
						
							|  |  |  | 			data.set("css-exports", cssExports); | 
					
						
							| 
									
										
										
										
											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; |