| 
									
										
										
										
											2017-03-26 13:24:11 +08:00
										 |  |  | /* global describe, it*/ | 
					
						
							| 
									
										
										
										
											2017-01-18 23:27:35 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2013-12-16 06:10:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-26 13:24:11 +08:00
										 |  |  | require("should"); | 
					
						
							| 
									
										
										
										
											2017-01-18 23:27:35 +08:00
										 |  |  | const path = require("path"); | 
					
						
							|  |  |  | const fs = require("fs"); | 
					
						
							|  |  |  | const vm = require("vm"); | 
					
						
							| 
									
										
										
										
											2017-11-21 17:41:01 +08:00
										 |  |  | const mkdirp = require("mkdirp"); | 
					
						
							| 
									
										
										
										
											2017-01-18 23:27:35 +08:00
										 |  |  | const Test = require("mocha/lib/test"); | 
					
						
							|  |  |  | const checkArrayExpectation = require("./checkArrayExpectation"); | 
					
						
							| 
									
										
										
										
											2013-12-16 06:10:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 23:27:35 +08:00
										 |  |  | const Stats = require("../lib/Stats"); | 
					
						
							|  |  |  | const webpack = require("../lib/webpack"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-21 17:41:01 +08:00
										 |  |  | const DEFAULT_OPTIMIZATIONS = { | 
					
						
							|  |  |  | 	removeAvailableModules: true, | 
					
						
							|  |  |  | 	removeEmptyChunks: true, | 
					
						
							|  |  |  | 	mergedDuplicateChunks: true, | 
					
						
							|  |  |  | 	flagIncludedChunks: true, | 
					
						
							|  |  |  | 	occurrenceOrder: true, | 
					
						
							|  |  |  | 	sideEffects: true, | 
					
						
							|  |  |  | 	providedExports: true, | 
					
						
							|  |  |  | 	usedExports: true, | 
					
						
							|  |  |  | 	noEmitOnErrors: false, | 
					
						
							|  |  |  | 	concatenateModules: false, | 
					
						
							|  |  |  | 	namedModules: false, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const NO_EMIT_ON_ERRORS_OPTIMIZATIONS = { | 
					
						
							|  |  |  | 	noEmitOnErrors: false | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 23:27:35 +08:00
										 |  |  | describe("TestCases", () => { | 
					
						
							|  |  |  | 	const casesPath = path.join(__dirname, "cases"); | 
					
						
							|  |  |  | 	let categories = fs.readdirSync(casesPath); | 
					
						
							|  |  |  | 	categories = categories.map((cat) => { | 
					
						
							| 
									
										
										
										
											2013-12-16 06:10:41 +08:00
										 |  |  | 		return { | 
					
						
							|  |  |  | 			name: cat, | 
					
						
							| 
									
										
										
										
											2017-01-18 23:27:35 +08:00
										 |  |  | 			tests: fs.readdirSync(path.join(casesPath, cat)).filter((folder) => folder.indexOf("_") < 0) | 
					
						
							| 
									
										
										
										
											2013-12-16 06:10:41 +08:00
										 |  |  | 		}; | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 	[{ | 
					
						
							| 
									
										
										
										
											2017-11-21 17:41:01 +08:00
										 |  |  | 		name: "normal", | 
					
						
							| 
									
										
										
										
											2017-05-10 19:15:14 +08:00
										 |  |  | 	}, { | 
					
						
							| 
									
										
										
										
											2017-11-21 17:41:01 +08:00
										 |  |  | 		name: "production", | 
					
						
							| 
									
										
										
										
											2017-12-13 23:05:21 +08:00
										 |  |  | 		mode: "production", | 
					
						
							| 
									
										
										
										
											2017-11-21 17:41:01 +08:00
										 |  |  | 	}, { | 
					
						
							|  |  |  | 		name: "development", | 
					
						
							|  |  |  | 		mode: "development", | 
					
						
							|  |  |  | 		devtool: "none" | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 	}, { | 
					
						
							|  |  |  | 		name: "hot", | 
					
						
							|  |  |  | 		plugins: [ | 
					
						
							| 
									
										
										
										
											2013-12-18 06:21:49 +08:00
										 |  |  | 			new webpack.HotModuleReplacementPlugin() | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 		] | 
					
						
							| 
									
										
										
										
											2015-08-09 19:32:19 +08:00
										 |  |  | 	}, { | 
					
						
							|  |  |  | 		name: "hot-multi-step", | 
					
						
							|  |  |  | 		plugins: [ | 
					
						
							| 
									
										
										
										
											2015-01-31 23:27:05 +08:00
										 |  |  | 			new webpack.HotModuleReplacementPlugin({ | 
					
						
							|  |  |  | 				multiStep: true | 
					
						
							|  |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2015-08-09 19:32:19 +08:00
										 |  |  | 		] | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 	}, { | 
					
						
							|  |  |  | 		name: "devtool-eval", | 
					
						
							|  |  |  | 		devtool: "eval" | 
					
						
							|  |  |  | 	}, { | 
					
						
							|  |  |  | 		name: "devtool-eval-named-modules", | 
					
						
							|  |  |  | 		devtool: "eval", | 
					
						
							|  |  |  | 		plugins: [ | 
					
						
							| 
									
										
										
										
											2015-06-28 04:47:51 +08:00
										 |  |  | 			new webpack.NamedModulesPlugin() | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 		] | 
					
						
							|  |  |  | 	}, { | 
					
						
							|  |  |  | 		name: "devtool-eval-source-map", | 
					
						
							|  |  |  | 		devtool: "#eval-source-map" | 
					
						
							|  |  |  | 	}, { | 
					
						
							|  |  |  | 		name: "devtool-inline-source-map", | 
					
						
							|  |  |  | 		devtool: "inline-source-map" | 
					
						
							|  |  |  | 	}, { | 
					
						
							|  |  |  | 		name: "devtool-source-map", | 
					
						
							|  |  |  | 		devtool: "#@source-map" | 
					
						
							|  |  |  | 	}, { | 
					
						
							|  |  |  | 		name: "devtool-cheap-inline-source-map", | 
					
						
							|  |  |  | 		devtool: "cheap-inline-source-map" | 
					
						
							|  |  |  | 	}, { | 
					
						
							|  |  |  | 		name: "devtool-cheap-eval-source-map", | 
					
						
							|  |  |  | 		devtool: "cheap-eval-source-map" | 
					
						
							|  |  |  | 	}, { | 
					
						
							|  |  |  | 		name: "devtool-cheap-eval-module-source-map", | 
					
						
							|  |  |  | 		devtool: "cheap-eval-module-source-map" | 
					
						
							|  |  |  | 	}, { | 
					
						
							|  |  |  | 		name: "devtool-cheap-source-map", | 
					
						
							|  |  |  | 		devtool: "cheap-source-map" | 
					
						
							|  |  |  | 	}, { | 
					
						
							|  |  |  | 		name: "minimized-source-map", | 
					
						
							| 
									
										
										
										
											2017-11-21 17:41:01 +08:00
										 |  |  | 		mode: "production", | 
					
						
							| 
									
										
										
										
											2016-11-10 15:52:00 +08:00
										 |  |  | 		devtool: "eval-cheap-module-source-map", | 
					
						
							| 
									
										
										
										
											2017-12-13 23:05:21 +08:00
										 |  |  | 		minimize: true | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 	}, { | 
					
						
							| 
									
										
										
										
											2016-11-10 15:52:00 +08:00
										 |  |  | 		name: "minimized-hashed-modules", | 
					
						
							| 
									
										
										
										
											2017-11-21 17:41:01 +08:00
										 |  |  | 		mode: "production", | 
					
						
							| 
									
										
										
										
											2016-06-04 20:47:20 +08:00
										 |  |  | 		minimize: true, | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 		plugins: [ | 
					
						
							| 
									
										
										
										
											2016-11-10 15:52:00 +08:00
										 |  |  | 			new webpack.HashedModuleIdsPlugin() | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 		] | 
					
						
							|  |  |  | 	}, { | 
					
						
							|  |  |  | 		name: "all-combined", | 
					
						
							| 
									
										
										
										
											2017-11-21 17:41:01 +08:00
										 |  |  | 		mode: "production", | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 		devtool: "#@source-map", | 
					
						
							| 
									
										
										
										
											2016-06-04 20:47:20 +08:00
										 |  |  | 		minimize: true, | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 		plugins: [ | 
					
						
							| 
									
										
										
										
											2013-12-18 06:21:49 +08:00
										 |  |  | 			new webpack.HotModuleReplacementPlugin(), | 
					
						
							| 
									
										
										
										
											2017-03-26 13:24:11 +08:00
										 |  |  | 			new webpack.NamedModulesPlugin(), | 
					
						
							|  |  |  | 			new webpack.NamedChunksPlugin() | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 		] | 
					
						
							| 
									
										
										
										
											2017-01-18 23:27:35 +08:00
										 |  |  | 	}].forEach((config) => { | 
					
						
							|  |  |  | 		describe(config.name, () => { | 
					
						
							|  |  |  | 			categories.forEach((category) => { | 
					
						
							| 
									
										
										
										
											2013-12-16 07:53:13 +08:00
										 |  |  | 				describe(category.name, function() { | 
					
						
							| 
									
										
										
										
											2016-11-10 15:52:00 +08:00
										 |  |  | 					this.timeout(30000); | 
					
						
							| 
									
										
										
										
											2017-01-18 23:27:35 +08:00
										 |  |  | 					category.tests.filter((test) => { | 
					
						
							|  |  |  | 						const testDirectory = path.join(casesPath, category.name, test); | 
					
						
							|  |  |  | 						const filterPath = path.join(testDirectory, "test.filter.js"); | 
					
						
							| 
									
										
										
										
											2016-11-08 19:09:48 +08:00
										 |  |  | 						if(fs.existsSync(filterPath) && !require(filterPath)(config)) { | 
					
						
							| 
									
										
										
										
											2017-01-18 23:27:35 +08:00
										 |  |  | 							describe.skip(test, () => it("filtered")); | 
					
						
							| 
									
										
										
										
											2016-11-08 19:09:48 +08:00
										 |  |  | 							return false; | 
					
						
							| 
									
										
										
										
											2016-06-04 20:47:20 +08:00
										 |  |  | 						} | 
					
						
							|  |  |  | 						return true; | 
					
						
							| 
									
										
										
										
											2017-01-18 23:27:35 +08:00
										 |  |  | 					}).forEach((testName) => { | 
					
						
							|  |  |  | 						const suite = describe(testName, () => {}); | 
					
						
							|  |  |  | 						it(testName + " should compile", (done) => { | 
					
						
							|  |  |  | 							const testDirectory = path.join(casesPath, category.name, testName); | 
					
						
							|  |  |  | 							const outputDirectory = path.join(__dirname, "js", config.name, category.name, testName); | 
					
						
							|  |  |  | 							const options = { | 
					
						
							| 
									
										
										
										
											2013-12-16 07:53:13 +08:00
										 |  |  | 								context: casesPath, | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 								entry: "./" + category.name + "/" + testName + "/index", | 
					
						
							| 
									
										
										
										
											2013-12-16 07:53:13 +08:00
										 |  |  | 								target: "async-node", | 
					
						
							| 
									
										
										
										
											2013-12-17 07:56:43 +08:00
										 |  |  | 								devtool: config.devtool, | 
					
						
							| 
									
										
										
										
											2017-11-21 17:41:01 +08:00
										 |  |  | 								mode: config.mode || "none", | 
					
						
							|  |  |  | 								optimization: config.mode ? NO_EMIT_ON_ERRORS_OPTIMIZATIONS : Object.assign({}, config.optimization, DEFAULT_OPTIMIZATIONS), | 
					
						
							|  |  |  | 								performance: { | 
					
						
							|  |  |  | 									hints: false | 
					
						
							|  |  |  | 								}, | 
					
						
							| 
									
										
										
										
											2013-12-16 07:53:13 +08:00
										 |  |  | 								output: { | 
					
						
							|  |  |  | 									pathinfo: true, | 
					
						
							|  |  |  | 									path: outputDirectory, | 
					
						
							|  |  |  | 									filename: "bundle.js" | 
					
						
							|  |  |  | 								}, | 
					
						
							| 
									
										
										
										
											2014-02-13 18:02:31 +08:00
										 |  |  | 								resolve: { | 
					
						
							| 
									
										
										
										
											2016-01-02 07:21:54 +08:00
										 |  |  | 									modules: ["web_modules", "node_modules"], | 
					
						
							|  |  |  | 									mainFields: ["webpack", "browser", "web", "browserify", ["jam", "main"], "main"], | 
					
						
							|  |  |  | 									aliasFields: ["browser"], | 
					
						
							| 
									
										
										
										
											2017-11-29 23:34:49 +08:00
										 |  |  | 									extensions: [".mjs", ".webpack.js", ".web.js", ".js", ".json"], | 
					
						
							|  |  |  | 									concord: true | 
					
						
							| 
									
										
										
										
											2014-02-13 18:02:31 +08:00
										 |  |  | 								}, | 
					
						
							|  |  |  | 								resolveLoader: { | 
					
						
							| 
									
										
										
										
											2016-01-02 07:21:54 +08:00
										 |  |  | 									modules: ["web_loaders", "web_modules", "node_loaders", "node_modules"], | 
					
						
							|  |  |  | 									mainFields: ["webpackLoader", "webLoader", "loader", "main"], | 
					
						
							| 
									
										
										
										
											2016-09-19 06:54:35 +08:00
										 |  |  | 									extensions: [".webpack-loader.js", ".web-loader.js", ".loader.js", ".js"] | 
					
						
							| 
									
										
										
										
											2014-02-13 18:02:31 +08:00
										 |  |  | 								}, | 
					
						
							| 
									
										
										
										
											2013-12-16 07:53:13 +08:00
										 |  |  | 								module: { | 
					
						
							| 
									
										
										
										
											2017-06-14 22:14:42 +08:00
										 |  |  | 									rules: [{ | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 										test: /\.coffee$/, | 
					
						
							| 
									
										
										
										
											2016-01-27 00:56:44 +08:00
										 |  |  | 										loader: "coffee-loader" | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 									}, { | 
					
						
							|  |  |  | 										test: /\.jade$/, | 
					
						
							| 
									
										
										
										
											2016-01-27 00:56:44 +08:00
										 |  |  | 										loader: "jade-loader" | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 									}] | 
					
						
							| 
									
										
										
										
											2013-12-18 06:21:49 +08:00
										 |  |  | 								}, | 
					
						
							| 
									
										
										
										
											2016-12-28 03:45:01 +08:00
										 |  |  | 								plugins: (config.plugins || []).concat(function() { | 
					
						
							| 
									
										
										
										
											2017-12-20 23:51:24 +08:00
										 |  |  | 									this.hooks.compilation.tap("TestCasesTest", (compilation) => { | 
					
						
							|  |  |  | 										["optimize", "optimizeModulesBasic", "optimizeChunksBasic", "afterOptimizeTree", "afterOptimizeAssets"].forEach((hook) => { | 
					
						
							|  |  |  | 											compilation.hooks[hook].tap("TestCasesTest", () => compilation.checkConstraints()); | 
					
						
							| 
									
										
										
										
											2016-07-18 06:41:26 +08:00
										 |  |  | 										}); | 
					
						
							| 
									
										
										
										
											2016-12-28 03:45:01 +08:00
										 |  |  | 									}); | 
					
						
							|  |  |  | 								}) | 
					
						
							| 
									
										
										
										
											2013-12-17 07:56:43 +08:00
										 |  |  | 							}; | 
					
						
							| 
									
										
										
										
											2017-01-18 23:27:35 +08:00
										 |  |  | 							webpack(options, (err, stats) => { | 
					
						
							| 
									
										
										
										
											2013-12-16 07:53:13 +08:00
										 |  |  | 								if(err) return done(err); | 
					
						
							| 
									
										
										
										
											2017-01-18 23:27:35 +08:00
										 |  |  | 								const statOptions = Stats.presetToOptions("verbose"); | 
					
						
							| 
									
										
										
										
											2016-09-07 15:46:27 +08:00
										 |  |  | 								statOptions.colors = false; | 
					
						
							| 
									
										
										
										
											2017-11-21 17:41:01 +08:00
										 |  |  | 								mkdirp.sync(outputDirectory); | 
					
						
							| 
									
										
										
										
											2016-09-07 15:46:27 +08:00
										 |  |  | 								fs.writeFileSync(path.join(outputDirectory, "stats.txt"), stats.toString(statOptions), "utf-8"); | 
					
						
							| 
									
										
										
										
											2017-01-18 23:27:35 +08:00
										 |  |  | 								const jsonStats = stats.toJson({ | 
					
						
							| 
									
										
										
										
											2014-02-13 18:02:31 +08:00
										 |  |  | 									errorDetails: true | 
					
						
							|  |  |  | 								}); | 
					
						
							| 
									
										
										
										
											2013-12-16 07:53:13 +08:00
										 |  |  | 								if(checkArrayExpectation(testDirectory, jsonStats, "error", "Error", done)) return; | 
					
						
							|  |  |  | 								if(checkArrayExpectation(testDirectory, jsonStats, "warning", "Warning", done)) return; | 
					
						
							| 
									
										
										
										
											2017-01-18 23:27:35 +08:00
										 |  |  | 								let exportedTest = 0; | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-16 07:53:13 +08:00
										 |  |  | 								function _it(title, fn) { | 
					
						
							| 
									
										
										
										
											2017-01-18 23:27:35 +08:00
										 |  |  | 									const test = new Test(title, fn); | 
					
						
							| 
									
										
										
										
											2013-12-16 07:53:13 +08:00
										 |  |  | 									suite.addTest(test); | 
					
						
							|  |  |  | 									exportedTest++; | 
					
						
							| 
									
										
										
										
											2017-11-29 20:21:00 +08:00
										 |  |  | 									// WORKAROUND for a v8 bug
 | 
					
						
							|  |  |  | 									// Error objects retrain all scopes in the stacktrace
 | 
					
						
							|  |  |  | 									test._trace = test._trace.message; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-16 07:53:13 +08:00
										 |  |  | 									return test; | 
					
						
							|  |  |  | 								} | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-16 07:53:13 +08:00
										 |  |  | 								function _require(module) { | 
					
						
							|  |  |  | 									if(module.substr(0, 2) === "./") { | 
					
						
							| 
									
										
										
										
											2017-01-18 23:27:35 +08:00
										 |  |  | 										const p = path.join(outputDirectory, module); | 
					
						
							|  |  |  | 										const fn = vm.runInThisContext("(function(require, module, exports, __dirname, it) {" + fs.readFileSync(p, "utf-8") + "\n})", p); | 
					
						
							|  |  |  | 										const m = { | 
					
						
							| 
									
										
										
										
											2017-11-12 01:48:29 +08:00
										 |  |  | 											exports: {}, | 
					
						
							|  |  |  | 											webpackTestSuiteModule: true | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 										}; | 
					
						
							| 
									
										
										
										
											2017-01-18 23:27:35 +08:00
										 |  |  | 										fn.call(m.exports, _require, m, m.exports, outputDirectory, _it); | 
					
						
							|  |  |  | 										return m.exports; | 
					
						
							| 
									
										
										
										
											2013-12-16 07:53:13 +08:00
										 |  |  | 									} else return require(module); | 
					
						
							|  |  |  | 								} | 
					
						
							| 
									
										
										
										
											2017-11-12 01:48:29 +08:00
										 |  |  | 								_require.webpackTestSuiteRequire = true; | 
					
						
							| 
									
										
										
										
											2013-12-16 07:53:13 +08:00
										 |  |  | 								_require("./bundle.js"); | 
					
						
							|  |  |  | 								if(exportedTest === 0) return done(new Error("No tests exported by test case")); | 
					
						
							|  |  |  | 								done(); | 
					
						
							|  |  |  | 							}); | 
					
						
							|  |  |  | 						}); | 
					
						
							| 
									
										
										
										
											2013-12-16 06:10:41 +08:00
										 |  |  | 					}); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2015-01-31 23:27:05 +08:00
										 |  |  | }); |