| 
									
										
										
										
											2017-06-19 16:35:05 +08:00
										 |  |  | /* global describe, it */ | 
					
						
							| 
									
										
										
										
											2017-01-18 21:53:20 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2013-02-14 00:00:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 21:08:11 +08:00
										 |  |  | require("should"); | 
					
						
							| 
									
										
										
										
											2013-02-14 00:00:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 21:53:20 +08:00
										 |  |  | const path = require("path"); | 
					
						
							|  |  |  | const webpack = require("../lib/webpack"); | 
					
						
							| 
									
										
										
										
											2013-02-14 00:00:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 21:53:20 +08:00
										 |  |  | describe("NodeTemplatePlugin", () => { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it("should compile and run a simple module", (done) => { | 
					
						
							| 
									
										
										
										
											2013-02-14 00:00:07 +08:00
										 |  |  | 		webpack({ | 
					
						
							| 
									
										
										
										
											2017-11-21 17:41:01 +08:00
										 |  |  | 			mode: "production", | 
					
						
							| 
									
										
										
										
											2013-02-14 00:00:07 +08:00
										 |  |  | 			context: path.join(__dirname, "fixtures", "nodetest"), | 
					
						
							|  |  |  | 			target: "node", | 
					
						
							|  |  |  | 			output: { | 
					
						
							|  |  |  | 				path: path.join(__dirname, "js"), | 
					
						
							|  |  |  | 				filename: "result.js", | 
					
						
							| 
									
										
										
										
											2013-06-28 20:20:11 +08:00
										 |  |  | 				chunkFilename: "[hash].result.[id].js", | 
					
						
							|  |  |  | 				library: "abc", | 
					
						
							|  |  |  | 				libraryTarget: "commonjs", | 
					
						
							| 
									
										
										
										
											2013-02-14 00:00:07 +08:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2017-12-13 23:05:21 +08:00
										 |  |  | 			entry: "./entry" | 
					
						
							| 
									
										
										
										
											2017-01-18 21:53:20 +08:00
										 |  |  | 		}, (err, stats) => { | 
					
						
							| 
									
										
										
										
											2013-02-14 00:00:07 +08:00
										 |  |  | 			if(err) return err; | 
					
						
							| 
									
										
										
										
											2017-01-02 08:44:24 +08:00
										 |  |  | 			stats.hasErrors().should.be.not.ok(); | 
					
						
							|  |  |  | 			stats.hasWarnings().should.be.not.ok(); | 
					
						
							| 
									
										
										
										
											2017-11-16 14:06:30 +08:00
										 |  |  | 			// eslint-disable-next-line node/no-missing-require
 | 
					
						
							| 
									
										
										
										
											2017-01-18 21:53:20 +08:00
										 |  |  | 			const result = require("./js/result").abc; | 
					
						
							| 
									
										
										
										
											2013-02-14 00:00:07 +08:00
										 |  |  | 			result.nextTick.should.be.equal(process.nextTick); | 
					
						
							|  |  |  | 			result.fs.should.be.equal(require("fs")); | 
					
						
							| 
									
										
										
										
											2017-01-18 21:53:20 +08:00
										 |  |  | 			result.loadChunk(456, (chunk) => { | 
					
						
							| 
									
										
										
										
											2013-02-14 00:00:07 +08:00
										 |  |  | 				chunk.should.be.eql(123); | 
					
						
							| 
									
										
										
										
											2017-01-18 21:53:20 +08:00
										 |  |  | 				result.loadChunk(567, (chunk) => { | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 					chunk.should.be.eql({ | 
					
						
							|  |  |  | 						a: 1 | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2013-02-14 00:00:07 +08:00
										 |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2013-06-28 20:20:11 +08:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 21:53:20 +08:00
										 |  |  | 	it("should compile and run a simple module in single mode", (done) => { | 
					
						
							| 
									
										
										
										
											2013-06-28 20:20:11 +08:00
										 |  |  | 		webpack({ | 
					
						
							| 
									
										
										
										
											2017-11-21 17:41:01 +08:00
										 |  |  | 			mode: "production", | 
					
						
							| 
									
										
										
										
											2013-06-28 20:20:11 +08:00
										 |  |  | 			context: path.join(__dirname, "fixtures", "nodetest"), | 
					
						
							|  |  |  | 			target: "node", | 
					
						
							|  |  |  | 			output: { | 
					
						
							|  |  |  | 				path: path.join(__dirname, "js"), | 
					
						
							|  |  |  | 				filename: "result2.js", | 
					
						
							|  |  |  | 				chunkFilename: "[hash].result2.[id].js", | 
					
						
							|  |  |  | 				library: "def", | 
					
						
							|  |  |  | 				libraryTarget: "umd", | 
					
						
							| 
									
										
										
										
											2016-02-27 06:50:04 +08:00
										 |  |  | 				auxiliaryComment: "test" | 
					
						
							| 
									
										
										
										
											2013-06-28 20:20:11 +08:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			entry: "./entry", | 
					
						
							| 
									
										
										
										
											2013-12-18 06:21:49 +08:00
										 |  |  | 			plugins: [ | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 				new webpack.optimize.LimitChunkCountPlugin({ | 
					
						
							|  |  |  | 					maxChunks: 1 | 
					
						
							| 
									
										
										
										
											2017-12-13 23:05:21 +08:00
										 |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2013-12-18 06:21:49 +08:00
										 |  |  | 			] | 
					
						
							| 
									
										
										
										
											2017-01-18 21:53:20 +08:00
										 |  |  | 		}, (err, stats) => { | 
					
						
							| 
									
										
										
										
											2013-06-28 20:20:11 +08:00
										 |  |  | 			if(err) return err; | 
					
						
							| 
									
										
										
										
											2017-01-02 08:44:24 +08:00
										 |  |  | 			stats.hasErrors().should.be.not.ok(); | 
					
						
							| 
									
										
										
										
											2017-11-16 14:06:30 +08:00
										 |  |  | 			// eslint-disable-next-line node/no-missing-require
 | 
					
						
							| 
									
										
										
										
											2017-01-18 21:53:20 +08:00
										 |  |  | 			const result = require("./js/result2"); | 
					
						
							| 
									
										
										
										
											2013-06-28 20:20:11 +08:00
										 |  |  | 			result.nextTick.should.be.equal(process.nextTick); | 
					
						
							|  |  |  | 			result.fs.should.be.equal(require("fs")); | 
					
						
							| 
									
										
										
										
											2017-01-18 21:53:20 +08:00
										 |  |  | 			const sameTick = true; | 
					
						
							|  |  |  | 			result.loadChunk(456, (chunk) => { | 
					
						
							| 
									
										
										
										
											2013-06-28 20:20:11 +08:00
										 |  |  | 				chunk.should.be.eql(123); | 
					
						
							|  |  |  | 				sameTick.should.be.eql(true); | 
					
						
							| 
									
										
										
										
											2017-01-18 21:53:20 +08:00
										 |  |  | 				result.loadChunk(567, (chunk) => { | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 					chunk.should.be.eql({ | 
					
						
							|  |  |  | 						a: 1 | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2013-06-28 20:20:11 +08:00
										 |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2013-02-14 00:00:07 +08:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }); |