| 
									
										
										
										
											2015-05-13 06:17:06 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 01:17:40 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const DelegatedModule = require("./DelegatedModule"); | 
					
						
							| 
									
										
										
										
											2015-05-13 06:17:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-18 05:03:21 +08:00
										 |  |  | // options.source
 | 
					
						
							|  |  |  | // options.type
 | 
					
						
							|  |  |  | // options.context
 | 
					
						
							|  |  |  | // options.scope
 | 
					
						
							|  |  |  | // options.content
 | 
					
						
							| 
									
										
										
										
											2017-02-23 01:17:40 +08:00
										 |  |  | class DelegatedModuleFactoryPlugin { | 
					
						
							|  |  |  | 	constructor(options) { | 
					
						
							|  |  |  | 		this.options = options; | 
					
						
							|  |  |  | 		options.type = options.type || "require"; | 
					
						
							| 
									
										
										
										
											2018-07-10 04:16:01 +08:00
										 |  |  | 		options.extensions = options.extensions || [ | 
					
						
							|  |  |  | 			"", | 
					
						
							|  |  |  | 			".wasm", | 
					
						
							|  |  |  | 			".mjs", | 
					
						
							|  |  |  | 			".js", | 
					
						
							|  |  |  | 			".json" | 
					
						
							|  |  |  | 		]; | 
					
						
							| 
									
										
										
										
											2017-02-23 01:17:40 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-05-13 06:17:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 01:17:40 +08:00
										 |  |  | 	apply(normalModuleFactory) { | 
					
						
							|  |  |  | 		const scope = this.options.scope; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		if (scope) { | 
					
						
							|  |  |  | 			normalModuleFactory.hooks.factory.tap( | 
					
						
							|  |  |  | 				"DelegatedModuleFactoryPlugin", | 
					
						
							|  |  |  | 				factory => (data, callback) => { | 
					
						
							|  |  |  | 					const dependency = data.dependencies[0]; | 
					
						
							|  |  |  | 					const request = dependency.request; | 
					
						
							|  |  |  | 					if (request && request.indexOf(scope + "/") === 0) { | 
					
						
							|  |  |  | 						const innerRequest = "." + request.substr(scope.length); | 
					
						
							|  |  |  | 						let resolved; | 
					
						
							|  |  |  | 						if (innerRequest in this.options.content) { | 
					
						
							|  |  |  | 							resolved = this.options.content[innerRequest]; | 
					
						
							|  |  |  | 							return callback( | 
					
						
							|  |  |  | 								null, | 
					
						
							|  |  |  | 								new DelegatedModule( | 
					
						
							|  |  |  | 									this.options.source, | 
					
						
							|  |  |  | 									resolved, | 
					
						
							|  |  |  | 									this.options.type, | 
					
						
							|  |  |  | 									innerRequest, | 
					
						
							|  |  |  | 									request | 
					
						
							|  |  |  | 								) | 
					
						
							|  |  |  | 							); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						for (let i = 0; i < this.options.extensions.length; i++) { | 
					
						
							|  |  |  | 							const extension = this.options.extensions[i]; | 
					
						
							|  |  |  | 							const requestPlusExt = innerRequest + extension; | 
					
						
							|  |  |  | 							if (requestPlusExt in this.options.content) { | 
					
						
							|  |  |  | 								resolved = this.options.content[requestPlusExt]; | 
					
						
							|  |  |  | 								return callback( | 
					
						
							|  |  |  | 									null, | 
					
						
							|  |  |  | 									new DelegatedModule( | 
					
						
							|  |  |  | 										this.options.source, | 
					
						
							|  |  |  | 										resolved, | 
					
						
							|  |  |  | 										this.options.type, | 
					
						
							|  |  |  | 										requestPlusExt, | 
					
						
							|  |  |  | 										request + extension | 
					
						
							|  |  |  | 									) | 
					
						
							|  |  |  | 								); | 
					
						
							|  |  |  | 							} | 
					
						
							| 
									
										
										
										
											2015-05-18 05:03:21 +08:00
										 |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					return factory(data, callback); | 
					
						
							| 
									
										
										
										
											2017-02-23 15:43:44 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			); | 
					
						
							| 
									
										
										
										
											2017-02-23 01:17:40 +08:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			normalModuleFactory.hooks.module.tap( | 
					
						
							|  |  |  | 				"DelegatedModuleFactoryPlugin", | 
					
						
							|  |  |  | 				module => { | 
					
						
							| 
									
										
										
										
											2018-07-20 22:24:35 +08:00
										 |  |  | 					const request = module.libIdent(this.options); | 
					
						
							|  |  |  | 					if (request) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						if (request && request in this.options.content) { | 
					
						
							|  |  |  | 							const resolved = this.options.content[request]; | 
					
						
							|  |  |  | 							return new DelegatedModule( | 
					
						
							|  |  |  | 								this.options.source, | 
					
						
							|  |  |  | 								resolved, | 
					
						
							|  |  |  | 								this.options.type, | 
					
						
							|  |  |  | 								request, | 
					
						
							|  |  |  | 								module | 
					
						
							|  |  |  | 							); | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2017-02-23 01:17:40 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					return module; | 
					
						
							| 
									
										
										
										
											2015-05-18 05:03:21 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			); | 
					
						
							| 
									
										
										
										
											2017-02-23 01:17:40 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-05-18 05:03:21 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-02-23 01:17:40 +08:00
										 |  |  | } | 
					
						
							|  |  |  | module.exports = DelegatedModuleFactoryPlugin; |