| 
									
										
										
										
											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-11 01:08:04 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-05 21:36:15 +08:00
										 |  |  | const RuntimeGlobals = require("../RuntimeGlobals"); | 
					
						
							| 
									
										
										
										
											2019-10-22 15:27:52 +08:00
										 |  |  | const { | 
					
						
							|  |  |  | 	toConstantDependency | 
					
						
							|  |  |  | } = require("../javascript/JavascriptParserHelpers"); | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | const CommonJsRequireContextDependency = require("./CommonJsRequireContextDependency"); | 
					
						
							|  |  |  | const CommonJsRequireDependency = require("./CommonJsRequireDependency"); | 
					
						
							|  |  |  | const ContextDependencyHelpers = require("./ContextDependencyHelpers"); | 
					
						
							|  |  |  | const LocalModuleDependency = require("./LocalModuleDependency"); | 
					
						
							|  |  |  | const { getLocalModule } = require("./LocalModulesHelpers"); | 
					
						
							|  |  |  | const RequireHeaderDependency = require("./RequireHeaderDependency"); | 
					
						
							| 
									
										
										
										
											2014-03-11 23:08:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-11 01:08:04 +08:00
										 |  |  | class CommonJsRequireDependencyParserPlugin { | 
					
						
							|  |  |  | 	constructor(options) { | 
					
						
							|  |  |  | 		this.options = options; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-03-11 23:08:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-11 01:08:04 +08:00
										 |  |  | 	apply(parser) { | 
					
						
							|  |  |  | 		const options = this.options; | 
					
						
							| 
									
										
										
										
											2017-11-27 22:27:41 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		const processItem = (expr, param) => { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			if (param.isString()) { | 
					
						
							| 
									
										
										
										
											2017-11-27 22:27:41 +08:00
										 |  |  | 				const dep = new CommonJsRequireDependency(param.string, param.range); | 
					
						
							|  |  |  | 				dep.loc = expr.loc; | 
					
						
							|  |  |  | 				dep.optional = !!parser.scope.inTry; | 
					
						
							|  |  |  | 				parser.state.current.addDependency(dep); | 
					
						
							|  |  |  | 				return true; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 		const processContext = (expr, param) => { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			const dep = ContextDependencyHelpers.create( | 
					
						
							|  |  |  | 				CommonJsRequireContextDependency, | 
					
						
							|  |  |  | 				expr.range, | 
					
						
							|  |  |  | 				param, | 
					
						
							|  |  |  | 				expr, | 
					
						
							| 
									
										
										
										
											2018-10-30 19:39:43 +08:00
										 |  |  | 				options, | 
					
						
							|  |  |  | 				{}, | 
					
						
							|  |  |  | 				parser | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			); | 
					
						
							|  |  |  | 			if (!dep) return; | 
					
						
							| 
									
										
										
										
											2017-11-27 22:27:41 +08:00
										 |  |  | 			dep.loc = expr.loc; | 
					
						
							|  |  |  | 			dep.optional = !!parser.scope.inTry; | 
					
						
							|  |  |  | 			parser.state.current.addDependency(dep); | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		parser.hooks.expression | 
					
						
							|  |  |  | 			.for("require.cache") | 
					
						
							|  |  |  | 			.tap( | 
					
						
							|  |  |  | 				"CommonJsRequireDependencyParserPlugin", | 
					
						
							| 
									
										
										
										
											2018-11-06 02:03:12 +08:00
										 |  |  | 				toConstantDependency(parser, RuntimeGlobals.moduleCache, [ | 
					
						
							| 
									
										
										
										
											2018-11-05 21:36:15 +08:00
										 |  |  | 					RuntimeGlobals.moduleCache | 
					
						
							| 
									
										
										
										
											2018-11-06 02:03:12 +08:00
										 |  |  | 				]) | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			); | 
					
						
							|  |  |  | 		parser.hooks.expression | 
					
						
							|  |  |  | 			.for("require") | 
					
						
							|  |  |  | 			.tap("CommonJsRequireDependencyParserPlugin", expr => { | 
					
						
							|  |  |  | 				const dep = new CommonJsRequireContextDependency( | 
					
						
							|  |  |  | 					{ | 
					
						
							|  |  |  | 						request: options.unknownContextRequest, | 
					
						
							|  |  |  | 						recursive: options.unknownContextRecursive, | 
					
						
							|  |  |  | 						regExp: options.unknownContextRegExp, | 
					
						
							|  |  |  | 						mode: "sync" | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					expr.range | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 				dep.critical = | 
					
						
							|  |  |  | 					options.unknownContextCritical && | 
					
						
							|  |  |  | 					"require function is used in a way in which dependencies cannot be statically extracted"; | 
					
						
							| 
									
										
										
										
											2017-01-11 01:08:04 +08:00
										 |  |  | 				dep.loc = expr.loc; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				dep.optional = !!parser.scope.inTry; | 
					
						
							| 
									
										
										
										
											2017-01-11 01:08:04 +08:00
										 |  |  | 				parser.state.current.addDependency(dep); | 
					
						
							|  |  |  | 				return true; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2018-04-04 03:47:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-06 14:53:40 +08:00
										 |  |  | 		const createHandler = callNew => expr => { | 
					
						
							| 
									
										
										
										
											2018-04-01 23:21:26 +08:00
										 |  |  | 			if (expr.arguments.length !== 1) return; | 
					
						
							|  |  |  | 			let localModule; | 
					
						
							|  |  |  | 			const param = parser.evaluateExpression(expr.arguments[0]); | 
					
						
							|  |  |  | 			if (param.isConditional()) { | 
					
						
							|  |  |  | 				let isExpression = false; | 
					
						
							|  |  |  | 				for (const p of param.options) { | 
					
						
							|  |  |  | 					const result = processItem(expr, p); | 
					
						
							|  |  |  | 					if (result === undefined) { | 
					
						
							|  |  |  | 						isExpression = true; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2017-01-11 01:08:04 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2019-10-30 13:40:40 +08:00
										 |  |  | 				if (!isExpression) { | 
					
						
							|  |  |  | 					const dep = new RequireHeaderDependency(expr.callee.range); | 
					
						
							|  |  |  | 					dep.loc = expr.loc; | 
					
						
							|  |  |  | 					parser.state.module.addPresentationalDependency(dep); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					return true; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-04-01 23:21:26 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			if ( | 
					
						
							|  |  |  | 				param.isString() && | 
					
						
							| 
									
										
										
										
											2018-07-27 04:37:36 +08:00
										 |  |  | 				(localModule = getLocalModule(parser.state, param.string)) | 
					
						
							| 
									
										
										
										
											2018-04-01 23:21:26 +08:00
										 |  |  | 			) { | 
					
						
							| 
									
										
										
										
											2018-10-17 20:17:06 +08:00
										 |  |  | 				localModule.flagUsed(); | 
					
						
							| 
									
										
										
										
											2018-04-06 14:30:26 +08:00
										 |  |  | 				const dep = new LocalModuleDependency(localModule, expr.range, callNew); | 
					
						
							| 
									
										
										
										
											2018-04-01 23:21:26 +08:00
										 |  |  | 				dep.loc = expr.loc; | 
					
						
							| 
									
										
										
										
											2019-10-30 13:40:40 +08:00
										 |  |  | 				parser.state.module.addPresentationalDependency(dep); | 
					
						
							| 
									
										
										
										
											2018-04-01 23:21:26 +08:00
										 |  |  | 				return true; | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				const result = processItem(expr, param); | 
					
						
							|  |  |  | 				if (result === undefined) { | 
					
						
							|  |  |  | 					processContext(expr, param); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					const dep = new RequireHeaderDependency(expr.callee.range); | 
					
						
							|  |  |  | 					dep.loc = expr.loc; | 
					
						
							| 
									
										
										
										
											2019-10-30 13:40:40 +08:00
										 |  |  | 					parser.state.module.addPresentationalDependency(dep); | 
					
						
							| 
									
										
										
										
											2018-04-01 23:21:26 +08:00
										 |  |  | 				} | 
					
						
							|  |  |  | 				return true; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-04-04 03:47:41 +08:00
										 |  |  | 		}; | 
					
						
							|  |  |  | 		parser.hooks.call | 
					
						
							|  |  |  | 			.for("require") | 
					
						
							| 
									
										
										
										
											2018-04-06 14:53:40 +08:00
										 |  |  | 			.tap("CommonJsRequireDependencyParserPlugin", createHandler(false)); | 
					
						
							| 
									
										
										
										
											2018-04-04 03:47:41 +08:00
										 |  |  | 		parser.hooks.new | 
					
						
							|  |  |  | 			.for("require") | 
					
						
							| 
									
										
										
										
											2018-04-06 14:53:40 +08:00
										 |  |  | 			.tap("CommonJsRequireDependencyParserPlugin", createHandler(true)); | 
					
						
							| 
									
										
										
										
											2018-07-20 01:55:09 +08:00
										 |  |  | 		parser.hooks.call | 
					
						
							|  |  |  | 			.for("module.require") | 
					
						
							|  |  |  | 			.tap("CommonJsRequireDependencyParserPlugin", createHandler(false)); | 
					
						
							|  |  |  | 		parser.hooks.new | 
					
						
							|  |  |  | 			.for("module.require") | 
					
						
							|  |  |  | 			.tap("CommonJsRequireDependencyParserPlugin", createHandler(true)); | 
					
						
							| 
									
										
										
										
											2017-01-11 01:08:04 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | module.exports = CommonJsRequireDependencyParserPlugin; |