| 
									
										
										
										
											2013-02-16 00:08:14 +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-03 05:30:08 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2013-02-16 00:08:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-19 19:45:06 +08:00
										 |  |  | const WebpackError = require("../WebpackError"); | 
					
						
							|  |  |  | const { | 
					
						
							|  |  |  | 	evaluateToString, | 
					
						
							|  |  |  | 	toConstantDependency | 
					
						
							|  |  |  | } = require("../javascript/JavascriptParserHelpers"); | 
					
						
							| 
									
										
										
										
											2019-10-30 14:10:26 +08:00
										 |  |  | const makeSerializable = require("../util/makeSerializable"); | 
					
						
							| 
									
										
										
										
											2017-01-03 05:30:08 +08:00
										 |  |  | const RequireIncludeDependency = require("./RequireIncludeDependency"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-17 02:24:34 +08:00
										 |  |  | /** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */ | 
					
						
							|  |  |  | /** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */ | 
					
						
							|  |  |  | /** @typedef {import("../javascript/JavascriptParser").Range} Range */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 05:30:08 +08:00
										 |  |  | module.exports = class RequireIncludeDependencyParserPlugin { | 
					
						
							| 
									
										
										
										
											2023-05-22 04:31:30 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {boolean} warn true: warn about deprecation, false: don't warn | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2019-10-19 19:45:06 +08:00
										 |  |  | 	constructor(warn) { | 
					
						
							|  |  |  | 		this.warn = warn; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-06-17 02:24:34 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {JavascriptParser} parser the parser | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-01-03 05:30:08 +08:00
										 |  |  | 	apply(parser) { | 
					
						
							| 
									
										
										
										
											2019-10-19 19:45:06 +08:00
										 |  |  | 		const { warn } = this; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		parser.hooks.call | 
					
						
							|  |  |  | 			.for("require.include") | 
					
						
							|  |  |  | 			.tap("RequireIncludeDependencyParserPlugin", expr => { | 
					
						
							|  |  |  | 				if (expr.arguments.length !== 1) return; | 
					
						
							|  |  |  | 				const param = parser.evaluateExpression(expr.arguments[0]); | 
					
						
							|  |  |  | 				if (!param.isString()) return; | 
					
						
							| 
									
										
										
										
											2019-10-19 19:45:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				if (warn) { | 
					
						
							| 
									
										
										
										
											2019-11-08 19:43:05 +08:00
										 |  |  | 					parser.state.module.addWarning( | 
					
						
							| 
									
										
										
										
											2023-06-17 02:24:34 +08:00
										 |  |  | 						new RequireIncludeDeprecationWarning( | 
					
						
							|  |  |  | 							/** @type {DependencyLocation} */ (expr.loc) | 
					
						
							|  |  |  | 						) | 
					
						
							| 
									
										
										
										
											2019-10-19 19:45:06 +08:00
										 |  |  | 					); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-17 02:24:34 +08:00
										 |  |  | 				const dep = new RequireIncludeDependency( | 
					
						
							|  |  |  | 					/** @type {string} */ (param.string), | 
					
						
							|  |  |  | 					/** @type {Range} */ (expr.range) | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 				dep.loc = /** @type {DependencyLocation} */ (expr.loc); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				parser.state.current.addDependency(dep); | 
					
						
							|  |  |  | 				return true; | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2019-10-19 19:45:06 +08:00
										 |  |  | 		parser.hooks.evaluateTypeof | 
					
						
							|  |  |  | 			.for("require.include") | 
					
						
							|  |  |  | 			.tap("RequireIncludePlugin", expr => { | 
					
						
							|  |  |  | 				if (warn) { | 
					
						
							| 
									
										
										
										
											2019-11-08 19:43:05 +08:00
										 |  |  | 					parser.state.module.addWarning( | 
					
						
							| 
									
										
										
										
											2023-06-17 02:24:34 +08:00
										 |  |  | 						new RequireIncludeDeprecationWarning( | 
					
						
							|  |  |  | 							/** @type {DependencyLocation} */ (expr.loc) | 
					
						
							|  |  |  | 						) | 
					
						
							| 
									
										
										
										
											2019-10-19 19:45:06 +08:00
										 |  |  | 					); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				return evaluateToString("function")(expr); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		parser.hooks.typeof | 
					
						
							|  |  |  | 			.for("require.include") | 
					
						
							|  |  |  | 			.tap("RequireIncludePlugin", expr => { | 
					
						
							|  |  |  | 				if (warn) { | 
					
						
							| 
									
										
										
										
											2019-11-08 19:43:05 +08:00
										 |  |  | 					parser.state.module.addWarning( | 
					
						
							| 
									
										
										
										
											2023-06-17 02:24:34 +08:00
										 |  |  | 						new RequireIncludeDeprecationWarning( | 
					
						
							|  |  |  | 							/** @type {DependencyLocation} */ (expr.loc) | 
					
						
							|  |  |  | 						) | 
					
						
							| 
									
										
										
										
											2019-10-19 19:45:06 +08:00
										 |  |  | 					); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				return toConstantDependency(parser, JSON.stringify("function"))(expr); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-02-16 00:08:14 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-03 05:30:08 +08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2019-10-19 19:45:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class RequireIncludeDeprecationWarning extends WebpackError { | 
					
						
							| 
									
										
										
										
											2023-06-17 02:24:34 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {DependencyLocation} loc location | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2019-10-19 19:45:06 +08:00
										 |  |  | 	constructor(loc) { | 
					
						
							|  |  |  | 		super("require.include() is deprecated and will be removed soon."); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		this.name = "RequireIncludeDeprecationWarning"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		this.loc = loc; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-10-30 14:10:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | makeSerializable( | 
					
						
							|  |  |  | 	RequireIncludeDeprecationWarning, | 
					
						
							|  |  |  | 	"webpack/lib/dependencies/RequireIncludeDependencyParserPlugin", | 
					
						
							|  |  |  | 	"RequireIncludeDeprecationWarning" | 
					
						
							|  |  |  | ); |