| 
									
										
										
										
											2020-08-05 05:42:29 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Ivan Kopeykin @vankop | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const RuntimeGlobals = require("./RuntimeGlobals"); | 
					
						
							| 
									
										
										
										
											2020-08-07 01:58:37 +08:00
										 |  |  | const SimpleRuntimeModule = require("./SimpleRuntimeModule"); | 
					
						
							| 
									
										
										
										
											2020-08-05 05:42:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** @typedef {import("../declarations/WebpackOptions").Target} Target */ | 
					
						
							|  |  |  | /** @typedef {import("./Compiler")} Compiler */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class BaseURIPlugin { | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2020-08-07 01:58:37 +08:00
										 |  |  | 	 * @param {string=} baseURISource baseURI source | 
					
						
							| 
									
										
										
										
											2020-08-05 05:42:29 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2020-08-07 01:58:37 +08:00
										 |  |  | 	constructor(baseURISource) { | 
					
						
							|  |  |  | 		this.baseURISource = baseURISource; | 
					
						
							| 
									
										
										
										
											2020-08-05 05:42:29 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {Compiler} compiler compiler | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2020-08-07 01:58:37 +08:00
										 |  |  | 		let baseURISource = this.baseURISource; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (!baseURISource) { | 
					
						
							|  |  |  | 			const target = compiler.options.target; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			switch (target) { | 
					
						
							|  |  |  | 				case "webworker": | 
					
						
							|  |  |  | 					baseURISource = "self.location"; | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				case "node": | 
					
						
							|  |  |  | 				case "async-node": | 
					
						
							|  |  |  | 				case "node-webkit": | 
					
						
							|  |  |  | 				case "electron-main": | 
					
						
							|  |  |  | 				case "electron-preload": | 
					
						
							|  |  |  | 					baseURISource = "require('url').pathToFileURL(__filename)"; | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				case "web": | 
					
						
							|  |  |  | 				case "electron-renderer": | 
					
						
							|  |  |  | 					baseURISource = "document.baseURI"; | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (!baseURISource) return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-05 05:42:29 +08:00
										 |  |  | 		compiler.hooks.compilation.tap("BaseURIPlugin", compilation => { | 
					
						
							|  |  |  | 			compilation.hooks.runtimeRequirementInTree | 
					
						
							|  |  |  | 				.for(RuntimeGlobals.baseURI) | 
					
						
							| 
									
										
										
										
											2020-08-07 01:58:37 +08:00
										 |  |  | 				.tap("BaseURIPlugin", chunk => { | 
					
						
							| 
									
										
										
										
											2020-08-05 05:42:29 +08:00
										 |  |  | 					compilation.addRuntimeModule( | 
					
						
							|  |  |  | 						chunk, | 
					
						
							| 
									
										
										
										
											2020-08-07 01:58:37 +08:00
										 |  |  | 						new SimpleRuntimeModule( | 
					
						
							|  |  |  | 							"baseURI", | 
					
						
							|  |  |  | 							RuntimeGlobals.baseURI, | 
					
						
							|  |  |  | 							baseURISource | 
					
						
							|  |  |  | 						) | 
					
						
							| 
									
										
										
										
											2020-08-05 05:42:29 +08:00
										 |  |  | 					); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = BaseURIPlugin; |