| 
									
										
										
										
											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-10 18:29:55 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ConstDependency = require("./dependencies/ConstDependency"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const NullFactory = require("./NullFactory"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-09 20:48:28 +08:00
										 |  |  | /** @typedef {import("./Compiler")} Compiler */ | 
					
						
							| 
									
										
										
										
											2018-06-20 03:19:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-10 18:29:55 +08:00
										 |  |  | class CompatibilityPlugin { | 
					
						
							| 
									
										
										
										
											2018-06-16 02:15:27 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * Apply the plugin | 
					
						
							| 
									
										
										
										
											2018-06-20 03:19:20 +08:00
										 |  |  | 	 * @param {Compiler} compiler Webpack Compiler | 
					
						
							| 
									
										
										
										
											2018-06-16 02:15:27 +08:00
										 |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-01-10 18:29:55 +08:00
										 |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		compiler.hooks.compilation.tap( | 
					
						
							|  |  |  | 			"CompatibilityPlugin", | 
					
						
							|  |  |  | 			(compilation, { normalModuleFactory }) => { | 
					
						
							|  |  |  | 				compilation.dependencyFactories.set(ConstDependency, new NullFactory()); | 
					
						
							|  |  |  | 				compilation.dependencyTemplates.set( | 
					
						
							|  |  |  | 					ConstDependency, | 
					
						
							|  |  |  | 					new ConstDependency.Template() | 
					
						
							|  |  |  | 				); | 
					
						
							| 
									
										
										
										
											2017-01-10 18:29:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				normalModuleFactory.hooks.parser | 
					
						
							|  |  |  | 					.for("javascript/auto") | 
					
						
							|  |  |  | 					.tap("CompatibilityPlugin", (parser, parserOptions) => { | 
					
						
							|  |  |  | 						if ( | 
					
						
							| 
									
										
										
										
											2018-08-21 08:26:50 +08:00
										 |  |  | 							parserOptions.browserify !== undefined && | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 							!parserOptions.browserify | 
					
						
							|  |  |  | 						) | 
					
						
							|  |  |  | 							return; | 
					
						
							| 
									
										
										
										
											2017-01-10 18:29:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						parser.hooks.call | 
					
						
							|  |  |  | 							.for("require") | 
					
						
							|  |  |  | 							.tap("CompatibilityPlugin", expr => { | 
					
						
							|  |  |  | 								// support for browserify style require delegator: "require(o, !0)"
 | 
					
						
							|  |  |  | 								if (expr.arguments.length !== 2) return; | 
					
						
							|  |  |  | 								const second = parser.evaluateExpression(expr.arguments[1]); | 
					
						
							|  |  |  | 								if (!second.isBoolean()) return; | 
					
						
							|  |  |  | 								if (second.asBool() !== true) return; | 
					
						
							|  |  |  | 								const dep = new ConstDependency("require", expr.callee.range); | 
					
						
							|  |  |  | 								dep.loc = expr.loc; | 
					
						
							|  |  |  | 								if (parser.state.current.dependencies.length > 1) { | 
					
						
							|  |  |  | 									const last = | 
					
						
							|  |  |  | 										parser.state.current.dependencies[ | 
					
						
							|  |  |  | 											parser.state.current.dependencies.length - 1 | 
					
						
							|  |  |  | 										]; | 
					
						
							|  |  |  | 									if ( | 
					
						
							|  |  |  | 										last.critical && | 
					
						
							|  |  |  | 										last.options && | 
					
						
							|  |  |  | 										last.options.request === "." && | 
					
						
							|  |  |  | 										last.userRequest === "." && | 
					
						
							|  |  |  | 										last.options.recursive | 
					
						
							|  |  |  | 									) | 
					
						
							|  |  |  | 										parser.state.current.dependencies.pop(); | 
					
						
							|  |  |  | 								} | 
					
						
							|  |  |  | 								parser.state.current.addDependency(dep); | 
					
						
							|  |  |  | 								return true; | 
					
						
							|  |  |  | 							}); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2017-01-10 18:29:55 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | module.exports = CompatibilityPlugin; |