| 
									
										
										
										
											2018-05-15 19:19:53 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-05 00:33:55 +08:00
										 |  |  | const _ = require("lodash"); | 
					
						
							| 
									
										
										
										
											2018-05-15 19:19:53 +08:00
										 |  |  | const path = require("path"); | 
					
						
							|  |  |  | const MemoryFs = require("memory-fs"); | 
					
						
							| 
									
										
										
										
											2019-06-05 00:33:55 +08:00
										 |  |  | const captureStdio = require("./helpers/captureStdio"); | 
					
						
							| 
									
										
										
										
											2018-05-15 19:19:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-01 14:18:45 +08:00
										 |  |  | let webpack; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-15 19:19:53 +08:00
										 |  |  | describe("ProgressPlugin", function() { | 
					
						
							| 
									
										
										
										
											2019-06-04 06:27:38 +08:00
										 |  |  | 	let stderr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	beforeEach(() => { | 
					
						
							| 
									
										
										
										
											2019-08-01 14:18:45 +08:00
										 |  |  | 		stderr = captureStdio(process.stderr, true); | 
					
						
							| 
									
										
										
										
											2019-08-05 18:15:03 +08:00
										 |  |  | 		webpack = require(".."); | 
					
						
							| 
									
										
										
										
											2019-06-04 06:27:38 +08:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 	afterEach(() => { | 
					
						
							|  |  |  | 		stderr && stderr.restore(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it("should not contain NaN as a percentage when it is applied to MultiCompiler", () => { | 
					
						
							| 
									
										
										
										
											2018-05-15 19:19:53 +08:00
										 |  |  | 		const compiler = createMultiCompiler(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-04 06:27:38 +08:00
										 |  |  | 		return RunCompilerAsync(compiler).then(() => { | 
					
						
							| 
									
										
										
										
											2019-06-05 00:33:55 +08:00
										 |  |  | 			expect(stderr.toString()).toContain("%"); | 
					
						
							| 
									
										
										
										
											2019-06-04 06:27:38 +08:00
										 |  |  | 			expect(stderr.toString()).not.toContain("NaN"); | 
					
						
							| 
									
										
										
										
											2018-05-15 19:19:53 +08:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2019-06-04 06:27:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-05 00:33:55 +08:00
										 |  |  | 	it("should not print lines longer than stderr.columns", () => { | 
					
						
							|  |  |  | 		const compiler = createSimpleCompiler(); | 
					
						
							| 
									
										
										
										
											2019-08-05 19:32:25 +08:00
										 |  |  | 		process.stderr.columns = 36; | 
					
						
							| 
									
										
										
										
											2019-06-05 00:33:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		return RunCompilerAsync(compiler).then(() => { | 
					
						
							|  |  |  | 			const logs = getLogs(stderr.toString()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			expect(logs.length).toBeGreaterThan(20); | 
					
						
							| 
									
										
										
										
											2019-08-05 18:15:03 +08:00
										 |  |  | 			logs.forEach(log => expect(log.length).toBeLessThanOrEqual(35)); | 
					
						
							| 
									
										
										
										
											2019-06-05 00:33:55 +08:00
										 |  |  | 			expect(logs).toContain( | 
					
						
							| 
									
										
										
										
											2019-08-05 18:15:03 +08:00
										 |  |  | 				"75% ...optimization ...ChunksPlugin", | 
					
						
							| 
									
										
										
										
											2019-06-05 00:33:55 +08:00
										 |  |  | 				"trims each detail string equally" | 
					
						
							|  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2019-08-01 14:18:45 +08:00
										 |  |  | 			expect(logs).toContain( | 
					
						
							| 
									
										
										
										
											2019-08-05 18:15:03 +08:00
										 |  |  | 				"10% ...ding ...ries ...ules ...tive", | 
					
						
							|  |  |  | 				"remove empty arguments and omit arguments when no space" | 
					
						
							| 
									
										
										
										
											2019-08-01 14:18:45 +08:00
										 |  |  | 			); | 
					
						
							| 
									
										
										
										
											2019-08-05 18:15:03 +08:00
										 |  |  | 			expect(logs).toContain("92% after chunk asset optimization"); | 
					
						
							|  |  |  | 			expect(logs).toContain("100%"); | 
					
						
							| 
									
										
										
										
											2019-06-05 00:33:55 +08:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it("should handle when stderr.columns is undefined", () => { | 
					
						
							|  |  |  | 		const compiler = createSimpleCompiler(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		process.stderr.columns = undefined; | 
					
						
							|  |  |  | 		return RunCompilerAsync(compiler).then(() => { | 
					
						
							|  |  |  | 			const logs = getLogs(stderr.toString()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			expect(logs.length).toBeGreaterThan(20); | 
					
						
							|  |  |  | 			expect(_.maxBy(logs, "length").length).toBeGreaterThan(50); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2018-05-15 19:19:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | const createMultiCompiler = () => { | 
					
						
							|  |  |  | 	const compiler = webpack([ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			context: path.join(__dirname, "fixtures"), | 
					
						
							|  |  |  | 			entry: "./a.js" | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			context: path.join(__dirname, "fixtures"), | 
					
						
							|  |  |  | 			entry: "./b.js" | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	]); | 
					
						
							|  |  |  | 	compiler.outputFileSystem = new MemoryFs(); | 
					
						
							| 
									
										
										
										
											2019-06-04 06:27:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-05 00:33:55 +08:00
										 |  |  | 	new webpack.ProgressPlugin().apply(compiler); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-15 19:19:53 +08:00
										 |  |  | 	return compiler; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-05 00:33:55 +08:00
										 |  |  | const createSimpleCompiler = () => { | 
					
						
							|  |  |  | 	const compiler = webpack({ | 
					
						
							|  |  |  | 		context: path.join(__dirname, "fixtures"), | 
					
						
							|  |  |  | 		entry: "./a.js" | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2019-06-04 06:27:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-05 00:33:55 +08:00
										 |  |  | 	compiler.outputFileSystem = new MemoryFs(); | 
					
						
							| 
									
										
										
										
											2019-06-04 06:27:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-10 20:00:28 +08:00
										 |  |  | 	new webpack.ProgressPlugin({ | 
					
						
							|  |  |  | 		activeModules: true | 
					
						
							|  |  |  | 	}).apply(compiler); | 
					
						
							| 
									
										
										
										
											2018-05-15 19:19:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-05 00:33:55 +08:00
										 |  |  | 	return compiler; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2018-05-15 19:19:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-01 14:18:45 +08:00
										 |  |  | const getLogs = logsStr => logsStr.split(/\r/).filter(v => !(v === " ")); | 
					
						
							| 
									
										
										
										
											2019-06-04 06:27:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-05 00:33:55 +08:00
										 |  |  | const RunCompilerAsync = compiler => | 
					
						
							|  |  |  | 	new Promise((resolve, reject) => { | 
					
						
							| 
									
										
										
										
											2018-05-15 19:19:53 +08:00
										 |  |  | 		compiler.run(err => { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							| 
									
										
										
										
											2019-06-05 00:33:55 +08:00
										 |  |  | 				reject(err); | 
					
						
							| 
									
										
										
										
											2018-05-15 19:19:53 +08:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2019-06-05 00:33:55 +08:00
										 |  |  | 				resolve(); | 
					
						
							| 
									
										
										
										
											2018-05-15 19:19:53 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); |