| 
									
										
										
										
											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"); | 
					
						
							| 
									
										
										
										
											2016-12-29 12:51:24 +08:00
										 |  |  | const ModuleFilenameHelpers = require("./ModuleFilenameHelpers"); | 
					
						
							| 
									
										
										
										
											2017-08-02 21:39:43 +08:00
										 |  |  | const Template = require("./Template"); | 
					
						
							| 
									
										
										
										
											2013-05-13 05:16:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 17:19:28 +08:00
										 |  |  | const validateOptions = require("schema-utils"); | 
					
						
							| 
									
										
										
										
											2017-11-11 20:05:55 +08:00
										 |  |  | const schema = require("../schemas/plugins/BannerPlugin.json"); | 
					
						
							| 
									
										
										
										
											2017-10-28 05:23:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +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") | 
					
						
							|  |  |  | 		.join("\n * ")}\n */`; | 
					
						
							| 
									
										
										
										
											2017-04-12 07:54:08 +08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2013-05-13 05:16:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-29 12:51:24 +08:00
										 |  |  | class BannerPlugin { | 
					
						
							| 
									
										
										
										
											2018-07-06 17:19:30 +08:00
										 |  |  | 	constructor(options = {}) { | 
					
						
							| 
									
										
										
										
											2017-11-11 20:05:55 +08:00
										 |  |  | 		validateOptions(schema, options, "Banner Plugin"); | 
					
						
							| 
									
										
										
										
											2017-10-28 05:23:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-06 17:19:30 +08:00
										 |  |  | 		this.options = options; | 
					
						
							| 
									
										
										
										
											2018-05-10 22:36:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (typeof options.banner === "function") { | 
					
						
							| 
									
										
										
										
											2018-05-11 15:46:16 +08:00
										 |  |  | 			const getBanner = this.options.banner; | 
					
						
							| 
									
										
										
										
											2018-05-10 22:36:08 +08:00
										 |  |  | 			this.banner = this.options.raw | 
					
						
							|  |  |  | 				? getBanner | 
					
						
							|  |  |  | 				: data => wrapComment(getBanner(data)); | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2018-05-11 15:46:16 +08:00
										 |  |  | 			const banner = this.options.raw | 
					
						
							| 
									
										
										
										
											2018-05-10 22:36:08 +08:00
										 |  |  | 				? this.options.banner | 
					
						
							|  |  |  | 				: wrapComment(this.options.banner); | 
					
						
							|  |  |  | 			this.banner = () => banner; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-12-29 12:51:24 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-05-09 08:01:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		compiler.hooks.compilation.tap("BannerPlugin", compilation => { | 
					
						
							|  |  |  | 			compilation.hooks.optimizeChunkAssets.tap("BannerPlugin", chunks => { | 
					
						
							|  |  |  | 				for (const chunk of chunks) { | 
					
						
							|  |  |  | 					if (options.entryOnly && !chunk.canBeInitial()) { | 
					
						
							| 
									
										
										
										
											2018-01-22 20:52:43 +08:00
										 |  |  | 						continue; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					for (const file of chunk.files) { | 
					
						
							|  |  |  | 						if (!matchObject(file)) { | 
					
						
							| 
									
										
										
										
											2018-01-22 20:52:43 +08:00
										 |  |  | 							continue; | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						let basename; | 
					
						
							|  |  |  | 						let query = ""; | 
					
						
							|  |  |  | 						let filename = file; | 
					
						
							|  |  |  | 						const hash = compilation.hash; | 
					
						
							|  |  |  | 						const querySplit = filename.indexOf("?"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						if (querySplit >= 0) { | 
					
						
							| 
									
										
										
										
											2018-01-22 20:52:43 +08:00
										 |  |  | 							query = filename.substr(querySplit); | 
					
						
							|  |  |  | 							filename = filename.substr(0, querySplit); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						const lastSlashIndex = filename.lastIndexOf("/"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						if (lastSlashIndex === -1) { | 
					
						
							| 
									
										
										
										
											2018-01-22 20:52:43 +08:00
										 |  |  | 							basename = filename; | 
					
						
							|  |  |  | 						} else { | 
					
						
							|  |  |  | 							basename = filename.substr(lastSlashIndex + 1); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 22:36:08 +08:00
										 |  |  | 						const data = { | 
					
						
							| 
									
										
										
										
											2018-01-22 20:52:43 +08:00
										 |  |  | 							hash, | 
					
						
							|  |  |  | 							chunk, | 
					
						
							|  |  |  | 							filename, | 
					
						
							|  |  |  | 							basename, | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 							query | 
					
						
							| 
									
										
										
										
											2018-05-10 22:36:08 +08:00
										 |  |  | 						}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						const comment = compilation.getPath(banner(data), data); | 
					
						
							| 
									
										
										
										
											2018-01-22 20:52:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						compilation.assets[file] = new ConcatSource( | 
					
						
							|  |  |  | 							comment, | 
					
						
							|  |  |  | 							"\n", | 
					
						
							|  |  |  | 							compilation.assets[file] | 
					
						
							|  |  |  | 						); | 
					
						
							| 
									
										
										
										
											2018-01-22 20:52:43 +08:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-05-13 05:16:22 +08:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-12-29 12:51:24 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = BannerPlugin; |