| 
									
										
										
										
											2020-10-26 21:00:11 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const { ConcatSource, PrefixSource } = require("webpack-sources"); | 
					
						
							|  |  |  | const InitFragment = require("./InitFragment"); | 
					
						
							|  |  |  | const Template = require("./Template"); | 
					
						
							|  |  |  | const { mergeRuntime } = require("./util/runtime"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** @typedef {import("webpack-sources").Source} Source */ | 
					
						
							|  |  |  | /** @typedef {import("./Generator").GenerateContext} GenerateContext */ | 
					
						
							|  |  |  | /** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-13 01:24:59 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @param {string} condition condition | 
					
						
							|  |  |  |  * @param {string | Source} source source | 
					
						
							|  |  |  |  * @returns {string | Source} wrapped source | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-10-26 21:00:11 +08:00
										 |  |  | const wrapInCondition = (condition, source) => { | 
					
						
							|  |  |  | 	if (typeof source === "string") { | 
					
						
							|  |  |  | 		return Template.asString([ | 
					
						
							|  |  |  | 			`if (${condition}) {`, | 
					
						
							|  |  |  | 			Template.indent(source), | 
					
						
							|  |  |  | 			"}", | 
					
						
							|  |  |  | 			"" | 
					
						
							|  |  |  | 		]); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-07-31 04:21:27 +08:00
										 |  |  | 	return new ConcatSource( | 
					
						
							|  |  |  | 		`if (${condition}) {\n`, | 
					
						
							|  |  |  | 		new PrefixSource("\t", source), | 
					
						
							|  |  |  | 		"}\n" | 
					
						
							|  |  |  | 	); | 
					
						
							| 
									
										
										
										
											2020-10-26 21:00:11 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-24 22:44:14 +08:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2024-03-15 22:24:33 +08:00
										 |  |  |  * @extends {InitFragment<GenerateContext>} | 
					
						
							| 
									
										
										
										
											2021-06-24 22:44:14 +08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-10-26 21:00:11 +08:00
										 |  |  | class ConditionalInitFragment extends InitFragment { | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2024-03-18 23:28:40 +08:00
										 |  |  | 	 * @param {string | Source | undefined} content the source code that will be included as initialization code | 
					
						
							| 
									
										
										
										
											2020-10-26 21:00:11 +08:00
										 |  |  | 	 * @param {number} stage category of initialization code (contribute to order) | 
					
						
							|  |  |  | 	 * @param {number} position position in the category (contribute to order) | 
					
						
							| 
									
										
										
										
											2023-06-13 01:24:59 +08:00
										 |  |  | 	 * @param {string | undefined} key unique key to avoid emitting the same initialization code twice | 
					
						
							| 
									
										
										
										
											2020-10-26 21:00:11 +08:00
										 |  |  | 	 * @param {RuntimeSpec | boolean} runtimeCondition in which runtime this fragment should be executed | 
					
						
							| 
									
										
										
										
											2024-03-18 23:28:40 +08:00
										 |  |  | 	 * @param {string | Source=} endContent the source code that will be included at the end of the module | 
					
						
							| 
									
										
										
										
											2020-10-26 21:00:11 +08:00
										 |  |  | 	 */ | 
					
						
							|  |  |  | 	constructor( | 
					
						
							|  |  |  | 		content, | 
					
						
							|  |  |  | 		stage, | 
					
						
							|  |  |  | 		position, | 
					
						
							|  |  |  | 		key, | 
					
						
							|  |  |  | 		runtimeCondition = true, | 
					
						
							| 
									
										
										
										
											2024-07-31 09:37:24 +08:00
										 |  |  | 		endContent = undefined | 
					
						
							| 
									
										
										
										
											2020-10-26 21:00:11 +08:00
										 |  |  | 	) { | 
					
						
							|  |  |  | 		super(content, stage, position, key, endContent); | 
					
						
							|  |  |  | 		this.runtimeCondition = runtimeCondition; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2024-01-27 00:17:45 +08:00
										 |  |  | 	 * @param {GenerateContext} context context | 
					
						
							| 
									
										
										
										
											2024-03-18 23:28:40 +08:00
										 |  |  | 	 * @returns {string | Source | undefined} the source code that will be included as initialization code | 
					
						
							| 
									
										
										
										
											2020-10-26 21:00:11 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2021-06-24 22:44:14 +08:00
										 |  |  | 	getContent(context) { | 
					
						
							| 
									
										
										
										
											2020-10-26 21:00:11 +08:00
										 |  |  | 		if (this.runtimeCondition === false || !this.content) return ""; | 
					
						
							|  |  |  | 		if (this.runtimeCondition === true) return this.content; | 
					
						
							| 
									
										
										
										
											2021-06-24 22:44:14 +08:00
										 |  |  | 		const expr = context.runtimeTemplate.runtimeConditionExpression({ | 
					
						
							|  |  |  | 			chunkGraph: context.chunkGraph, | 
					
						
							|  |  |  | 			runtimeRequirements: context.runtimeRequirements, | 
					
						
							|  |  |  | 			runtime: context.runtime, | 
					
						
							| 
									
										
										
										
											2020-10-26 21:00:11 +08:00
										 |  |  | 			runtimeCondition: this.runtimeCondition | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 		if (expr === "true") return this.content; | 
					
						
							|  |  |  | 		return wrapInCondition(expr, this.content); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2024-01-27 00:17:45 +08:00
										 |  |  | 	 * @param {GenerateContext} context context | 
					
						
							| 
									
										
										
										
											2020-10-26 21:00:11 +08:00
										 |  |  | 	 * @returns {string|Source=} the source code that will be included at the end of the module | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2021-06-24 22:44:14 +08:00
										 |  |  | 	getEndContent(context) { | 
					
						
							| 
									
										
										
										
											2020-10-26 21:00:11 +08:00
										 |  |  | 		if (this.runtimeCondition === false || !this.endContent) return ""; | 
					
						
							|  |  |  | 		if (this.runtimeCondition === true) return this.endContent; | 
					
						
							| 
									
										
										
										
											2021-06-24 22:44:14 +08:00
										 |  |  | 		const expr = context.runtimeTemplate.runtimeConditionExpression({ | 
					
						
							|  |  |  | 			chunkGraph: context.chunkGraph, | 
					
						
							|  |  |  | 			runtimeRequirements: context.runtimeRequirements, | 
					
						
							|  |  |  | 			runtime: context.runtime, | 
					
						
							| 
									
										
										
										
											2020-10-26 21:00:11 +08:00
										 |  |  | 			runtimeCondition: this.runtimeCondition | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 		if (expr === "true") return this.endContent; | 
					
						
							|  |  |  | 		return wrapInCondition(expr, this.endContent); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-13 01:24:59 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {ConditionalInitFragment} other fragment to merge with | 
					
						
							|  |  |  | 	 * @returns {ConditionalInitFragment} merged fragment | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2020-10-26 21:00:11 +08:00
										 |  |  | 	merge(other) { | 
					
						
							|  |  |  | 		if (this.runtimeCondition === true) return this; | 
					
						
							|  |  |  | 		if (other.runtimeCondition === true) return other; | 
					
						
							|  |  |  | 		if (this.runtimeCondition === false) return other; | 
					
						
							|  |  |  | 		if (other.runtimeCondition === false) return this; | 
					
						
							|  |  |  | 		const runtimeCondition = mergeRuntime( | 
					
						
							|  |  |  | 			this.runtimeCondition, | 
					
						
							|  |  |  | 			other.runtimeCondition | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 		return new ConditionalInitFragment( | 
					
						
							|  |  |  | 			this.content, | 
					
						
							|  |  |  | 			this.stage, | 
					
						
							|  |  |  | 			this.position, | 
					
						
							|  |  |  | 			this.key, | 
					
						
							|  |  |  | 			runtimeCondition, | 
					
						
							|  |  |  | 			this.endContent | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = ConditionalInitFragment; |