| 
									
										
										
										
											2017-12-29 18:23:14 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* globals describe it */ | 
					
						
							|  |  |  | const path = require("path"); | 
					
						
							|  |  |  | const MemoryFs = require("memory-fs"); | 
					
						
							|  |  |  | const webpack = require("../"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const createMultiCompiler = () => { | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 	const compiler = webpack([ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			context: path.join(__dirname, "fixtures"), | 
					
						
							|  |  |  | 			entry: "./a.js" | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			context: path.join(__dirname, "fixtures"), | 
					
						
							|  |  |  | 			entry: "./b.js" | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	]); | 
					
						
							| 
									
										
										
										
											2017-12-29 18:23:14 +08:00
										 |  |  | 	compiler.outputFileSystem = new MemoryFs(); | 
					
						
							|  |  |  | 	return compiler; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe("MultiCompiler", function() { | 
					
						
							| 
									
										
										
										
											2018-05-18 22:34:01 +08:00
										 |  |  | 	jest.setTimeout(20000); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-29 18:23:14 +08:00
										 |  |  | 	it("should trigger 'run' for each child compiler", done => { | 
					
						
							|  |  |  | 		const compiler = createMultiCompiler(); | 
					
						
							|  |  |  | 		let called = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		compiler.hooks.run.tap("MultiCompiler test", () => called++); | 
					
						
							|  |  |  | 		compiler.run(err => { | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 			if (err) { | 
					
						
							| 
									
										
										
										
											2017-12-29 18:23:14 +08:00
										 |  |  | 				throw err; | 
					
						
							|  |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2018-01-24 23:00:43 +08:00
										 |  |  | 				expect(called).toBe(2); | 
					
						
							| 
									
										
										
										
											2017-12-29 18:23:14 +08:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it("should trigger 'watchRun' for each child compiler", done => { | 
					
						
							|  |  |  | 		const compiler = createMultiCompiler(); | 
					
						
							|  |  |  | 		let called = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		compiler.hooks.watchRun.tap("MultiCompiler test", () => called++); | 
					
						
							|  |  |  | 		const watcher = compiler.watch(1000, err => { | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 			if (err) { | 
					
						
							| 
									
										
										
										
											2017-12-29 18:23:14 +08:00
										 |  |  | 				throw err; | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				watcher.close(); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:00:43 +08:00
										 |  |  | 				expect(called).toBe(2); | 
					
						
							| 
									
										
										
										
											2017-12-29 18:23:14 +08:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2018-03-07 18:45:38 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	it("should not be run twice at a time (run)", function(done) { | 
					
						
							|  |  |  | 		const compiler = createMultiCompiler(); | 
					
						
							|  |  |  | 		compiler.run((err, stats) => { | 
					
						
							|  |  |  | 			if (err) return done(err); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 		compiler.run((err, stats) => { | 
					
						
							|  |  |  | 			if (err) return done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	it("should not be run twice at a time (watch)", function(done) { | 
					
						
							|  |  |  | 		const compiler = createMultiCompiler(); | 
					
						
							| 
									
										
										
										
											2018-04-19 16:36:13 +08:00
										 |  |  | 		const watcher = compiler.watch({}, (err, stats) => { | 
					
						
							| 
									
										
										
										
											2018-03-07 18:45:38 +08:00
										 |  |  | 			if (err) return done(err); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 		compiler.watch({}, (err, stats) => { | 
					
						
							| 
									
										
										
										
											2018-04-19 16:36:13 +08:00
										 |  |  | 			if (err) return watcher.close(done); | 
					
						
							| 
									
										
										
										
											2018-03-07 18:45:38 +08:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	it("should not be run twice at a time (run - watch)", function(done) { | 
					
						
							|  |  |  | 		const compiler = createMultiCompiler(); | 
					
						
							|  |  |  | 		compiler.run((err, stats) => { | 
					
						
							|  |  |  | 			if (err) return done(err); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 		compiler.watch({}, (err, stats) => { | 
					
						
							|  |  |  | 			if (err) return done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	it("should not be run twice at a time (watch - run)", function(done) { | 
					
						
							|  |  |  | 		const compiler = createMultiCompiler(); | 
					
						
							| 
									
										
										
										
											2018-04-19 16:36:13 +08:00
										 |  |  | 		let watcher; | 
					
						
							|  |  |  | 		watcher = compiler.watch({}, (err, stats) => { | 
					
						
							| 
									
										
										
										
											2018-03-07 18:45:38 +08:00
										 |  |  | 			if (err) return done(err); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 		compiler.run((err, stats) => { | 
					
						
							| 
									
										
										
										
											2018-04-19 16:36:13 +08:00
										 |  |  | 			if (err) return watcher.close(done); | 
					
						
							| 
									
										
										
										
											2018-03-07 18:45:38 +08:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	it("should not be run twice at a time (instance cb)", function(done) { | 
					
						
							|  |  |  | 		const compiler = webpack( | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				context: __dirname, | 
					
						
							|  |  |  | 				mode: "production", | 
					
						
							|  |  |  | 				entry: "./c", | 
					
						
							|  |  |  | 				output: { | 
					
						
							|  |  |  | 					path: "/", | 
					
						
							|  |  |  | 					filename: "bundle.js" | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			() => {} | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 		compiler.outputFileSystem = new MemoryFs(); | 
					
						
							|  |  |  | 		compiler.run((err, stats) => { | 
					
						
							|  |  |  | 			if (err) return done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	it("should run again correctly after first compilation", function(done) { | 
					
						
							|  |  |  | 		const compiler = createMultiCompiler(); | 
					
						
							|  |  |  | 		compiler.run((err, stats) => { | 
					
						
							|  |  |  | 			if (err) return done(err); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			compiler.run((err, stats) => { | 
					
						
							|  |  |  | 				if (err) return done(err); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	it("should watch again correctly after first compilation", function(done) { | 
					
						
							|  |  |  | 		const compiler = createMultiCompiler(); | 
					
						
							|  |  |  | 		compiler.run((err, stats) => { | 
					
						
							|  |  |  | 			if (err) return done(err); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-19 16:36:13 +08:00
										 |  |  | 			let watcher; | 
					
						
							|  |  |  | 			watcher = compiler.watch({}, (err, stats) => { | 
					
						
							| 
									
										
										
										
											2018-03-07 18:45:38 +08:00
										 |  |  | 				if (err) return done(err); | 
					
						
							| 
									
										
										
										
											2018-04-19 16:36:13 +08:00
										 |  |  | 				watcher.close(done); | 
					
						
							| 
									
										
										
										
											2018-03-07 18:45:38 +08:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	it("should run again correctly after first closed watch", function(done) { | 
					
						
							|  |  |  | 		const compiler = createMultiCompiler(); | 
					
						
							|  |  |  | 		const watching = compiler.watch({}, (err, stats) => { | 
					
						
							|  |  |  | 			if (err) return done(err); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 		watching.close(() => { | 
					
						
							|  |  |  | 			compiler.run((err, stats) => { | 
					
						
							|  |  |  | 				if (err) return done(err); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	it("should watch again correctly after first closed watch", function(done) { | 
					
						
							|  |  |  | 		const compiler = createMultiCompiler(); | 
					
						
							|  |  |  | 		const watching = compiler.watch({}, (err, stats) => { | 
					
						
							|  |  |  | 			if (err) return done(err); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 		watching.close(() => { | 
					
						
							| 
									
										
										
										
											2018-04-19 16:36:13 +08:00
										 |  |  | 			let watcher; | 
					
						
							|  |  |  | 			watcher = compiler.watch({}, (err, stats) => { | 
					
						
							| 
									
										
										
										
											2018-03-07 18:45:38 +08:00
										 |  |  | 				if (err) return done(err); | 
					
						
							| 
									
										
										
										
											2018-04-19 16:36:13 +08:00
										 |  |  | 				watcher.close(done); | 
					
						
							| 
									
										
										
										
											2018-03-07 18:45:38 +08:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2017-12-29 18:23:14 +08:00
										 |  |  | }); |