| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-09 02:07:44 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2018-07-03 16:24:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-23 20:53:50 +08:00
										 |  |  | const CachedConstDependency = require("./dependencies/CachedConstDependency"); | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | const ConstDependency = require("./dependencies/ConstDependency"); | 
					
						
							| 
									
										
										
										
											2019-10-22 15:27:52 +08:00
										 |  |  | const { evaluateToString } = require("./javascript/JavascriptParserHelpers"); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-09 05:59:19 +08:00
										 |  |  | /** @typedef {import("./Compiler")} Compiler */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | const getQuery = request => { | 
					
						
							| 
									
										
										
										
											2017-01-09 02:07:44 +08:00
										 |  |  | 	const i = request.indexOf("?"); | 
					
						
							| 
									
										
										
										
											2018-02-01 07:59:08 +08:00
										 |  |  | 	return i !== -1 ? request.substr(i) : ""; | 
					
						
							| 
									
										
										
										
											2017-01-11 17:51:58 +08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2015-12-30 23:39:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-09 17:35:08 +08:00
										 |  |  | const collectDeclaration = (declarations, pattern) => { | 
					
						
							|  |  |  | 	const stack = [pattern]; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 	while (stack.length > 0) { | 
					
						
							| 
									
										
										
										
											2018-01-09 17:35:08 +08:00
										 |  |  | 		const node = stack.pop(); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		switch (node.type) { | 
					
						
							| 
									
										
										
										
											2018-01-09 17:35:08 +08:00
										 |  |  | 			case "Identifier": | 
					
						
							|  |  |  | 				declarations.add(node.name); | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case "ArrayPattern": | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 				for (const element of node.elements) { | 
					
						
							|  |  |  | 					if (element) { | 
					
						
							|  |  |  | 						stack.push(element); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-01-09 17:35:08 +08:00
										 |  |  | 				break; | 
					
						
							|  |  |  | 			case "AssignmentPattern": | 
					
						
							|  |  |  | 				stack.push(node.left); | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case "ObjectPattern": | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 				for (const property of node.properties) { | 
					
						
							|  |  |  | 					stack.push(property.value); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-01-09 17:35:08 +08:00
										 |  |  | 				break; | 
					
						
							|  |  |  | 			case "RestElement": | 
					
						
							|  |  |  | 				stack.push(node.argument); | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const getHoistedDeclarations = (branch, includeFunctionDeclarations) => { | 
					
						
							|  |  |  | 	const declarations = new Set(); | 
					
						
							|  |  |  | 	const stack = [branch]; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 	while (stack.length > 0) { | 
					
						
							| 
									
										
										
										
											2018-01-09 17:35:08 +08:00
										 |  |  | 		const node = stack.pop(); | 
					
						
							|  |  |  | 		// Some node could be `null` or `undefined`.
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		if (!node) continue; | 
					
						
							|  |  |  | 		switch (node.type) { | 
					
						
							| 
									
										
										
										
											2018-01-09 17:35:08 +08:00
										 |  |  | 			// Walk through control statements to look for hoisted declarations.
 | 
					
						
							|  |  |  | 			// Some branches are skipped since they do not allow declarations.
 | 
					
						
							|  |  |  | 			case "BlockStatement": | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 				for (const stmt of node.body) { | 
					
						
							|  |  |  | 					stack.push(stmt); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-01-09 17:35:08 +08:00
										 |  |  | 				break; | 
					
						
							|  |  |  | 			case "IfStatement": | 
					
						
							|  |  |  | 				stack.push(node.consequent); | 
					
						
							|  |  |  | 				stack.push(node.alternate); | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case "ForStatement": | 
					
						
							|  |  |  | 				stack.push(node.init); | 
					
						
							|  |  |  | 				stack.push(node.body); | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case "ForInStatement": | 
					
						
							|  |  |  | 			case "ForOfStatement": | 
					
						
							|  |  |  | 				stack.push(node.left); | 
					
						
							|  |  |  | 				stack.push(node.body); | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case "DoWhileStatement": | 
					
						
							|  |  |  | 			case "WhileStatement": | 
					
						
							|  |  |  | 			case "LabeledStatement": | 
					
						
							|  |  |  | 				stack.push(node.body); | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case "SwitchStatement": | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 				for (const cs of node.cases) { | 
					
						
							|  |  |  | 					for (const consequent of cs.consequent) { | 
					
						
							|  |  |  | 						stack.push(consequent); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-01-09 17:35:08 +08:00
										 |  |  | 				break; | 
					
						
							|  |  |  | 			case "TryStatement": | 
					
						
							|  |  |  | 				stack.push(node.block); | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 				if (node.handler) { | 
					
						
							|  |  |  | 					stack.push(node.handler.body); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-01-09 17:35:08 +08:00
										 |  |  | 				stack.push(node.finalizer); | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case "FunctionDeclaration": | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 				if (includeFunctionDeclarations) { | 
					
						
							| 
									
										
										
										
											2018-01-09 17:35:08 +08:00
										 |  |  | 					collectDeclaration(declarations, node.id); | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-01-09 17:35:08 +08:00
										 |  |  | 				break; | 
					
						
							|  |  |  | 			case "VariableDeclaration": | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 				if (node.kind === "var") { | 
					
						
							|  |  |  | 					for (const decl of node.declarations) { | 
					
						
							| 
									
										
										
										
											2018-01-09 17:35:08 +08:00
										 |  |  | 						collectDeclaration(declarations, decl.id); | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-01-09 17:35:08 +08:00
										 |  |  | 				break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return Array.from(declarations); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-09 02:07:44 +08:00
										 |  |  | class ConstPlugin { | 
					
						
							| 
									
										
										
										
											2018-11-09 05:59:19 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-01-09 02:07:44 +08:00
										 |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		compiler.hooks.compilation.tap( | 
					
						
							|  |  |  | 			"ConstPlugin", | 
					
						
							|  |  |  | 			(compilation, { normalModuleFactory }) => { | 
					
						
							|  |  |  | 				compilation.dependencyTemplates.set( | 
					
						
							|  |  |  | 					ConstDependency, | 
					
						
							|  |  |  | 					new ConstDependency.Template() | 
					
						
							|  |  |  | 				); | 
					
						
							| 
									
										
										
										
											2016-09-14 18:04:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-23 20:53:50 +08:00
										 |  |  | 				compilation.dependencyTemplates.set( | 
					
						
							|  |  |  | 					CachedConstDependency, | 
					
						
							|  |  |  | 					new CachedConstDependency.Template() | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				const handler = parser => { | 
					
						
							|  |  |  | 					parser.hooks.statementIf.tap("ConstPlugin", statement => { | 
					
						
							|  |  |  | 						const param = parser.evaluateExpression(statement.test); | 
					
						
							|  |  |  | 						const bool = param.asBool(); | 
					
						
							|  |  |  | 						if (typeof bool === "boolean") { | 
					
						
							|  |  |  | 							if (statement.test.type !== "Literal") { | 
					
						
							|  |  |  | 								const dep = new ConstDependency(`${bool}`, param.range); | 
					
						
							|  |  |  | 								dep.loc = statement.loc; | 
					
						
							| 
									
										
										
										
											2019-10-30 13:40:40 +08:00
										 |  |  | 								parser.state.module.addPresentationalDependency(dep); | 
					
						
							| 
									
										
										
										
											2018-01-09 17:35:08 +08:00
										 |  |  | 							} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 							const branchToRemove = bool | 
					
						
							|  |  |  | 								? statement.alternate | 
					
						
							|  |  |  | 								: statement.consequent; | 
					
						
							|  |  |  | 							if (branchToRemove) { | 
					
						
							|  |  |  | 								// Before removing the dead branch, the hoisted declarations
 | 
					
						
							|  |  |  | 								// must be collected.
 | 
					
						
							|  |  |  | 								//
 | 
					
						
							|  |  |  | 								// Given the following code:
 | 
					
						
							|  |  |  | 								//
 | 
					
						
							|  |  |  | 								//     if (true) f() else g()
 | 
					
						
							|  |  |  | 								//     if (false) {
 | 
					
						
							|  |  |  | 								//       function f() {}
 | 
					
						
							|  |  |  | 								//       const g = function g() {}
 | 
					
						
							|  |  |  | 								//       if (someTest) {
 | 
					
						
							|  |  |  | 								//         let a = 1
 | 
					
						
							|  |  |  | 								//         var x, {y, z} = obj
 | 
					
						
							|  |  |  | 								//       }
 | 
					
						
							|  |  |  | 								//     } else {
 | 
					
						
							|  |  |  | 								//       …
 | 
					
						
							|  |  |  | 								//     }
 | 
					
						
							|  |  |  | 								//
 | 
					
						
							|  |  |  | 								// the generated code is:
 | 
					
						
							|  |  |  | 								//
 | 
					
						
							|  |  |  | 								//     if (true) f() else {}
 | 
					
						
							|  |  |  | 								//     if (false) {
 | 
					
						
							|  |  |  | 								//       var f, x, y, z;   (in loose mode)
 | 
					
						
							|  |  |  | 								//       var x, y, z;      (in strict mode)
 | 
					
						
							|  |  |  | 								//     } else {
 | 
					
						
							|  |  |  | 								//       …
 | 
					
						
							|  |  |  | 								//     }
 | 
					
						
							|  |  |  | 								//
 | 
					
						
							|  |  |  | 								// NOTE: When code runs in strict mode, `var` declarations
 | 
					
						
							|  |  |  | 								// are hoisted but `function` declarations don't.
 | 
					
						
							|  |  |  | 								//
 | 
					
						
							|  |  |  | 								let declarations; | 
					
						
							|  |  |  | 								if (parser.scope.isStrict) { | 
					
						
							|  |  |  | 									// If the code runs in strict mode, variable declarations
 | 
					
						
							|  |  |  | 									// using `var` must be hoisted.
 | 
					
						
							|  |  |  | 									declarations = getHoistedDeclarations(branchToRemove, false); | 
					
						
							|  |  |  | 								} else { | 
					
						
							|  |  |  | 									// Otherwise, collect all hoisted declaration.
 | 
					
						
							|  |  |  | 									declarations = getHoistedDeclarations(branchToRemove, true); | 
					
						
							|  |  |  | 								} | 
					
						
							|  |  |  | 								let replacement; | 
					
						
							|  |  |  | 								if (declarations.length > 0) { | 
					
						
							|  |  |  | 									replacement = `{ var ${declarations.join(", ")}; }`; | 
					
						
							|  |  |  | 								} else { | 
					
						
							|  |  |  | 									replacement = "{}"; | 
					
						
							|  |  |  | 								} | 
					
						
							|  |  |  | 								const dep = new ConstDependency( | 
					
						
							|  |  |  | 									replacement, | 
					
						
							|  |  |  | 									branchToRemove.range | 
					
						
							|  |  |  | 								); | 
					
						
							|  |  |  | 								dep.loc = branchToRemove.loc; | 
					
						
							| 
									
										
										
										
											2019-10-30 13:40:40 +08:00
										 |  |  | 								parser.state.module.addPresentationalDependency(dep); | 
					
						
							| 
									
										
										
										
											2018-01-09 17:35:08 +08:00
										 |  |  | 							} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 							return bool; | 
					
						
							| 
									
										
										
										
											2018-01-09 17:35:08 +08:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					}); | 
					
						
							|  |  |  | 					parser.hooks.expressionConditionalOperator.tap( | 
					
						
							|  |  |  | 						"ConstPlugin", | 
					
						
							|  |  |  | 						expression => { | 
					
						
							|  |  |  | 							const param = parser.evaluateExpression(expression.test); | 
					
						
							|  |  |  | 							const bool = param.asBool(); | 
					
						
							|  |  |  | 							if (typeof bool === "boolean") { | 
					
						
							|  |  |  | 								if (expression.test.type !== "Literal") { | 
					
						
							|  |  |  | 									const dep = new ConstDependency(` ${bool}`, param.range); | 
					
						
							|  |  |  | 									dep.loc = expression.loc; | 
					
						
							| 
									
										
										
										
											2019-10-30 13:40:40 +08:00
										 |  |  | 									parser.state.module.addPresentationalDependency(dep); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 								} | 
					
						
							|  |  |  | 								// Expressions do not hoist.
 | 
					
						
							|  |  |  | 								// It is safe to remove the dead branch.
 | 
					
						
							|  |  |  | 								//
 | 
					
						
							|  |  |  | 								// Given the following code:
 | 
					
						
							|  |  |  | 								//
 | 
					
						
							|  |  |  | 								//   false ? someExpression() : otherExpression();
 | 
					
						
							|  |  |  | 								//
 | 
					
						
							|  |  |  | 								// the generated code is:
 | 
					
						
							|  |  |  | 								//
 | 
					
						
							|  |  |  | 								//   false ? undefined : otherExpression();
 | 
					
						
							|  |  |  | 								//
 | 
					
						
							|  |  |  | 								const branchToRemove = bool | 
					
						
							|  |  |  | 									? expression.alternate | 
					
						
							|  |  |  | 									: expression.consequent; | 
					
						
							|  |  |  | 								const dep = new ConstDependency( | 
					
						
							|  |  |  | 									"undefined", | 
					
						
							|  |  |  | 									branchToRemove.range | 
					
						
							|  |  |  | 								); | 
					
						
							|  |  |  | 								dep.loc = branchToRemove.loc; | 
					
						
							| 
									
										
										
										
											2019-10-30 13:40:40 +08:00
										 |  |  | 								parser.state.module.addPresentationalDependency(dep); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 								return bool; | 
					
						
							|  |  |  | 							} | 
					
						
							| 
									
										
										
										
											2017-01-09 02:07:44 +08:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					); | 
					
						
							| 
									
										
										
										
											2018-10-17 23:12:07 +08:00
										 |  |  | 					parser.hooks.expressionLogicalOperator.tap( | 
					
						
							|  |  |  | 						"ConstPlugin", | 
					
						
							|  |  |  | 						expression => { | 
					
						
							| 
									
										
										
										
											2018-10-18 14:21:12 +08:00
										 |  |  | 							if ( | 
					
						
							|  |  |  | 								expression.operator === "&&" || | 
					
						
							|  |  |  | 								expression.operator === "||" | 
					
						
							|  |  |  | 							) { | 
					
						
							|  |  |  | 								const param = parser.evaluateExpression(expression.left); | 
					
						
							|  |  |  | 								const bool = param.asBool(); | 
					
						
							|  |  |  | 								if (typeof bool === "boolean") { | 
					
						
							|  |  |  | 									// Expressions do not hoist.
 | 
					
						
							|  |  |  | 									// It is safe to remove the dead branch.
 | 
					
						
							|  |  |  | 									//
 | 
					
						
							|  |  |  | 									// ------------------------------------------
 | 
					
						
							|  |  |  | 									//
 | 
					
						
							|  |  |  | 									// Given the following code:
 | 
					
						
							|  |  |  | 									//
 | 
					
						
							|  |  |  | 									//   falsyExpression() && someExpression();
 | 
					
						
							|  |  |  | 									//
 | 
					
						
							|  |  |  | 									// the generated code is:
 | 
					
						
							|  |  |  | 									//
 | 
					
						
							|  |  |  | 									//   falsyExpression() && false;
 | 
					
						
							|  |  |  | 									//
 | 
					
						
							|  |  |  | 									// ------------------------------------------
 | 
					
						
							|  |  |  | 									//
 | 
					
						
							|  |  |  | 									// Given the following code:
 | 
					
						
							|  |  |  | 									//
 | 
					
						
							|  |  |  | 									//   truthyExpression() && someExpression();
 | 
					
						
							|  |  |  | 									//
 | 
					
						
							|  |  |  | 									// the generated code is:
 | 
					
						
							|  |  |  | 									//
 | 
					
						
							|  |  |  | 									//   true && someExpression();
 | 
					
						
							|  |  |  | 									//
 | 
					
						
							|  |  |  | 									// ------------------------------------------
 | 
					
						
							|  |  |  | 									//
 | 
					
						
							|  |  |  | 									// Given the following code:
 | 
					
						
							|  |  |  | 									//
 | 
					
						
							|  |  |  | 									//   truthyExpression() || someExpression();
 | 
					
						
							|  |  |  | 									//
 | 
					
						
							|  |  |  | 									// the generated code is:
 | 
					
						
							|  |  |  | 									//
 | 
					
						
							|  |  |  | 									//   truthyExpression() || false;
 | 
					
						
							|  |  |  | 									//
 | 
					
						
							|  |  |  | 									// ------------------------------------------
 | 
					
						
							|  |  |  | 									//
 | 
					
						
							|  |  |  | 									// Given the following code:
 | 
					
						
							|  |  |  | 									//
 | 
					
						
							|  |  |  | 									//   falsyExpression() || someExpression();
 | 
					
						
							|  |  |  | 									//
 | 
					
						
							|  |  |  | 									// the generated code is:
 | 
					
						
							|  |  |  | 									//
 | 
					
						
							|  |  |  | 									//   false && someExpression();
 | 
					
						
							|  |  |  | 									//
 | 
					
						
							|  |  |  | 									const keepRight = | 
					
						
							|  |  |  | 										(expression.operator === "&&" && bool) || | 
					
						
							|  |  |  | 										(expression.operator === "||" && !bool); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 									if (param.isBoolean() || keepRight) { | 
					
						
							| 
									
										
										
										
											2018-10-25 10:43:26 +08:00
										 |  |  | 										// for case like
 | 
					
						
							|  |  |  | 										//
 | 
					
						
							|  |  |  | 										//   return'development'===process.env.NODE_ENV&&'foo'
 | 
					
						
							|  |  |  | 										//
 | 
					
						
							|  |  |  | 										// we need a space before the bool to prevent result like
 | 
					
						
							|  |  |  | 										//
 | 
					
						
							|  |  |  | 										//   returnfalse&&'foo'
 | 
					
						
							|  |  |  | 										//
 | 
					
						
							|  |  |  | 										const dep = new ConstDependency(` ${bool}`, param.range); | 
					
						
							| 
									
										
										
										
											2018-10-18 14:21:12 +08:00
										 |  |  | 										dep.loc = expression.loc; | 
					
						
							| 
									
										
										
										
											2019-10-30 13:40:40 +08:00
										 |  |  | 										parser.state.module.addPresentationalDependency(dep); | 
					
						
							| 
									
										
										
										
											2018-10-18 14:21:12 +08:00
										 |  |  | 									} else { | 
					
						
							|  |  |  | 										parser.walkExpression(expression.left); | 
					
						
							|  |  |  | 									} | 
					
						
							|  |  |  | 									if (!keepRight) { | 
					
						
							|  |  |  | 										const dep = new ConstDependency( | 
					
						
							|  |  |  | 											"false", | 
					
						
							|  |  |  | 											expression.right.range | 
					
						
							|  |  |  | 										); | 
					
						
							|  |  |  | 										dep.loc = expression.loc; | 
					
						
							| 
									
										
										
										
											2019-10-30 13:40:40 +08:00
										 |  |  | 										parser.state.module.addPresentationalDependency(dep); | 
					
						
							| 
									
										
										
										
											2018-10-18 14:21:12 +08:00
										 |  |  | 									} | 
					
						
							|  |  |  | 									return keepRight; | 
					
						
							|  |  |  | 								} | 
					
						
							| 
									
										
										
										
											2018-10-17 23:12:07 +08:00
										 |  |  | 							} | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					); | 
					
						
							|  |  |  | 					parser.hooks.evaluateIdentifier | 
					
						
							|  |  |  | 						.for("__resourceQuery") | 
					
						
							|  |  |  | 						.tap("ConstPlugin", expr => { | 
					
						
							|  |  |  | 							if (!parser.state.module) return; | 
					
						
							| 
									
										
										
										
											2018-07-03 16:24:29 +08:00
										 |  |  | 							return evaluateToString(getQuery(parser.state.module.resource))( | 
					
						
							|  |  |  | 								expr | 
					
						
							|  |  |  | 							); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						}); | 
					
						
							|  |  |  | 					parser.hooks.expression | 
					
						
							|  |  |  | 						.for("__resourceQuery") | 
					
						
							| 
									
										
										
										
											2018-07-23 20:53:50 +08:00
										 |  |  | 						.tap("ConstPlugin", expr => { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 							if (!parser.state.module) return; | 
					
						
							| 
									
										
										
										
											2018-07-23 20:53:50 +08:00
										 |  |  | 							const dep = new CachedConstDependency( | 
					
						
							|  |  |  | 								JSON.stringify(getQuery(parser.state.module.resource)), | 
					
						
							|  |  |  | 								expr.range, | 
					
						
							|  |  |  | 								"__resourceQuery" | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 							); | 
					
						
							| 
									
										
										
										
											2018-07-23 20:53:50 +08:00
										 |  |  | 							dep.loc = expr.loc; | 
					
						
							| 
									
										
										
										
											2019-10-30 13:40:40 +08:00
										 |  |  | 							parser.state.module.addPresentationalDependency(dep); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 							return true; | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 				}; | 
					
						
							| 
									
										
										
										
											2017-12-14 17:22:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +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); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2017-01-09 02:07:44 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = ConstPlugin; |