| 
									
										
										
										
											2013-02-14 04:24:00 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2017-01-11 17:51:58 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2013-02-14 04:24:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-08 20:46:29 +08:00
										 |  |  | const ParserHelpers = require("./ParserHelpers"); | 
					
						
							| 
									
										
										
										
											2017-01-08 00:05:02 +08:00
										 |  |  | const ConstDependency = require("./dependencies/ConstDependency"); | 
					
						
							|  |  |  | const NullFactory = require("./NullFactory"); | 
					
						
							| 
									
										
										
										
											2014-06-16 21:18:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-08 00:05:02 +08:00
										 |  |  | module.exports = class RequireJsStuffPlugin { | 
					
						
							|  |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		compiler.hooks.compilation.tap( | 
					
						
							|  |  |  | 			"RequireJsStuffPlugin", | 
					
						
							|  |  |  | 			(compilation, { normalModuleFactory }) => { | 
					
						
							|  |  |  | 				compilation.dependencyFactories.set(ConstDependency, new NullFactory()); | 
					
						
							|  |  |  | 				compilation.dependencyTemplates.set( | 
					
						
							|  |  |  | 					ConstDependency, | 
					
						
							|  |  |  | 					new ConstDependency.Template() | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 				const handler = (parser, parserOptions) => { | 
					
						
							|  |  |  | 					if ( | 
					
						
							|  |  |  | 						typeof parserOptions.requireJs !== "undefined" && | 
					
						
							|  |  |  | 						!parserOptions.requireJs | 
					
						
							|  |  |  | 					) | 
					
						
							|  |  |  | 						return; | 
					
						
							| 
									
										
										
										
											2016-09-14 18:04:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					parser.hooks.call | 
					
						
							|  |  |  | 						.for("require.config") | 
					
						
							|  |  |  | 						.tap( | 
					
						
							|  |  |  | 							"RequireJsStuffPlugin", | 
					
						
							|  |  |  | 							ParserHelpers.toConstantDependency(parser, "undefined") | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 					parser.hooks.call | 
					
						
							|  |  |  | 						.for("requirejs.config") | 
					
						
							|  |  |  | 						.tap( | 
					
						
							|  |  |  | 							"RequireJsStuffPlugin", | 
					
						
							|  |  |  | 							ParserHelpers.toConstantDependency(parser, "undefined") | 
					
						
							|  |  |  | 						); | 
					
						
							| 
									
										
										
										
											2017-01-08 00:05:02 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					parser.hooks.expression | 
					
						
							|  |  |  | 						.for("require.version") | 
					
						
							|  |  |  | 						.tap( | 
					
						
							|  |  |  | 							"RequireJsStuffPlugin", | 
					
						
							|  |  |  | 							ParserHelpers.toConstantDependency( | 
					
						
							|  |  |  | 								parser, | 
					
						
							|  |  |  | 								JSON.stringify("0.0.0") | 
					
						
							|  |  |  | 							) | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 					parser.hooks.expression | 
					
						
							|  |  |  | 						.for("requirejs.onError") | 
					
						
							|  |  |  | 						.tap( | 
					
						
							|  |  |  | 							"RequireJsStuffPlugin", | 
					
						
							|  |  |  | 							ParserHelpers.toConstantDependencyWithWebpackRequire( | 
					
						
							|  |  |  | 								parser, | 
					
						
							|  |  |  | 								"__webpack_require__.oe" | 
					
						
							|  |  |  | 							) | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 				}; | 
					
						
							|  |  |  | 				normalModuleFactory.hooks.parser | 
					
						
							|  |  |  | 					.for("javascript/auto") | 
					
						
							|  |  |  | 					.tap("RequireJsStuffPlugin", handler); | 
					
						
							|  |  |  | 				normalModuleFactory.hooks.parser | 
					
						
							|  |  |  | 					.for("javascript/dynamic") | 
					
						
							|  |  |  | 					.tap("RequireJsStuffPlugin", handler); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2017-01-08 00:05:02 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-11 17:51:58 +08:00
										 |  |  | }; |