| 
									
										
										
										
											2013-05-13 05:16:22 +08:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2016-12-29 12:51:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 19:05:58 +08:00
										 |  |  | const { ConcatSource } = require("webpack-sources"); | 
					
						
							| 
									
										
										
										
											2020-05-12 18:16:51 +08:00
										 |  |  | const Compilation = require("./Compilation"); | 
					
						
							| 
									
										
										
										
											2016-12-29 12:51:24 +08:00
										 |  |  | const ModuleFilenameHelpers = require("./ModuleFilenameHelpers"); | 
					
						
							| 
									
										
										
										
											2017-08-02 21:39:43 +08:00
										 |  |  | const Template = require("./Template"); | 
					
						
							| 
									
										
										
										
											2021-04-16 21:35:18 +08:00
										 |  |  | const createSchemaValidation = require("./util/create-schema-validation"); | 
					
						
							| 
									
										
										
										
											2017-10-28 05:23:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-22 08:59:10 +08:00
										 |  |  | /** @typedef {import("../declarations/plugins/BannerPlugin").BannerFunction} BannerFunction */ | 
					
						
							| 
									
										
										
										
											2018-09-20 16:13:55 +08:00
										 |  |  | /** @typedef {import("../declarations/plugins/BannerPlugin").BannerPluginArgument} BannerPluginArgument */ | 
					
						
							|  |  |  | /** @typedef {import("../declarations/plugins/BannerPlugin").BannerPluginOptions} BannerPluginOptions */ | 
					
						
							| 
									
										
										
										
											2024-08-15 02:38:08 +08:00
										 |  |  | /** @typedef {import("./Compilation").PathData} PathData */ | 
					
						
							| 
									
										
										
										
											2018-08-23 01:23:48 +08:00
										 |  |  | /** @typedef {import("./Compiler")} Compiler */ | 
					
						
							| 
									
										
										
										
											2024-08-15 02:38:08 +08:00
										 |  |  | /** @typedef {import("./TemplatedPathPlugin").TemplatePath} TemplatePath */ | 
					
						
							| 
									
										
										
										
											2018-08-23 01:23:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-16 21:35:18 +08:00
										 |  |  | const validate = createSchemaValidation( | 
					
						
							| 
									
										
										
										
											2025-03-12 09:56:14 +08:00
										 |  |  | 	/** @type {((value: typeof import("../schemas/plugins/BannerPlugin.json")) => boolean)} */ | 
					
						
							| 
									
										
										
										
											2025-07-02 20:10:54 +08:00
										 |  |  | 	(require("../schemas/plugins/BannerPlugin.check")), | 
					
						
							| 
									
										
										
										
											2021-04-16 21:35:18 +08:00
										 |  |  | 	() => require("../schemas/plugins/BannerPlugin.json"), | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		name: "Banner Plugin", | 
					
						
							|  |  |  | 		baseDataPath: "options" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-26 02:31:28 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @param {string} str string to wrap | 
					
						
							|  |  |  |  * @returns {string} wrapped string | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | const wrapComment = (str) => { | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 	if (!str.includes("\n")) { | 
					
						
							|  |  |  | 		return Template.toComment(str); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 	return `/*!\n * ${str | 
					
						
							|  |  |  | 		.replace(/\*\//g, "* /") | 
					
						
							|  |  |  | 		.split("\n") | 
					
						
							| 
									
										
										
										
											2019-09-29 18:31:16 +08:00
										 |  |  | 		.join("\n * ") | 
					
						
							|  |  |  | 		.replace(/\s+\n/g, "\n") | 
					
						
							| 
									
										
										
										
											2022-08-31 17:56:03 +08:00
										 |  |  | 		.trimEnd()}\n */`; | 
					
						
							| 
									
										
										
										
											2017-04-12 07:54:08 +08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2013-05-13 05:16:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-23 20:03:37 +08:00
										 |  |  | const PLUGIN_NAME = "BannerPlugin"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-29 12:51:24 +08:00
										 |  |  | class BannerPlugin { | 
					
						
							| 
									
										
										
										
											2018-09-20 16:13:55 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {BannerPluginArgument} options options object | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2016-12-29 12:51:24 +08:00
										 |  |  | 	constructor(options) { | 
					
						
							| 
									
										
										
										
											2018-05-10 22:36:08 +08:00
										 |  |  | 		if (typeof options === "string" || typeof options === "function") { | 
					
						
							| 
									
										
										
										
											2016-12-29 12:51:24 +08:00
										 |  |  | 			options = { | 
					
						
							|  |  |  | 				banner: options | 
					
						
							|  |  |  | 			}; | 
					
						
							| 
									
										
										
										
											2018-05-10 22:36:08 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-16 21:35:18 +08:00
										 |  |  | 		validate(options); | 
					
						
							| 
									
										
										
										
											2019-10-15 19:56:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-06 17:19:30 +08:00
										 |  |  | 		this.options = options; | 
					
						
							| 
									
										
										
										
											2018-05-10 22:36:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-20 16:13:55 +08:00
										 |  |  | 		const bannerOption = options.banner; | 
					
						
							|  |  |  | 		if (typeof bannerOption === "function") { | 
					
						
							|  |  |  | 			const getBanner = bannerOption; | 
					
						
							| 
									
										
										
										
											2024-08-15 02:38:08 +08:00
										 |  |  | 			/** @type {BannerFunction} */ | 
					
						
							| 
									
										
										
										
											2018-05-10 22:36:08 +08:00
										 |  |  | 			this.banner = this.options.raw | 
					
						
							|  |  |  | 				? getBanner | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 				: /** @type {BannerFunction} */ (data) => wrapComment(getBanner(data)); | 
					
						
							| 
									
										
										
										
											2018-05-10 22:36:08 +08:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2018-05-11 15:46:16 +08:00
										 |  |  | 			const banner = this.options.raw | 
					
						
							| 
									
										
										
										
											2018-09-20 16:13:55 +08:00
										 |  |  | 				? bannerOption | 
					
						
							|  |  |  | 				: wrapComment(bannerOption); | 
					
						
							| 
									
										
										
										
											2024-08-15 02:38:08 +08:00
										 |  |  | 			/** @type {BannerFunction} */ | 
					
						
							| 
									
										
										
										
											2018-05-10 22:36:08 +08:00
										 |  |  | 			this.banner = () => banner; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-12-29 12:51:24 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-05-09 08:01:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 01:23:48 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2020-04-23 16:48:36 +08:00
										 |  |  | 	 * Apply the plugin | 
					
						
							|  |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							| 
									
										
										
										
											2018-08-23 01:23:48 +08:00
										 |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2016-12-29 12:51:24 +08:00
										 |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2017-04-14 01:25:04 +08:00
										 |  |  | 		const options = this.options; | 
					
						
							|  |  |  | 		const banner = this.banner; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		const matchObject = ModuleFilenameHelpers.matchObject.bind( | 
					
						
							|  |  |  | 			undefined, | 
					
						
							|  |  |  | 			options | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2022-03-02 01:22:07 +08:00
										 |  |  | 		const cache = new WeakMap(); | 
					
						
							| 
									
										
										
										
											2024-03-16 01:55:59 +08:00
										 |  |  | 		const stage = | 
					
						
							|  |  |  | 			this.options.stage || Compilation.PROCESS_ASSETS_STAGE_ADDITIONS; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 		compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => { | 
					
						
							| 
									
										
										
										
											2025-04-23 20:03:37 +08:00
										 |  |  | 			compilation.hooks.processAssets.tap({ name: PLUGIN_NAME, stage }, () => { | 
					
						
							|  |  |  | 				for (const chunk of compilation.chunks) { | 
					
						
							|  |  |  | 					if (options.entryOnly && !chunk.canBeInitial()) { | 
					
						
							|  |  |  | 						continue; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					for (const file of chunk.files) { | 
					
						
							|  |  |  | 						if (!matchObject(file)) { | 
					
						
							| 
									
										
										
										
											2018-01-22 20:52:43 +08:00
										 |  |  | 							continue; | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-23 20:03:37 +08:00
										 |  |  | 						/** @type {PathData} */ | 
					
						
							|  |  |  | 						const data = { chunk, filename: file }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						const comment = compilation.getPath( | 
					
						
							|  |  |  | 							/** @type {TemplatePath} */ | 
					
						
							|  |  |  | 							(banner), | 
					
						
							|  |  |  | 							data | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 						compilation.updateAsset(file, (old) => { | 
					
						
							| 
									
										
										
										
											2025-04-23 20:03:37 +08:00
										 |  |  | 							const cached = cache.get(old); | 
					
						
							|  |  |  | 							if (!cached || cached.comment !== comment) { | 
					
						
							|  |  |  | 								const source = options.footer | 
					
						
							|  |  |  | 									? new ConcatSource(old, "\n", comment) | 
					
						
							|  |  |  | 									: new ConcatSource(comment, "\n", old); | 
					
						
							|  |  |  | 								cache.set(old, { source, comment }); | 
					
						
							|  |  |  | 								return source; | 
					
						
							| 
									
										
										
										
											2020-05-12 18:16:51 +08:00
										 |  |  | 							} | 
					
						
							| 
									
										
										
										
											2025-04-23 20:03:37 +08:00
										 |  |  | 							return cached.source; | 
					
						
							|  |  |  | 						}); | 
					
						
							| 
									
										
										
										
											2018-01-22 20:52:43 +08:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2025-04-23 20:03:37 +08:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-05-13 05:16:22 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-12-29 12:51:24 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = BannerPlugin; |