| 
									
										
										
										
											2013-02-26 19:36:34 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | var AbstractPlugin = require("../AbstractPlugin"); | 
					
						
							|  |  |  | var LabeledModuleDependency = require("./LabeledModuleDependency"); | 
					
						
							|  |  |  | var LabeledExportsDependency = require("./LabeledExportsDependency"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = AbstractPlugin.create({ | 
					
						
							|  |  |  | 	"label require": function(stmt) { | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 		if(stmt.body.type !== "ExpressionStatement") return; | 
					
						
							|  |  |  | 		switch(stmt.body.expression.type) { | 
					
						
							| 
									
										
										
										
											2015-07-13 06:20:09 +08:00
										 |  |  | 			case "Literal": | 
					
						
							|  |  |  | 				var param = this.evaluateExpression(stmt.body.expression); | 
					
						
							| 
									
										
										
										
											2013-02-26 19:36:34 +08:00
										 |  |  | 				return this.applyPluginsBailResult("label require:item", stmt, param); | 
					
						
							| 
									
										
										
										
											2015-07-13 06:20:09 +08:00
										 |  |  | 			case "SequenceExpression": | 
					
						
							|  |  |  | 				stmt.body.expression.expressions.forEach(function(expression) { | 
					
						
							|  |  |  | 					var param = this.evaluateExpression(expression); | 
					
						
							|  |  |  | 					return this.applyPluginsBailResult("label require:item", stmt, param); | 
					
						
							|  |  |  | 				}, this); | 
					
						
							|  |  |  | 				return true; | 
					
						
							| 
									
										
										
										
											2013-02-26 19:36:34 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 	"label require:item": function(stmt, param) { | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 		if(param.isString()) { | 
					
						
							| 
									
										
										
										
											2013-02-26 19:36:34 +08:00
										 |  |  | 			var dep = new LabeledModuleDependency(param.string, stmt.range); | 
					
						
							|  |  |  | 			dep.loc = stmt.loc; | 
					
						
							|  |  |  | 			dep.optional = !!this.scope.inTry; | 
					
						
							|  |  |  | 			this.state.current.addDependency(dep); | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 	"label exports": function(stmt) { | 
					
						
							| 
									
										
										
										
											2016-01-19 08:52:28 +08:00
										 |  |  | 		var name; | 
					
						
							|  |  |  | 		var dep; | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 		switch(stmt.body.type) { | 
					
						
							| 
									
										
										
										
											2015-07-13 06:20:09 +08:00
										 |  |  | 			case "VariableDeclaration": | 
					
						
							|  |  |  | 				stmt.body.declarations.forEach(function(decl) { | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 					if(!decl.init) return; | 
					
						
							| 
									
										
										
										
											2015-07-13 06:20:09 +08:00
										 |  |  | 					var dep = new LabeledExportsDependency(decl.id.name, decl.init.range[0]); | 
					
						
							|  |  |  | 					dep.loc = stmt.loc; | 
					
						
							|  |  |  | 					this.state.current.addDependency(dep); | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 					if(!this.state.module.meta.exports) this.state.module.meta.exports = []; | 
					
						
							| 
									
										
										
										
											2015-07-13 06:20:09 +08:00
										 |  |  | 					this.state.module.meta.exports.push(decl.id.name); | 
					
						
							|  |  |  | 				}, this); | 
					
						
							|  |  |  | 				return true; | 
					
						
							|  |  |  | 			case "FunctionDeclaration": | 
					
						
							| 
									
										
										
										
											2016-01-19 08:52:28 +08:00
										 |  |  | 				name = stmt.body.id.name; | 
					
						
							|  |  |  | 				dep = new LabeledExportsDependency(name, stmt.body.range[0]); | 
					
						
							| 
									
										
										
										
											2013-02-26 19:36:34 +08:00
										 |  |  | 				dep.loc = stmt.loc; | 
					
						
							|  |  |  | 				this.state.current.addDependency(dep); | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 				if(!this.state.module.meta.exports) this.state.module.meta.exports = []; | 
					
						
							| 
									
										
										
										
											2013-02-26 19:36:34 +08:00
										 |  |  | 				this.state.module.meta.exports.push(name); | 
					
						
							|  |  |  | 				return true; | 
					
						
							| 
									
										
										
										
											2015-07-13 06:20:09 +08:00
										 |  |  | 			case "ExpressionStatement": | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 				if(stmt.body.expression.type === "Identifier") { | 
					
						
							| 
									
										
										
										
											2016-01-19 08:52:28 +08:00
										 |  |  | 					name = stmt.body.expression.name; | 
					
						
							|  |  |  | 					dep = new LabeledExportsDependency(name, stmt.body.expression.range[0]); | 
					
						
							| 
									
										
										
										
											2013-02-26 19:36:34 +08:00
										 |  |  | 					dep.loc = stmt.loc; | 
					
						
							|  |  |  | 					this.state.current.addDependency(dep); | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 					if(!this.state.module.meta.exports) this.state.module.meta.exports = []; | 
					
						
							| 
									
										
										
										
											2013-02-26 19:36:34 +08:00
										 |  |  | 					this.state.module.meta.exports.push(name); | 
					
						
							| 
									
										
										
										
											2015-07-13 06:20:09 +08:00
										 |  |  | 					return true; | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 				} else if(stmt.body.expression.type === "SequenceExpression") { | 
					
						
							| 
									
										
										
										
											2015-07-13 06:20:09 +08:00
										 |  |  | 					stmt.body.expression.expressions.forEach(function(e) { | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 						if(e.type !== "Identifier") return; | 
					
						
							| 
									
										
										
										
											2016-01-19 08:52:28 +08:00
										 |  |  | 						name = e.name; | 
					
						
							|  |  |  | 						dep = new LabeledExportsDependency(name, e.range[0]); | 
					
						
							| 
									
										
										
										
											2015-07-13 06:20:09 +08:00
										 |  |  | 						dep.loc = stmt.loc; | 
					
						
							|  |  |  | 						this.state.current.addDependency(dep); | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 						if(!this.state.module.meta.exports) this.state.module.meta.exports = []; | 
					
						
							| 
									
										
										
										
											2015-07-13 06:20:09 +08:00
										 |  |  | 						this.state.module.meta.exports.push(name); | 
					
						
							|  |  |  | 					}, this); | 
					
						
							|  |  |  | 					return true; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2015-04-24 05:55:50 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-02-26 19:36:34 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | }); |