| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2017-01-09 02:07:44 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | const ConstDependency = require("./dependencies/ConstDependency"); | 
					
						
							|  |  |  | const NullFactory = require("./NullFactory"); | 
					
						
							| 
									
										
										
										
											2017-01-09 17:30:12 +08:00
										 |  |  | const ParserHelpers = require("./ParserHelpers"); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-09 02:07:44 +08:00
										 |  |  | const getQuery = (request) => { | 
					
						
							|  |  |  | 	const i = request.indexOf("?"); | 
					
						
							|  |  |  | 	return request.indexOf("?") < 0 ? "" : request.substr(i); | 
					
						
							| 
									
										
										
										
											2017-01-11 17:51:58 +08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2015-12-30 23:39:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-09 02:07:44 +08:00
										 |  |  | class ConstPlugin { | 
					
						
							|  |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2017-12-06 22:01:25 +08:00
										 |  |  | 		compiler.hooks.compilation.tap("ConstPlugin", (compilation, { | 
					
						
							|  |  |  | 			normalModuleFactory | 
					
						
							|  |  |  | 		}) => { | 
					
						
							| 
									
										
										
										
											2017-01-09 02:07:44 +08:00
										 |  |  | 			compilation.dependencyFactories.set(ConstDependency, new NullFactory()); | 
					
						
							|  |  |  | 			compilation.dependencyTemplates.set(ConstDependency, new ConstDependency.Template()); | 
					
						
							| 
									
										
										
										
											2016-09-14 18:04:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-14 17:22:27 +08:00
										 |  |  | 			const handler = parser => { | 
					
						
							| 
									
										
										
										
											2017-12-19 21:35:30 +08:00
										 |  |  | 				parser.hooks.statementIf.tap("ConstPlugin", statement => { | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | 					const param = parser.evaluateExpression(statement.test); | 
					
						
							| 
									
										
										
										
											2017-01-09 02:07:44 +08:00
										 |  |  | 					const bool = param.asBool(); | 
					
						
							|  |  |  | 					if(typeof bool === "boolean") { | 
					
						
							|  |  |  | 						if(statement.test.type !== "Literal") { | 
					
						
							|  |  |  | 							const dep = new ConstDependency(`${bool}`, param.range); | 
					
						
							|  |  |  | 							dep.loc = statement.loc; | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | 							parser.state.current.addDependency(dep); | 
					
						
							| 
									
										
										
										
											2017-01-09 02:07:44 +08:00
										 |  |  | 						} | 
					
						
							|  |  |  | 						return bool; | 
					
						
							| 
									
										
										
										
											2016-09-14 18:04:42 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2017-01-09 02:07:44 +08:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2017-12-19 21:35:30 +08:00
										 |  |  | 				parser.hooks.expressionConditionalOperator.tap("ConstPlugin", expression => { | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | 					const param = parser.evaluateExpression(expression.test); | 
					
						
							| 
									
										
										
										
											2017-01-09 02:07:44 +08:00
										 |  |  | 					const bool = param.asBool(); | 
					
						
							|  |  |  | 					if(typeof bool === "boolean") { | 
					
						
							|  |  |  | 						if(expression.test.type !== "Literal") { | 
					
						
							|  |  |  | 							const dep = new ConstDependency(` ${bool}`, param.range); | 
					
						
							|  |  |  | 							dep.loc = expression.loc; | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | 							parser.state.current.addDependency(dep); | 
					
						
							| 
									
										
										
										
											2017-01-09 02:07:44 +08:00
										 |  |  | 						} | 
					
						
							|  |  |  | 						return bool; | 
					
						
							| 
									
										
										
										
											2016-09-14 18:04:42 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2017-01-09 02:07:44 +08:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2017-12-19 21:35:30 +08:00
										 |  |  | 				parser.hooks.evaluateIdentifier.for("__resourceQuery").tap("ConstPlugin", expr => { | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | 					if(!parser.state.module) return; | 
					
						
							|  |  |  | 					return ParserHelpers.evaluateToString(getQuery(parser.state.module.resource))(expr); | 
					
						
							| 
									
										
										
										
											2017-01-09 02:07:44 +08:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2017-12-19 21:35:30 +08:00
										 |  |  | 				parser.hooks.expression.for("__resourceQuery").tap("ConstPlugin", () => { | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | 					if(!parser.state.module) return; | 
					
						
							|  |  |  | 					parser.state.current.addVariable("__resourceQuery", JSON.stringify(getQuery(parser.state.module.resource))); | 
					
						
							| 
									
										
										
										
											2017-01-09 02:07:44 +08:00
										 |  |  | 					return true; | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2017-12-14 17:22:27 +08:00
										 |  |  | 			}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			normalModuleFactory.hooks.parser.for("javascript/auto").tap("ConstPlugin", handler); | 
					
						
							|  |  |  | 			normalModuleFactory.hooks.parser.for("javascript/dynamic").tap("ConstPlugin", handler); | 
					
						
							|  |  |  | 			normalModuleFactory.hooks.parser.for("javascript/esm").tap("ConstPlugin", handler); | 
					
						
							| 
									
										
										
										
											2016-09-14 18:04:42 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-01-09 02:07:44 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = ConstPlugin; |