| 
									
										
										
										
											2016-07-13 17:03:14 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2017-01-05 03:15:54 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2016-07-13 17:03:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-05 21:38:15 +08:00
										 |  |  | const identifierUtils = require("../util/identifier"); | 
					
						
							| 
									
										
										
										
											2016-10-29 17:11:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | function moveModuleBetween(oldChunk, newChunk) { | 
					
						
							|  |  |  | 	return function(module) { | 
					
						
							|  |  |  | 		oldChunk.moveModule(module, newChunk); | 
					
						
							| 
									
										
										
										
											2017-01-11 17:51:58 +08:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2016-10-29 17:11:44 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function isNotAEntryModule(entryModule) { | 
					
						
							|  |  |  | 	return function(module) { | 
					
						
							|  |  |  | 		return entryModule !== module; | 
					
						
							| 
									
										
										
										
											2017-01-11 17:51:58 +08:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2016-07-13 17:03:14 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function copyWithReason(obj) { | 
					
						
							| 
									
										
										
										
											2017-02-05 07:37:36 +08:00
										 |  |  | 	const newObj = {}; | 
					
						
							| 
									
										
										
										
											2017-01-05 03:15:54 +08:00
										 |  |  | 	Object.keys(obj).forEach((key) => { | 
					
						
							| 
									
										
										
										
											2016-07-13 17:03:14 +08:00
										 |  |  | 		newObj[key] = obj[key]; | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	if(!newObj.reasons || newObj.reasons.indexOf("aggressive-splitted") < 0) | 
					
						
							|  |  |  | 		newObj.reasons = (newObj.reasons || []).concat("aggressive-splitted"); | 
					
						
							|  |  |  | 	return newObj; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-05 03:15:54 +08:00
										 |  |  | class AggressiveSplittingPlugin { | 
					
						
							|  |  |  | 	constructor(options) { | 
					
						
							|  |  |  | 		this.options = options || {}; | 
					
						
							|  |  |  | 		if(typeof this.options.minSize !== "number") this.options.minSize = 30 * 1024; | 
					
						
							|  |  |  | 		if(typeof this.options.maxSize !== "number") this.options.maxSize = 50 * 1024; | 
					
						
							|  |  |  | 		if(typeof this.options.chunkOverhead !== "number") this.options.chunkOverhead = 0; | 
					
						
							|  |  |  | 		if(typeof this.options.entryChunkMultiplicator !== "number") this.options.entryChunkMultiplicator = 1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2017-06-02 17:09:17 +08:00
										 |  |  | 		compiler.plugin("this-compilation", (compilation) => { | 
					
						
							| 
									
										
										
										
											2017-01-05 03:15:54 +08:00
										 |  |  | 			compilation.plugin("optimize-chunks-advanced", (chunks) => { | 
					
						
							| 
									
										
										
										
											2017-07-18 20:24:42 +08:00
										 |  |  | 				// Precompute stuff
 | 
					
						
							|  |  |  | 				const nameToModuleMap = new Map(); | 
					
						
							|  |  |  | 				compilation.modules.forEach(m => { | 
					
						
							|  |  |  | 					const name = identifierUtils.makePathsRelative(compiler.context, m.identifier(), compilation.cache); | 
					
						
							|  |  |  | 					nameToModuleMap.set(name, m); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-05 03:15:54 +08:00
										 |  |  | 				const savedSplits = compilation.records && compilation.records.aggressiveSplits || []; | 
					
						
							| 
									
										
										
										
											2017-02-05 07:37:36 +08:00
										 |  |  | 				const usedSplits = compilation._aggressiveSplittingSplits ? | 
					
						
							| 
									
										
										
										
											2017-02-05 10:10:08 +08:00
										 |  |  | 					savedSplits.concat(compilation._aggressiveSplittingSplits) : savedSplits; | 
					
						
							| 
									
										
										
										
											2017-02-05 07:37:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-05 03:15:54 +08:00
										 |  |  | 				const minSize = this.options.minSize; | 
					
						
							|  |  |  | 				const maxSize = this.options.maxSize; | 
					
						
							|  |  |  | 				// 1. try to restore to recorded splitting
 | 
					
						
							|  |  |  | 				for(let j = 0; j < usedSplits.length; j++) { | 
					
						
							|  |  |  | 					const splitData = usedSplits[j]; | 
					
						
							| 
									
										
										
										
											2017-07-18 20:24:42 +08:00
										 |  |  | 					const selectedModules = splitData.modules.map(name => nameToModuleMap.get(name)); | 
					
						
							| 
									
										
										
										
											2017-01-05 03:15:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-18 20:24:42 +08:00
										 |  |  | 					// Does the modules exist at all?
 | 
					
						
							|  |  |  | 					if(selectedModules.every(Boolean)) { | 
					
						
							| 
									
										
										
										
											2017-04-21 16:05:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-18 20:24:42 +08:00
										 |  |  | 						// Find all chunks containing all modules in the split
 | 
					
						
							|  |  |  | 						for(let i = 0; i < chunks.length; i++) { | 
					
						
							|  |  |  | 							const chunk = chunks[i]; | 
					
						
							| 
									
										
										
										
											2017-04-21 16:05:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-18 20:24:42 +08:00
										 |  |  | 							// Cheap check if chunk is suitable at all
 | 
					
						
							|  |  |  | 							if(chunk.getNumberOfModules() < splitData.modules.length) | 
					
						
							|  |  |  | 								continue; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 							// Check if all modules are in the chunk
 | 
					
						
							|  |  |  | 							if(selectedModules.every(m => chunk.containsModule(m))) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 								// Is chunk identical to the split or do we need to split it?
 | 
					
						
							|  |  |  | 								if(chunk.getNumberOfModules() > splitData.modules.length) { | 
					
						
							|  |  |  | 									// split the chunk into two parts
 | 
					
						
							|  |  |  | 									const newChunk = compilation.addChunk(); | 
					
						
							|  |  |  | 									selectedModules.forEach(moveModuleBetween(chunk, newChunk)); | 
					
						
							|  |  |  | 									chunk.split(newChunk); | 
					
						
							|  |  |  | 									chunk.name = null; | 
					
						
							|  |  |  | 									newChunk._fromAggressiveSplitting = true; | 
					
						
							|  |  |  | 									if(j < savedSplits.length) | 
					
						
							|  |  |  | 										newChunk._fromAggressiveSplittingIndex = j; | 
					
						
							|  |  |  | 									if(splitData.id !== null && splitData.id !== undefined) { | 
					
						
							|  |  |  | 										newChunk.id = splitData.id; | 
					
						
							|  |  |  | 									} | 
					
						
							|  |  |  | 									newChunk.origins = chunk.origins.map(copyWithReason); | 
					
						
							|  |  |  | 									chunk.origins = chunk.origins.map(copyWithReason); | 
					
						
							|  |  |  | 									return true; | 
					
						
							|  |  |  | 								} else { // chunk is identical to the split
 | 
					
						
							|  |  |  | 									if(j < savedSplits.length) | 
					
						
							|  |  |  | 										chunk._fromAggressiveSplittingIndex = j; | 
					
						
							|  |  |  | 									chunk.name = null; | 
					
						
							|  |  |  | 									if(splitData.id !== null && splitData.id !== undefined) { | 
					
						
							|  |  |  | 										chunk.id = splitData.id; | 
					
						
							|  |  |  | 									} | 
					
						
							| 
									
										
										
										
											2017-03-26 10:28:33 +08:00
										 |  |  | 								} | 
					
						
							| 
									
										
										
										
											2017-01-05 03:15:54 +08:00
										 |  |  | 							} | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2017-07-18 20:24:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-05 03:15:54 +08:00
										 |  |  | 				// 2. for any other chunk which isn't splitted yet, split it
 | 
					
						
							| 
									
										
										
										
											2017-02-05 07:37:36 +08:00
										 |  |  | 				for(let i = 0; i < chunks.length; i++) { | 
					
						
							|  |  |  | 					const chunk = chunks[i]; | 
					
						
							| 
									
										
										
										
											2017-01-05 03:15:54 +08:00
										 |  |  | 					const size = chunk.size(this.options); | 
					
						
							| 
									
										
										
										
											2017-04-21 16:05:56 +08:00
										 |  |  | 					if(size > maxSize && chunk.getNumberOfModules() > 1) { | 
					
						
							| 
									
										
										
										
											2017-02-05 07:37:36 +08:00
										 |  |  | 						const newChunk = compilation.addChunk(); | 
					
						
							| 
									
										
										
										
											2017-05-20 20:46:21 +08:00
										 |  |  | 						const modules = chunk.getModules() | 
					
						
							| 
									
										
										
										
											2017-01-05 03:15:54 +08:00
										 |  |  | 							.filter(isNotAEntryModule(chunk.entryModule)) | 
					
						
							|  |  |  | 							.sort((a, b) => { | 
					
						
							|  |  |  | 								a = a.identifier(); | 
					
						
							|  |  |  | 								b = b.identifier(); | 
					
						
							|  |  |  | 								if(a > b) return 1; | 
					
						
							|  |  |  | 								if(a < b) return -1; | 
					
						
							|  |  |  | 								return 0; | 
					
						
							|  |  |  | 							}); | 
					
						
							|  |  |  | 						for(let k = 0; k < modules.length; k++) { | 
					
						
							|  |  |  | 							chunk.moveModule(modules[k], newChunk); | 
					
						
							|  |  |  | 							const newSize = newChunk.size(this.options); | 
					
						
							|  |  |  | 							const chunkSize = chunk.size(this.options); | 
					
						
							|  |  |  | 							// break early if it's fine
 | 
					
						
							|  |  |  | 							if(chunkSize < maxSize && newSize < maxSize && newSize >= minSize && chunkSize >= minSize) | 
					
						
							|  |  |  | 								break; | 
					
						
							|  |  |  | 							if(newSize > maxSize && k === 0) { | 
					
						
							|  |  |  | 								// break if there is a single module which is bigger than maxSize
 | 
					
						
							|  |  |  | 								break; | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 							if(newSize > maxSize || chunkSize < minSize) { | 
					
						
							|  |  |  | 								// move it back
 | 
					
						
							|  |  |  | 								newChunk.moveModule(modules[k], chunk); | 
					
						
							|  |  |  | 								// check if it's fine now
 | 
					
						
							|  |  |  | 								if(newSize < maxSize && newSize >= minSize && chunkSize >= minSize) | 
					
						
							|  |  |  | 									break; | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2017-04-21 16:05:56 +08:00
										 |  |  | 						if(newChunk.getNumberOfModules() > 0) { | 
					
						
							| 
									
										
										
										
											2016-07-13 17:03:14 +08:00
										 |  |  | 							chunk.split(newChunk); | 
					
						
							|  |  |  | 							chunk.name = null; | 
					
						
							|  |  |  | 							newChunk.origins = chunk.origins.map(copyWithReason); | 
					
						
							|  |  |  | 							chunk.origins = chunk.origins.map(copyWithReason); | 
					
						
							| 
									
										
										
										
											2017-01-05 03:15:54 +08:00
										 |  |  | 							compilation._aggressiveSplittingSplits = (compilation._aggressiveSplittingSplits || []).concat({ | 
					
						
							| 
									
										
										
										
											2017-07-18 04:30:29 +08:00
										 |  |  | 								modules: newChunk.mapModules(m => identifierUtils.makePathsRelative(compiler.context, m.identifier(), compilation.cache)) | 
					
						
							| 
									
										
										
										
											2017-01-05 03:15:54 +08:00
										 |  |  | 							}); | 
					
						
							| 
									
										
										
										
											2016-07-13 17:03:14 +08:00
										 |  |  | 							return true; | 
					
						
							|  |  |  | 						} else { | 
					
						
							| 
									
										
										
										
											2017-01-05 03:15:54 +08:00
										 |  |  | 							chunks.splice(chunks.indexOf(newChunk), 1); | 
					
						
							| 
									
										
										
										
											2016-07-13 17:03:14 +08:00
										 |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2017-01-05 03:15:54 +08:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 			compilation.plugin("record-hash", (records) => { | 
					
						
							|  |  |  | 				// 3. save to made splittings to records
 | 
					
						
							|  |  |  | 				const minSize = this.options.minSize; | 
					
						
							|  |  |  | 				if(!records.aggressiveSplits) records.aggressiveSplits = []; | 
					
						
							|  |  |  | 				compilation.chunks.forEach((chunk) => { | 
					
						
							|  |  |  | 					if(chunk.hasEntryModule()) return; | 
					
						
							|  |  |  | 					const size = chunk.size(this.options); | 
					
						
							|  |  |  | 					const incorrectSize = size < minSize; | 
					
						
							| 
									
										
										
										
											2017-07-18 04:30:29 +08:00
										 |  |  | 					const modules = chunk.mapModules(m => identifierUtils.makePathsRelative(compiler.context, m.identifier(), compilation.cache)); | 
					
						
							| 
									
										
										
										
											2017-01-05 03:15:54 +08:00
										 |  |  | 					if(typeof chunk._fromAggressiveSplittingIndex === "undefined") { | 
					
						
							|  |  |  | 						if(incorrectSize) return; | 
					
						
							|  |  |  | 						chunk.recorded = true; | 
					
						
							|  |  |  | 						records.aggressiveSplits.push({ | 
					
						
							|  |  |  | 							modules: modules, | 
					
						
							|  |  |  | 							hash: chunk.hash, | 
					
						
							|  |  |  | 							id: chunk.id | 
					
						
							| 
									
										
										
										
											2016-07-13 17:03:14 +08:00
										 |  |  | 						}); | 
					
						
							|  |  |  | 					} else { | 
					
						
							| 
									
										
										
										
											2017-02-05 07:37:36 +08:00
										 |  |  | 						const splitData = records.aggressiveSplits[chunk._fromAggressiveSplittingIndex]; | 
					
						
							| 
									
										
										
										
											2017-01-05 03:15:54 +08:00
										 |  |  | 						if(splitData.hash !== chunk.hash || incorrectSize) { | 
					
						
							|  |  |  | 							if(chunk._fromAggressiveSplitting) { | 
					
						
							|  |  |  | 								chunk._aggressiveSplittingInvalid = true; | 
					
						
							|  |  |  | 								splitData.invalid = true; | 
					
						
							|  |  |  | 							} else { | 
					
						
							|  |  |  | 								splitData.hash = chunk.hash; | 
					
						
							|  |  |  | 							} | 
					
						
							| 
									
										
										
										
											2016-07-13 17:03:14 +08:00
										 |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2017-01-05 03:15:54 +08:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 				records.aggressiveSplits = records.aggressiveSplits.filter((splitData) => { | 
					
						
							|  |  |  | 					return !splitData.invalid; | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2016-07-13 17:03:14 +08:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-01-05 03:15:54 +08:00
										 |  |  | 			compilation.plugin("need-additional-seal", (callback) => { | 
					
						
							|  |  |  | 				const invalid = compilation.chunks.some((chunk) => { | 
					
						
							|  |  |  | 					return chunk._aggressiveSplittingInvalid; | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 				if(invalid) | 
					
						
							|  |  |  | 					return true; | 
					
						
							| 
									
										
										
										
											2016-07-13 17:03:14 +08:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-01-05 03:15:54 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | module.exports = AggressiveSplittingPlugin; |