| 
									
										
										
										
											2018-01-20 00:06:59 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = class RuntimeChunkPlugin { | 
					
						
							| 
									
										
										
										
											2018-01-26 01:52:36 +08:00
										 |  |  | 	constructor(options) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		this.options = Object.assign( | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				name: entrypoint => `runtime~${entrypoint.name}` | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			options | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2018-01-26 01:52:36 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-01-20 00:06:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2018-01-24 19:00:50 +08:00
										 |  |  | 		compiler.hooks.thisCompilation.tap("RuntimeChunkPlugin", compilation => { | 
					
						
							| 
									
										
										
										
											2018-01-20 00:06:59 +08:00
										 |  |  | 			compilation.hooks.optimizeChunksAdvanced.tap("RuntimeChunkPlugin", () => { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				for (const entrypoint of compilation.entrypoints.values()) { | 
					
						
							| 
									
										
										
										
											2018-01-20 00:06:59 +08:00
										 |  |  | 					const chunk = entrypoint.getRuntimeChunk(); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					if (chunk.getNumberOfModules() > 0) { | 
					
						
							| 
									
										
										
										
											2018-02-02 00:50:03 +08:00
										 |  |  | 						let name = this.options.name; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						if (typeof name === "function") { | 
					
						
							| 
									
										
										
										
											2018-02-02 00:50:03 +08:00
										 |  |  | 							name = name(entrypoint); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						const newChunk = compilation.addChunk(name); | 
					
						
							| 
									
										
										
										
											2018-01-20 00:06:59 +08:00
										 |  |  | 						entrypoint.unshiftChunk(newChunk); | 
					
						
							|  |  |  | 						newChunk.addGroup(entrypoint); | 
					
						
							|  |  |  | 						entrypoint.setRuntimeChunk(newChunk); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }; |