| 
									
										
										
										
											2017-01-18 21:53:20 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2013-02-14 00:00:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-15 20:51:52 +08:00
										 |  |  | require("./helpers/warmup-webpack"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 21:53:20 +08:00
										 |  |  | const path = require("path"); | 
					
						
							| 
									
										
										
										
											2013-02-14 00:00:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-18 17:38:06 +08:00
										 |  |  | // cspell:word nodetest
 | 
					
						
							| 
									
										
										
										
											2017-01-18 21:53:20 +08:00
										 |  |  | describe("NodeTemplatePlugin", () => { | 
					
						
							| 
									
										
										
										
											2018-11-05 13:46:46 +08:00
										 |  |  | 	jest.setTimeout(20000); | 
					
						
							| 
									
										
										
										
											2025-07-02 20:10:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 	it("should compile and run a simple module", (done) => { | 
					
						
							| 
									
										
										
										
											2021-07-15 20:51:52 +08:00
										 |  |  | 		const webpack = require(".."); | 
					
						
							| 
									
										
										
										
											2025-07-02 20:10:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 		webpack( | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				mode: "production", | 
					
						
							|  |  |  | 				context: path.join(__dirname, "fixtures", "nodetest"), | 
					
						
							|  |  |  | 				target: "node", | 
					
						
							|  |  |  | 				output: { | 
					
						
							|  |  |  | 					path: path.join(__dirname, "js", "NodeTemplatePlugin"), | 
					
						
							|  |  |  | 					filename: "result.js", | 
					
						
							| 
									
										
										
										
											2018-12-28 21:09:35 +08:00
										 |  |  | 					chunkFilename: "[fullhash].result.[id].js", | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 					library: "abc", | 
					
						
							|  |  |  | 					libraryTarget: "commonjs" | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				entry: "./entry" | 
					
						
							| 
									
										
										
										
											2013-02-14 00:00:07 +08:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 			(err, stats) => { | 
					
						
							|  |  |  | 				if (err) return err; | 
					
						
							|  |  |  | 				expect(stats.hasErrors()).toBe(false); | 
					
						
							|  |  |  | 				expect(stats.hasWarnings()).toBe(false); | 
					
						
							| 
									
										
										
										
											2025-07-02 20:10:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 				const result = require("./js/NodeTemplatePlugin/result").abc; | 
					
						
							| 
									
										
										
										
											2025-07-02 20:10:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 				expect(result.nextTick).toBe(process.nextTick); | 
					
						
							|  |  |  | 				expect(result.fs).toBe(require("fs")); | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 				result.loadChunk(456, (chunk) => { | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 					expect(chunk).toBe(123); | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 					result.loadChunk(567, (chunk) => { | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 						expect(chunk).toEqual({ | 
					
						
							|  |  |  | 							a: 1 | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 						done(); | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2013-02-14 00:00:07 +08:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2013-06-28 20:20:11 +08:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 	it("should compile and run a simple module in single mode", (done) => { | 
					
						
							| 
									
										
										
										
											2021-07-15 20:51:52 +08:00
										 |  |  | 		const webpack = require(".."); | 
					
						
							| 
									
										
										
										
											2025-07-02 20:10:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 		webpack( | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				mode: "production", | 
					
						
							|  |  |  | 				context: path.join(__dirname, "fixtures", "nodetest"), | 
					
						
							|  |  |  | 				target: "node", | 
					
						
							|  |  |  | 				output: { | 
					
						
							|  |  |  | 					path: path.join(__dirname, "js", "NodeTemplatePluginSingle"), | 
					
						
							|  |  |  | 					filename: "result2.js", | 
					
						
							| 
									
										
										
										
											2018-12-28 21:09:35 +08:00
										 |  |  | 					chunkFilename: "[fullhash].result2.[id].js", | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 					library: "def", | 
					
						
							|  |  |  | 					libraryTarget: "umd", | 
					
						
							|  |  |  | 					auxiliaryComment: "test" | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				entry: "./entry", | 
					
						
							|  |  |  | 				plugins: [ | 
					
						
							|  |  |  | 					new webpack.optimize.LimitChunkCountPlugin({ | 
					
						
							|  |  |  | 						maxChunks: 1 | 
					
						
							|  |  |  | 					}) | 
					
						
							|  |  |  | 				] | 
					
						
							| 
									
										
										
										
											2013-06-28 20:20:11 +08:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 			(err, stats) => { | 
					
						
							|  |  |  | 				if (err) return err; | 
					
						
							|  |  |  | 				expect(stats.hasErrors()).toBe(false); | 
					
						
							| 
									
										
										
										
											2025-07-02 20:10:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 				const result = require("./js/NodeTemplatePluginSingle/result2"); | 
					
						
							| 
									
										
										
										
											2025-07-02 20:10:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 				expect(result.nextTick).toBe(process.nextTick); | 
					
						
							|  |  |  | 				expect(result.fs).toBe(require("fs")); | 
					
						
							|  |  |  | 				const sameTick = true; | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 				result.loadChunk(456, (chunk) => { | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 					expect(chunk).toBe(123); | 
					
						
							|  |  |  | 					expect(sameTick).toBe(true); | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 					result.loadChunk(567, (chunk) => { | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 						expect(chunk).toEqual({ | 
					
						
							|  |  |  | 							a: 1 | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 						done(); | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2013-06-28 20:20:11 +08:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2013-02-14 00:00:07 +08:00
										 |  |  | 	}); | 
					
						
							|  |  |  | }); |