| 
									
										
										
										
											2017-04-08 22:20:40 +08:00
										 |  |  | /*globals describe it */ | 
					
						
							| 
									
										
										
										
											2017-01-18 23:11:11 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const path = require("path"); | 
					
						
							|  |  |  | const fs = require("fs"); | 
					
						
							| 
									
										
										
										
											2015-04-27 03:19:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 23:11:11 +08:00
										 |  |  | const webpack = require("../lib/webpack"); | 
					
						
							| 
									
										
										
										
											2017-11-21 17:41:01 +08:00
										 |  |  | const Stats = require("../lib/Stats"); | 
					
						
							| 
									
										
										
										
											2015-04-27 03:19:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 23:11:11 +08:00
										 |  |  | const base = path.join(__dirname, "statsCases"); | 
					
						
							|  |  |  | const outputBase = path.join(__dirname, "js", "stats"); | 
					
						
							| 
									
										
										
										
											2017-02-03 21:45:37 +08:00
										 |  |  | const tests = fs.readdirSync(base).filter(testName => | 
					
						
							| 
									
										
										
										
											2017-01-18 23:11:11 +08:00
										 |  |  | 	fs.existsSync(path.join(base, testName, "index.js")) || | 
					
						
							| 
									
										
										
										
											2017-02-03 21:45:37 +08:00
										 |  |  | 	fs.existsSync(path.join(base, testName, "webpack.config.js")) | 
					
						
							|  |  |  | ); | 
					
						
							| 
									
										
										
										
											2015-04-27 03:19:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-19 21:40:40 +08:00
										 |  |  | describe("StatsTestCases", () => { | 
					
						
							| 
									
										
										
										
											2017-01-18 23:11:11 +08:00
										 |  |  | 	tests.forEach(testName => { | 
					
						
							| 
									
										
										
										
											2018-01-24 23:00:43 +08:00
										 |  |  | 		it("should print correct stats for " + testName, (done) => { | 
					
						
							|  |  |  | 			jest.setTimeout(10000); | 
					
						
							| 
									
										
										
										
											2017-01-18 23:11:11 +08:00
										 |  |  | 			let options = { | 
					
						
							| 
									
										
										
										
											2017-11-21 17:41:01 +08:00
										 |  |  | 				mode: "development", | 
					
						
							| 
									
										
										
										
											2015-05-16 22:10:25 +08:00
										 |  |  | 				entry: "./index", | 
					
						
							|  |  |  | 				output: { | 
					
						
							|  |  |  | 					filename: "bundle.js" | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2015-04-27 03:19:39 +08:00
										 |  |  | 			}; | 
					
						
							|  |  |  | 			if(fs.existsSync(path.join(base, testName, "webpack.config.js"))) { | 
					
						
							|  |  |  | 				options = require(path.join(base, testName, "webpack.config.js")); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-01-18 23:11:11 +08:00
										 |  |  | 			(Array.isArray(options) ? options : [options]).forEach((options) => { | 
					
						
							| 
									
										
										
										
											2016-09-02 04:43:50 +08:00
										 |  |  | 				if(!options.context) options.context = path.join(base, testName); | 
					
						
							|  |  |  | 				if(!options.output) options.output = options.output || {}; | 
					
						
							|  |  |  | 				if(!options.output.path) options.output.path = path.join(outputBase, testName); | 
					
						
							| 
									
										
										
										
											2017-09-22 20:34:51 +08:00
										 |  |  | 				if(!options.plugins) options.plugins = []; | 
					
						
							| 
									
										
										
										
											2017-12-13 23:05:21 +08:00
										 |  |  | 				if(!options.optimization) options.optimization = {}; | 
					
						
							|  |  |  | 				if(options.optimization.minimize === undefined) options.optimization.minimize = false; | 
					
						
							| 
									
										
										
										
											2017-09-22 20:34:51 +08:00
										 |  |  | 				// To support deprecated loaders
 | 
					
						
							|  |  |  | 				// TODO remove in webpack 5
 | 
					
						
							|  |  |  | 				options.plugins.push(new webpack.LoaderOptionsPlugin({ | 
					
						
							|  |  |  | 					options: {} | 
					
						
							|  |  |  | 				})); | 
					
						
							| 
									
										
										
										
											2015-09-03 20:17:11 +08:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-01-18 23:11:11 +08:00
										 |  |  | 			const c = webpack(options); | 
					
						
							|  |  |  | 			const compilers = c.compilers ? c.compilers : [c]; | 
					
						
							|  |  |  | 			compilers.forEach((c) => { | 
					
						
							|  |  |  | 				const ifs = c.inputFileSystem; | 
					
						
							| 
									
										
										
										
											2015-09-03 20:17:11 +08:00
										 |  |  | 				c.inputFileSystem = Object.create(ifs); | 
					
						
							| 
									
										
										
										
											2017-04-08 22:20:40 +08:00
										 |  |  | 				c.inputFileSystem.readFile = function() { | 
					
						
							| 
									
										
										
										
											2017-01-18 23:11:11 +08:00
										 |  |  | 					const args = Array.prototype.slice.call(arguments); | 
					
						
							|  |  |  | 					const callback = args.pop(); | 
					
						
							|  |  |  | 					ifs.readFile.apply(ifs, args.concat([(err, result) => { | 
					
						
							| 
									
										
										
										
											2015-09-03 20:17:11 +08:00
										 |  |  | 						if(err) return callback(err); | 
					
						
							|  |  |  | 						callback(null, result.toString("utf-8").replace(/\r/g, "")); | 
					
						
							|  |  |  | 					}])); | 
					
						
							|  |  |  | 				}; | 
					
						
							| 
									
										
										
										
											2017-12-20 16:53:33 +08:00
										 |  |  | 				new webpack.optimize.OccurrenceOrderPlugin().apply(c); | 
					
						
							| 
									
										
										
										
											2015-09-03 20:17:11 +08:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-01-18 23:11:11 +08:00
										 |  |  | 			c.run((err, stats) => { | 
					
						
							| 
									
										
										
										
											2015-04-27 03:19:39 +08:00
										 |  |  | 				if(err) return done(err); | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-03 17:01:29 +08:00
										 |  |  | 				if(/error$/.test(testName)) { | 
					
						
							| 
									
										
										
										
											2018-01-24 23:00:43 +08:00
										 |  |  | 					expect(stats.hasErrors()).toBe(true); | 
					
						
							| 
									
										
										
										
											2015-09-03 20:17:11 +08:00
										 |  |  | 				} else if(stats.hasErrors()) { | 
					
						
							| 
									
										
										
										
											2017-12-28 14:21:47 +08:00
										 |  |  | 					return done(new Error(stats.toJson().errors.join("\n\n"))); | 
					
						
							| 
									
										
										
										
											2015-08-03 17:01:29 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 23:11:11 +08:00
										 |  |  | 				let toStringOptions = { | 
					
						
							| 
									
										
										
										
											2017-11-21 17:41:01 +08:00
										 |  |  | 					context: path.join(base, testName), | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 					colors: false | 
					
						
							|  |  |  | 				}; | 
					
						
							| 
									
										
										
										
											2017-01-18 23:11:11 +08:00
										 |  |  | 				let hasColorSetting = false; | 
					
						
							| 
									
										
										
										
											2015-08-03 17:01:29 +08:00
										 |  |  | 				if(typeof options.stats !== "undefined") { | 
					
						
							|  |  |  | 					toStringOptions = options.stats; | 
					
						
							| 
									
										
										
										
											2017-11-21 17:41:01 +08:00
										 |  |  | 					if(toStringOptions === null || typeof toStringOptions !== "object") | 
					
						
							|  |  |  | 						toStringOptions = Stats.presetToOptions(toStringOptions); | 
					
						
							| 
									
										
										
										
											2015-11-14 21:08:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 					hasColorSetting = typeof options.stats.colors !== "undefined"; | 
					
						
							| 
									
										
										
										
											2017-11-21 17:41:01 +08:00
										 |  |  | 					if(!toStringOptions.context) toStringOptions.context = path.join(base, testName); | 
					
						
							| 
									
										
										
										
											2015-08-03 17:01:29 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2017-01-11 17:51:58 +08:00
										 |  |  | 				if(Array.isArray(options) && !toStringOptions.children) { | 
					
						
							|  |  |  | 					toStringOptions.children = options.map(o => o.stats); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2015-08-03 17:01:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 23:11:11 +08:00
										 |  |  | 				let actual = stats.toString(toStringOptions); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:00:43 +08:00
										 |  |  | 				expect(typeof actual).toBe("string"); | 
					
						
							| 
									
										
										
										
											2015-11-14 21:08:04 +08:00
										 |  |  | 				if(!hasColorSetting) { | 
					
						
							|  |  |  | 					actual = actual | 
					
						
							|  |  |  | 						.replace(/\u001b\[[0-9;]*m/g, "") | 
					
						
							|  |  |  | 						.replace(/[0-9]+(\s?ms)/g, "X$1"); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					actual = actual | 
					
						
							|  |  |  | 						.replace(/\u001b\[1m\u001b\[([0-9;]*)m/g, "<CLR=$1,BOLD>") | 
					
						
							|  |  |  | 						.replace(/\u001b\[1m/g, "<CLR=BOLD>") | 
					
						
							|  |  |  | 						.replace(/\u001b\[39m\u001b\[22m/g, "</CLR>") | 
					
						
							|  |  |  | 						.replace(/\u001b\[([0-9;]*)m/g, "<CLR=$1>") | 
					
						
							|  |  |  | 						.replace(/[0-9]+(<\/CLR>)?(\s?ms)/g, "X$1$2"); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				actual = actual | 
					
						
							| 
									
										
										
										
											2015-09-03 20:42:00 +08:00
										 |  |  | 					.replace(/\r\n?/g, "\n") | 
					
						
							|  |  |  | 					.replace(/[\t ]*Version:.+\n/g, "") | 
					
						
							| 
									
										
										
										
											2016-09-09 20:19:36 +08:00
										 |  |  | 					.replace(path.join(base, testName), "Xdir/" + testName) | 
					
						
							|  |  |  | 					.replace(/ dependencies:Xms/g, ""); | 
					
						
							| 
									
										
										
										
											2017-01-18 23:11:11 +08:00
										 |  |  | 				const expected = fs.readFileSync(path.join(base, testName, "expected.txt"), "utf-8").replace(/\r/g, ""); | 
					
						
							| 
									
										
										
										
											2015-04-27 03:19:39 +08:00
										 |  |  | 				if(actual !== expected) { | 
					
						
							|  |  |  | 					fs.writeFileSync(path.join(base, testName, "actual.txt"), actual, "utf-8"); | 
					
						
							|  |  |  | 				} else if(fs.existsSync(path.join(base, testName, "actual.txt"))) { | 
					
						
							|  |  |  | 					fs.unlinkSync(path.join(base, testName, "actual.txt")); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-01-24 23:00:43 +08:00
										 |  |  | 				expect(actual).toBe(expected); | 
					
						
							| 
									
										
										
										
											2015-04-27 03:19:39 +08:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }); |