| 
									
										
										
										
											2015-02-05 06:21:55 +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-04 04:19:06 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2015-02-05 06:21:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-11 12:27:09 +08:00
										 |  |  | const asyncLib = require("neo-async"); | 
					
						
							| 
									
										
										
										
											2017-01-04 04:19:06 +08:00
										 |  |  | const NormalModule = require("./NormalModule"); | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | const PrefetchDependency = require("./dependencies/PrefetchDependency"); | 
					
						
							| 
									
										
										
										
											2017-01-04 04:19:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-09 20:48:28 +08:00
										 |  |  | /** @typedef {import("./Compiler")} Compiler */ | 
					
						
							| 
									
										
										
										
											2018-06-21 18:39:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 04:19:06 +08:00
										 |  |  | class AutomaticPrefetchPlugin { | 
					
						
							| 
									
										
										
										
											2018-06-14 22:51:20 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * Apply the plugin | 
					
						
							| 
									
										
										
										
											2018-06-21 18:39:48 +08:00
										 |  |  | 	 * @param {Compiler} compiler Webpack Compiler | 
					
						
							| 
									
										
										
										
											2018-06-14 22:51:20 +08:00
										 |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-01-04 04:19:06 +08:00
										 |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		compiler.hooks.compilation.tap( | 
					
						
							|  |  |  | 			"AutomaticPrefetchPlugin", | 
					
						
							|  |  |  | 			(compilation, { normalModuleFactory }) => { | 
					
						
							|  |  |  | 				compilation.dependencyFactories.set( | 
					
						
							|  |  |  | 					PrefetchDependency, | 
					
						
							|  |  |  | 					normalModuleFactory | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2017-01-04 04:19:06 +08:00
										 |  |  | 		let lastModules = null; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		compiler.hooks.afterCompile.tap("AutomaticPrefetchPlugin", compilation => { | 
					
						
							| 
									
										
										
										
											2017-01-04 04:19:06 +08:00
										 |  |  | 			lastModules = compilation.modules | 
					
						
							|  |  |  | 				.filter(m => m instanceof NormalModule) | 
					
						
							| 
									
										
										
										
											2018-06-23 22:53:59 +08:00
										 |  |  | 				.map((/** @type {NormalModule} */ m) => ({ | 
					
						
							| 
									
										
										
										
											2017-01-04 04:19:06 +08:00
										 |  |  | 					context: m.context, | 
					
						
							|  |  |  | 					request: m.request | 
					
						
							|  |  |  | 				})); | 
					
						
							| 
									
										
										
										
											2015-02-05 06:21:55 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		compiler.hooks.make.tapAsync( | 
					
						
							|  |  |  | 			"AutomaticPrefetchPlugin", | 
					
						
							|  |  |  | 			(compilation, callback) => { | 
					
						
							|  |  |  | 				if (!lastModules) return callback(); | 
					
						
							|  |  |  | 				asyncLib.forEach( | 
					
						
							|  |  |  | 					lastModules, | 
					
						
							|  |  |  | 					(m, callback) => { | 
					
						
							| 
									
										
										
										
											2018-08-22 21:19:05 +08:00
										 |  |  | 						compilation.addModuleChain( | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 							m.context || compiler.context, | 
					
						
							|  |  |  | 							new PrefetchDependency(m.request), | 
					
						
							|  |  |  | 							callback | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					callback | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2017-01-04 04:19:06 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | module.exports = AutomaticPrefetchPlugin; |