| 
									
										
										
										
											2017-01-18 23:39:17 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-23 02:40:42 +08:00
										 |  |  | /*globals describe it before after  */ | 
					
						
							| 
									
										
										
										
											2017-01-18 23:39:17 +08:00
										 |  |  | const should = require("should"); | 
					
						
							|  |  |  | const path = require("path"); | 
					
						
							|  |  |  | const fs = require("fs"); | 
					
						
							|  |  |  | const MemoryFs = require("memory-fs"); | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 23:39:17 +08:00
										 |  |  | const webpack = require("../"); | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 23:39:17 +08:00
										 |  |  | describe("WatchDetection", () => { | 
					
						
							|  |  |  | 	for(let changeTimeout = 0; changeTimeout < 100; changeTimeout += 10) { | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 		createTestCase(changeTimeout); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-18 23:39:17 +08:00
										 |  |  | 	for(let changeTimeout = 100; changeTimeout <= 2000; changeTimeout += 100) { | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 		createTestCase(changeTimeout); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function createTestCase(changeTimeout) { | 
					
						
							|  |  |  | 		describe("time between changes " + changeTimeout + "ms", function() { | 
					
						
							|  |  |  | 			this.timeout(10000); | 
					
						
							| 
									
										
										
										
											2017-01-18 23:39:17 +08:00
										 |  |  | 			const fixturePath = path.join(__dirname, "fixtures", "temp-" + changeTimeout); | 
					
						
							|  |  |  | 			const filePath = path.join(fixturePath, "file.js"); | 
					
						
							|  |  |  | 			const file2Path = path.join(fixturePath, "file2.js"); | 
					
						
							|  |  |  | 			const loaderPath = path.join(__dirname, "fixtures", "delay-loader.js"); | 
					
						
							|  |  |  | 			before(() => { | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 				try { | 
					
						
							|  |  |  | 					fs.mkdirSync(fixturePath); | 
					
						
							|  |  |  | 				} catch(e) {} | 
					
						
							|  |  |  | 				fs.writeFileSync(filePath, "require('./file2')", "utf-8"); | 
					
						
							|  |  |  | 				fs.writeFileSync(file2Path, "original", "utf-8"); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-01-18 23:39:17 +08:00
										 |  |  | 			after((done) => { | 
					
						
							|  |  |  | 				setTimeout(() => { | 
					
						
							| 
									
										
										
										
											2016-05-07 05:15:07 +08:00
										 |  |  | 					try { | 
					
						
							|  |  |  | 						fs.unlinkSync(filePath); | 
					
						
							|  |  |  | 					} catch(e) {} | 
					
						
							|  |  |  | 					try { | 
					
						
							|  |  |  | 						fs.unlinkSync(file2Path); | 
					
						
							|  |  |  | 					} catch(e) {} | 
					
						
							|  |  |  | 					try { | 
					
						
							|  |  |  | 						fs.rmdirSync(fixturePath); | 
					
						
							|  |  |  | 					} catch(e) {} | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}, 100); // cool down a bit
 | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-01-18 23:39:17 +08:00
										 |  |  | 			it("should build the bundle correctly", (done) => { | 
					
						
							|  |  |  | 				const compiler = webpack({ | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 					entry: loaderPath + "!" + filePath, | 
					
						
							|  |  |  | 					output: { | 
					
						
							|  |  |  | 						path: "/", | 
					
						
							|  |  |  | 						filename: "bundle.js" | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2017-01-18 23:39:17 +08:00
										 |  |  | 				const memfs = compiler.outputFileSystem = new MemoryFs(); | 
					
						
							|  |  |  | 				let onChange; | 
					
						
							|  |  |  | 				compiler.plugin("done", () => { | 
					
						
							|  |  |  | 					if(onChange) | 
					
						
							|  |  |  | 						onChange(); | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 23:39:17 +08:00
										 |  |  | 				let watcher; | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				step1(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				function step1() { | 
					
						
							| 
									
										
										
										
											2017-01-18 23:39:17 +08:00
										 |  |  | 					onChange = () => { | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 						if(memfs.readFileSync("/bundle.js") && memfs.readFileSync("/bundle.js").toString().indexOf("original") >= 0) | 
					
						
							|  |  |  | 							step2(); | 
					
						
							| 
									
										
										
										
											2017-01-18 23:39:17 +08:00
										 |  |  | 					}; | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 					watcher = compiler.watch({ | 
					
						
							|  |  |  | 						aggregateTimeout: 50 | 
					
						
							| 
									
										
										
										
											2017-01-18 23:39:17 +08:00
										 |  |  | 					}, () => {}); | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				function step2() { | 
					
						
							|  |  |  | 					onChange = null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					fs.writeFile(filePath, "require('./file2'); again", "utf-8", handleError); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					setTimeout(step3, changeTimeout); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				function step3() { | 
					
						
							|  |  |  | 					onChange = null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					fs.writeFile(file2Path, "wrong", "utf-8", handleError); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					setTimeout(step4, changeTimeout); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				function step4() { | 
					
						
							| 
									
										
										
										
											2017-01-18 23:39:17 +08:00
										 |  |  | 					onChange = () => { | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 						if(memfs.readFileSync("/bundle.js").toString().indexOf("correct") >= 0) | 
					
						
							|  |  |  | 							step4(); | 
					
						
							| 
									
										
										
										
											2017-01-18 23:39:17 +08:00
										 |  |  | 					}; | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 					fs.writeFile(file2Path, "correct", "utf-8", handleError); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				function step4() { | 
					
						
							|  |  |  | 					onChange = null; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 17:45:49 +08:00
										 |  |  | 					watcher.close(() => { | 
					
						
							|  |  |  | 						setTimeout(done, 1000); | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				function handleError(err) { | 
					
						
							|  |  |  | 					if(err) done(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-01-18 23:39:17 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | }); |