| 
									
										
										
										
											2022-03-02 01:22:07 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const path = require("path"); | 
					
						
							|  |  |  | const fs = require("graceful-fs"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const webpack = require(".."); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 02:29:34 +08:00
										 |  |  | const pluginDir = path.join(__dirname, "js", "BannerPlugin"); | 
					
						
							|  |  |  | const outputDir = path.join(pluginDir, "output"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-02 20:10:54 +08:00
										 |  |  | describe("BannerPlugin", () => { | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 	it("should cache assets", (done) => { | 
					
						
							| 
									
										
										
										
											2025-07-02 20:10:54 +08:00
										 |  |  | 		const entry1File = path.join(pluginDir, "entry1.js"); | 
					
						
							|  |  |  | 		const entry2File = path.join(pluginDir, "entry2.js"); | 
					
						
							|  |  |  | 		const outputFile = path.join(outputDir, "entry1.js"); | 
					
						
							|  |  |  | 		try { | 
					
						
							|  |  |  | 			fs.mkdirSync(path.join(pluginDir), { | 
					
						
							|  |  |  | 				recursive: true | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} catch (_err) { | 
					
						
							|  |  |  | 			// empty
 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		const compiler = webpack({ | 
					
						
							|  |  |  | 			mode: "development", | 
					
						
							|  |  |  | 			entry: { | 
					
						
							|  |  |  | 				entry1: entry1File, | 
					
						
							|  |  |  | 				entry2: entry2File | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			output: { | 
					
						
							|  |  |  | 				path: outputDir | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			plugins: [new webpack.BannerPlugin("banner is a string")] | 
					
						
							| 
									
										
										
										
											2022-03-02 01:22:07 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2025-07-02 20:10:54 +08:00
										 |  |  | 		fs.writeFileSync(entry1File, "1", "utf8"); | 
					
						
							|  |  |  | 		fs.writeFileSync(entry2File, "1", "utf8"); | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 		compiler.run((err) => { | 
					
						
							| 
									
										
										
										
											2025-07-02 20:10:54 +08:00
										 |  |  | 			if (err) return done(err); | 
					
						
							|  |  |  | 			const footerFileResults = fs.readFileSync(outputFile, "utf8").split("\n"); | 
					
						
							|  |  |  | 			expect(footerFileResults[0]).toBe("/*! banner is a string */"); | 
					
						
							|  |  |  | 			fs.writeFileSync(entry2File, "2", "utf8"); | 
					
						
							|  |  |  | 			compiler.run((err, stats) => { | 
					
						
							|  |  |  | 				const { assets } = stats.toJson(); | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 				expect(assets.find((as) => as.name === "entry1.js").emitted).toBe( | 
					
						
							|  |  |  | 					false | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 				expect(assets.find((as) => as.name === "entry2.js").emitted).toBe(true); | 
					
						
							| 
									
										
										
										
											2025-07-02 20:10:54 +08:00
										 |  |  | 				done(err); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2022-03-02 01:22:07 +08:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2022-04-02 02:29:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 	it("can place banner as footer", (done) => { | 
					
						
							| 
									
										
										
										
											2025-07-02 20:10:54 +08:00
										 |  |  | 		const footerFile = path.join(pluginDir, "footerFile.js"); | 
					
						
							|  |  |  | 		const outputFile = path.join(outputDir, "footerFile.js"); | 
					
						
							|  |  |  | 		try { | 
					
						
							|  |  |  | 			fs.mkdirSync(path.join(pluginDir), { | 
					
						
							|  |  |  | 				recursive: true | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} catch (_err) { | 
					
						
							|  |  |  | 			// empty
 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		const compiler = webpack({ | 
					
						
							|  |  |  | 			mode: "development", | 
					
						
							|  |  |  | 			entry: { | 
					
						
							|  |  |  | 				footerFile | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			output: { | 
					
						
							|  |  |  | 				path: outputDir | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			plugins: [ | 
					
						
							|  |  |  | 				new webpack.BannerPlugin({ | 
					
						
							|  |  |  | 					banner: "banner is a string", | 
					
						
							|  |  |  | 					footer: true | 
					
						
							|  |  |  | 				}) | 
					
						
							|  |  |  | 			] | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 		fs.writeFileSync(footerFile, "footer", "utf8"); | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 		compiler.run((err) => { | 
					
						
							| 
									
										
										
										
											2025-07-02 20:10:54 +08:00
										 |  |  | 			if (err) return done(err); | 
					
						
							|  |  |  | 			const footerFileResults = fs.readFileSync(outputFile, "utf8").split("\n"); | 
					
						
							|  |  |  | 			expect(footerFileResults.pop()).toBe("/*! banner is a string */"); | 
					
						
							|  |  |  | 			done(); | 
					
						
							| 
									
										
										
										
											2022-04-02 02:29:34 +08:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2024-03-16 02:06:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 	it("should allow to change stage", (done) => { | 
					
						
							| 
									
										
										
										
											2025-07-02 20:10:54 +08:00
										 |  |  | 		const entryFile = path.join(pluginDir, "entry3.js"); | 
					
						
							|  |  |  | 		const outputFile = path.join(outputDir, "entry3.js"); | 
					
						
							|  |  |  | 		try { | 
					
						
							|  |  |  | 			fs.mkdirSync(path.join(pluginDir), { | 
					
						
							|  |  |  | 				recursive: true | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} catch (_err) { | 
					
						
							|  |  |  | 			// empty
 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		const compiler = webpack({ | 
					
						
							|  |  |  | 			mode: "production", | 
					
						
							|  |  |  | 			entry: { | 
					
						
							|  |  |  | 				entry3: entryFile | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			output: { | 
					
						
							|  |  |  | 				path: outputDir | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			plugins: [ | 
					
						
							|  |  |  | 				new webpack.BannerPlugin({ | 
					
						
							|  |  |  | 					raw: true, | 
					
						
							|  |  |  | 					banner: "/* banner is a string */", | 
					
						
							|  |  |  | 					stage: webpack.Compilation.PROCESS_ASSETS_STAGE_REPORT | 
					
						
							|  |  |  | 				}) | 
					
						
							|  |  |  | 			] | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 		fs.writeFileSync(entryFile, "console.log(1 + 1);", "utf8"); | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 		compiler.run((err) => { | 
					
						
							| 
									
										
										
										
											2025-07-02 20:10:54 +08:00
										 |  |  | 			if (err) return done(err); | 
					
						
							|  |  |  | 			const fileResult = fs.readFileSync(outputFile, "utf8").split("\n"); | 
					
						
							|  |  |  | 			expect(fileResult[0]).toBe("/* banner is a string */"); | 
					
						
							|  |  |  | 			done(); | 
					
						
							| 
									
										
										
										
											2024-03-16 02:06:50 +08:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }); |