| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2017-01-03 01:00:08 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 01:00:08 +08:00
										 |  |  | const ConstDependency = require("./dependencies/ConstDependency"); | 
					
						
							| 
									
										
										
										
											2017-01-09 02:11:26 +08:00
										 |  |  | const ParserHelpers = require("./ParserHelpers"); | 
					
						
							| 
									
										
										
										
											2014-06-16 21:18:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 01:00:08 +08:00
										 |  |  | const NullFactory = require("./NullFactory"); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-13 18:14:00 +08:00
										 |  |  | /* eslint-disable camelcase */ | 
					
						
							| 
									
										
										
										
											2017-01-03 01:00:08 +08:00
										 |  |  | const REPLACEMENTS = { | 
					
						
							| 
									
										
										
										
											2017-12-13 18:14:00 +08:00
										 |  |  | 	__webpack_require__: "__webpack_require__", | 
					
						
							|  |  |  | 	__webpack_public_path__: "__webpack_require__.p", | 
					
						
							|  |  |  | 	__webpack_modules__: "__webpack_require__.m", | 
					
						
							|  |  |  | 	__webpack_chunk_load__: "__webpack_require__.e", | 
					
						
							|  |  |  | 	__non_webpack_require__: "require", | 
					
						
							|  |  |  | 	__webpack_nonce__: "__webpack_require__.nc", | 
					
						
							|  |  |  | 	"require.onError": "__webpack_require__.oe" | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | const NO_WEBPACK_REQUIRE = { | 
					
						
							|  |  |  | 	__non_webpack_require__: true | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-01-03 01:00:08 +08:00
										 |  |  | const REPLACEMENT_TYPES = { | 
					
						
							| 
									
										
										
										
											2017-12-13 18:14:00 +08:00
										 |  |  | 	__webpack_public_path__: "string", | 
					
						
							|  |  |  | 	__webpack_require__: "function", | 
					
						
							|  |  |  | 	__webpack_modules__: "object", | 
					
						
							|  |  |  | 	__webpack_chunk_load__: "function", | 
					
						
							|  |  |  | 	__webpack_nonce__: "string" | 
					
						
							| 
									
										
										
										
											2013-07-11 05:20:07 +08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-12-13 18:14:00 +08:00
										 |  |  | /* eslint-enable camelcase */ | 
					
						
							| 
									
										
										
										
											2017-01-03 01:00:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class APIPlugin { | 
					
						
							|  |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		compiler.hooks.compilation.tap( | 
					
						
							|  |  |  | 			"APIPlugin", | 
					
						
							|  |  |  | 			(compilation, { normalModuleFactory }) => { | 
					
						
							|  |  |  | 				compilation.dependencyFactories.set(ConstDependency, new NullFactory()); | 
					
						
							|  |  |  | 				compilation.dependencyTemplates.set( | 
					
						
							|  |  |  | 					ConstDependency, | 
					
						
							|  |  |  | 					new ConstDependency.Template() | 
					
						
							|  |  |  | 				); | 
					
						
							| 
									
										
										
										
											2017-01-03 01:00:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				const handler = parser => { | 
					
						
							|  |  |  | 					Object.keys(REPLACEMENTS).forEach(key => { | 
					
						
							|  |  |  | 						parser.hooks.expression | 
					
						
							|  |  |  | 							.for(key) | 
					
						
							|  |  |  | 							.tap( | 
					
						
							|  |  |  | 								"APIPlugin", | 
					
						
							|  |  |  | 								NO_WEBPACK_REQUIRE[key] | 
					
						
							|  |  |  | 									? ParserHelpers.toConstantDependency( | 
					
						
							|  |  |  | 											parser, | 
					
						
							|  |  |  | 											REPLACEMENTS[key] | 
					
						
							| 
									
										
										
										
											2018-03-26 22:56:10 +08:00
										 |  |  | 									  ) | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 									: ParserHelpers.toConstantDependencyWithWebpackRequire( | 
					
						
							|  |  |  | 											parser, | 
					
						
							|  |  |  | 											REPLACEMENTS[key] | 
					
						
							| 
									
										
										
										
											2018-03-26 22:56:10 +08:00
										 |  |  | 									  ) | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 							); | 
					
						
							| 
									
										
										
										
											2019-02-28 16:10:17 +08:00
										 |  |  | 						const type = REPLACEMENT_TYPES[key]; | 
					
						
							|  |  |  | 						if (type) { | 
					
						
							|  |  |  | 							parser.hooks.evaluateTypeof | 
					
						
							|  |  |  | 								.for(key) | 
					
						
							|  |  |  | 								.tap("APIPlugin", ParserHelpers.evaluateToString(type)); | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					}); | 
					
						
							|  |  |  | 				}; | 
					
						
							| 
									
										
										
										
											2017-12-14 17:22:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				normalModuleFactory.hooks.parser | 
					
						
							|  |  |  | 					.for("javascript/auto") | 
					
						
							|  |  |  | 					.tap("APIPlugin", handler); | 
					
						
							|  |  |  | 				normalModuleFactory.hooks.parser | 
					
						
							|  |  |  | 					.for("javascript/dynamic") | 
					
						
							|  |  |  | 					.tap("APIPlugin", handler); | 
					
						
							|  |  |  | 				normalModuleFactory.hooks.parser | 
					
						
							|  |  |  | 					.for("javascript/esm") | 
					
						
							|  |  |  | 					.tap("APIPlugin", handler); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2017-01-03 01:00:08 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = APIPlugin; |