| 
									
										
										
										
											2015-05-13 06:15:01 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2017-02-23 23:00:38 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2015-05-13 06:15:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 23:00:38 +08:00
										 |  |  | const ExternalModule = require("./ExternalModule"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ExternalModuleFactoryPlugin { | 
					
						
							|  |  |  | 	constructor(type, externals) { | 
					
						
							|  |  |  | 		this.type = type; | 
					
						
							|  |  |  | 		this.externals = externals; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-05-13 06:15:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 23:00:38 +08:00
										 |  |  | 	apply(normalModuleFactory) { | 
					
						
							|  |  |  | 		const globalType = this.type; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		normalModuleFactory.hooks.factory.tap( | 
					
						
							|  |  |  | 			"ExternalModuleFactoryPlugin", | 
					
						
							|  |  |  | 			factory => (data, callback) => { | 
					
						
							|  |  |  | 				const context = data.context; | 
					
						
							|  |  |  | 				const dependency = data.dependencies[0]; | 
					
						
							| 
									
										
										
										
											2015-07-13 06:20:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				const handleExternal = (value, type, callback) => { | 
					
						
							|  |  |  | 					if (typeof type === "function") { | 
					
						
							|  |  |  | 						callback = type; | 
					
						
							|  |  |  | 						type = undefined; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					if (value === false) return factory(data, callback); | 
					
						
							|  |  |  | 					if (value === true) value = dependency.request; | 
					
						
							| 
									
										
										
										
											2018-08-21 08:26:50 +08:00
										 |  |  | 					if (type === undefined && /^[a-z0-9]+ /.test(value)) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						const idx = value.indexOf(" "); | 
					
						
							|  |  |  | 						type = value.substr(0, idx); | 
					
						
							|  |  |  | 						value = value.substr(idx + 1); | 
					
						
							| 
									
										
										
										
											2015-05-13 06:15:01 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					callback( | 
					
						
							|  |  |  | 						null, | 
					
						
							|  |  |  | 						new ExternalModule(value, type || globalType, dependency.request) | 
					
						
							|  |  |  | 					); | 
					
						
							|  |  |  | 					return true; | 
					
						
							|  |  |  | 				}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				const handleExternals = (externals, callback) => { | 
					
						
							|  |  |  | 					if (typeof externals === "string") { | 
					
						
							|  |  |  | 						if (externals === dependency.request) { | 
					
						
							|  |  |  | 							return handleExternal(dependency.request, callback); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} else if (Array.isArray(externals)) { | 
					
						
							|  |  |  | 						let i = 0; | 
					
						
							|  |  |  | 						const next = () => { | 
					
						
							|  |  |  | 							let asyncFlag; | 
					
						
							|  |  |  | 							const handleExternalsAndCallback = (err, module) => { | 
					
						
							|  |  |  | 								if (err) return callback(err); | 
					
						
							|  |  |  | 								if (!module) { | 
					
						
							|  |  |  | 									if (asyncFlag) { | 
					
						
							|  |  |  | 										asyncFlag = false; | 
					
						
							|  |  |  | 										return; | 
					
						
							|  |  |  | 									} | 
					
						
							|  |  |  | 									return next(); | 
					
						
							| 
									
										
										
										
											2016-10-29 17:11:44 +08:00
										 |  |  | 								} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 								callback(null, module); | 
					
						
							|  |  |  | 							}; | 
					
						
							| 
									
										
										
										
											2016-10-29 17:11:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 							do { | 
					
						
							|  |  |  | 								asyncFlag = true; | 
					
						
							|  |  |  | 								if (i >= externals.length) return callback(); | 
					
						
							|  |  |  | 								handleExternals(externals[i++], handleExternalsAndCallback); | 
					
						
							| 
									
										
										
										
											2018-06-27 19:48:13 +08:00
										 |  |  | 							} while (!asyncFlag); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 							asyncFlag = false; | 
					
						
							|  |  |  | 						}; | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						next(); | 
					
						
							|  |  |  | 						return; | 
					
						
							|  |  |  | 					} else if (externals instanceof RegExp) { | 
					
						
							|  |  |  | 						if (externals.test(dependency.request)) { | 
					
						
							|  |  |  | 							return handleExternal(dependency.request, callback); | 
					
						
							| 
									
										
										
										
											2015-05-13 06:15:01 +08:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					} else if (typeof externals === "function") { | 
					
						
							|  |  |  | 						externals.call( | 
					
						
							|  |  |  | 							null, | 
					
						
							|  |  |  | 							context, | 
					
						
							|  |  |  | 							dependency.request, | 
					
						
							|  |  |  | 							(err, value, type) => { | 
					
						
							|  |  |  | 								if (err) return callback(err); | 
					
						
							| 
									
										
										
										
											2018-08-21 08:26:50 +08:00
										 |  |  | 								if (value !== undefined) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 									handleExternal(value, type, callback); | 
					
						
							|  |  |  | 								} else { | 
					
						
							|  |  |  | 									callback(); | 
					
						
							|  |  |  | 								} | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 						return; | 
					
						
							|  |  |  | 					} else if ( | 
					
						
							|  |  |  | 						typeof externals === "object" && | 
					
						
							|  |  |  | 						Object.prototype.hasOwnProperty.call(externals, dependency.request) | 
					
						
							|  |  |  | 					) { | 
					
						
							|  |  |  | 						return handleExternal(externals[dependency.request], callback); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					callback(); | 
					
						
							|  |  |  | 				}; | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				handleExternals(this.externals, (err, module) => { | 
					
						
							|  |  |  | 					if (err) return callback(err); | 
					
						
							|  |  |  | 					if (!module) return handleExternal(false, callback); | 
					
						
							|  |  |  | 					return callback(null, module); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2017-02-23 23:00:38 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | module.exports = ExternalModuleFactoryPlugin; |