| 
									
										
										
										
											2014-03-05 16:58:51 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2017-02-18 20:17:16 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | const Module = require("./Module"); | 
					
						
							|  |  |  | const OriginalSource = require("webpack-sources").OriginalSource; | 
					
						
							|  |  |  | const RawSource = require("webpack-sources").RawSource; | 
					
						
							|  |  |  | const WebpackMissingModule = require("./dependencies/WebpackMissingModule"); | 
					
						
							| 
									
										
										
										
											2017-02-20 16:17:51 +08:00
										 |  |  | const Template = require("./Template"); | 
					
						
							| 
									
										
										
										
											2014-03-05 16:58:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 20:17:16 +08:00
										 |  |  | class ExternalModule extends Module { | 
					
						
							| 
									
										
										
										
											2017-06-09 04:22:58 +08:00
										 |  |  | 	constructor(request, type, userRequest) { | 
					
						
							| 
									
										
										
										
											2017-02-18 20:17:16 +08:00
										 |  |  | 		super(); | 
					
						
							|  |  |  | 		this.request = request; | 
					
						
							| 
									
										
										
										
											2017-06-09 04:22:58 +08:00
										 |  |  | 		this.userRequest = userRequest; | 
					
						
							| 
									
										
										
										
											2017-02-18 20:17:16 +08:00
										 |  |  | 		this.type = type; | 
					
						
							|  |  |  | 		this.built = false; | 
					
						
							|  |  |  | 		this.external = true; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-03-05 16:58:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-18 00:49:09 +08:00
										 |  |  | 	libIdent() { | 
					
						
							| 
									
										
										
										
											2017-06-09 04:22:58 +08:00
										 |  |  | 		return this.userRequest; | 
					
						
							| 
									
										
										
										
											2017-05-18 00:49:09 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-19 08:55:07 +08:00
										 |  |  | 	chunkCondition(chunk) { | 
					
						
							|  |  |  | 		return chunk.hasEntryModule(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 20:17:16 +08:00
										 |  |  | 	identifier() { | 
					
						
							|  |  |  | 		return "external " + JSON.stringify(this.request); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-03-05 16:58:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 20:17:16 +08:00
										 |  |  | 	readableIdentifier() { | 
					
						
							|  |  |  | 		return "external " + JSON.stringify(this.request); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-03-05 16:58:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 20:17:16 +08:00
										 |  |  | 	needRebuild() { | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-03-05 16:58:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 20:17:16 +08:00
										 |  |  | 	build(options, compilation, resolver, fs, callback) { | 
					
						
							| 
									
										
										
										
											2017-05-04 10:19:54 +08:00
										 |  |  | 		this.builtTime = Date.now(); | 
					
						
							| 
									
										
										
										
											2017-02-18 20:17:16 +08:00
										 |  |  | 		callback(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-03-05 16:58:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-19 08:55:07 +08:00
										 |  |  | 	getSourceForGlobalVariableExternal(variableName, type) { | 
					
						
							|  |  |  | 		if(!Array.isArray(variableName)) { | 
					
						
							|  |  |  | 			// make it an array as the look up works the same basically
 | 
					
						
							|  |  |  | 			variableName = [variableName]; | 
					
						
							| 
									
										
										
										
											2017-02-18 21:06:23 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// needed for e.g. window["some"]["thing"]
 | 
					
						
							| 
									
										
										
										
											2017-02-19 08:55:07 +08:00
										 |  |  | 		const objectLookup = variableName.map(r => `[${JSON.stringify(r)}]`).join(""); | 
					
						
							| 
									
										
										
										
											2017-02-18 21:06:23 +08:00
										 |  |  | 		return `(function() { module.exports = ${type}${objectLookup}; }());`; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-19 08:55:07 +08:00
										 |  |  | 	getSourceForCommonJsExternal(moduleAndSpecifiers) { | 
					
						
							|  |  |  | 		if(!Array.isArray(moduleAndSpecifiers)) { | 
					
						
							|  |  |  | 			return `module.exports = require(${JSON.stringify(moduleAndSpecifiers)});`; | 
					
						
							| 
									
										
										
										
											2017-02-18 21:06:23 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-19 08:55:07 +08:00
										 |  |  | 		const moduleName = moduleAndSpecifiers[0]; | 
					
						
							|  |  |  | 		const objectLookup = moduleAndSpecifiers.slice(1).map(r => `[${JSON.stringify(r)}]`).join(""); | 
					
						
							| 
									
										
										
										
											2017-02-18 21:06:23 +08:00
										 |  |  | 		return `module.exports = require(${moduleName})${objectLookup};`; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	checkExternalVariable(variableToCheck, request) { | 
					
						
							| 
									
										
										
										
											2017-02-20 16:15:52 +08:00
										 |  |  | 		return `if(typeof ${variableToCheck} === 'undefined') {${WebpackMissingModule.moduleCode(request)}}\n`; | 
					
						
							| 
									
										
										
										
											2017-02-18 21:06:23 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-19 09:11:08 +08:00
										 |  |  | 	getSourceForAmdOrUmdExternal(id, optional, request) { | 
					
						
							| 
									
										
										
										
											2017-02-20 16:17:51 +08:00
										 |  |  | 		const externalVariable = Template.toIdentifier(`__WEBPACK_EXTERNAL_MODULE_${id}__`); | 
					
						
							| 
									
										
										
										
											2017-02-18 21:06:23 +08:00
										 |  |  | 		const missingModuleError = optional ? this.checkExternalVariable(externalVariable, request) : ""; | 
					
						
							| 
									
										
										
										
											2017-02-19 09:11:08 +08:00
										 |  |  | 		return `${missingModuleError}module.exports = ${externalVariable};`; | 
					
						
							| 
									
										
										
										
											2017-02-18 21:06:23 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-19 09:11:08 +08:00
										 |  |  | 	getSourceForDefaultCase(optional, request) { | 
					
						
							| 
									
										
										
										
											2017-02-18 21:06:23 +08:00
										 |  |  | 		const missingModuleError = optional ? this.checkExternalVariable(request, request) : ""; | 
					
						
							| 
									
										
										
										
											2017-02-19 09:11:08 +08:00
										 |  |  | 		return `${missingModuleError}module.exports = ${request};`; | 
					
						
							| 
									
										
										
										
											2017-02-18 21:06:23 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	getSourceString() { | 
					
						
							|  |  |  | 		const request = typeof this.request === "object" ? this.request[this.type] : this.request; | 
					
						
							| 
									
										
										
										
											2017-02-18 20:17:16 +08:00
										 |  |  | 		switch(this.type) { | 
					
						
							|  |  |  | 			case "this": | 
					
						
							|  |  |  | 			case "window": | 
					
						
							|  |  |  | 			case "global": | 
					
						
							| 
									
										
										
										
											2017-02-18 21:06:23 +08:00
										 |  |  | 				return this.getSourceForGlobalVariableExternal(request, this.type); | 
					
						
							| 
									
										
										
										
											2017-02-18 20:17:16 +08:00
										 |  |  | 			case "commonjs": | 
					
						
							|  |  |  | 			case "commonjs2": | 
					
						
							| 
									
										
										
										
											2017-02-18 21:06:23 +08:00
										 |  |  | 				return this.getSourceForCommonJsExternal(request); | 
					
						
							| 
									
										
										
										
											2017-02-18 20:17:16 +08:00
										 |  |  | 			case "amd": | 
					
						
							|  |  |  | 			case "umd": | 
					
						
							|  |  |  | 			case "umd2": | 
					
						
							| 
									
										
										
										
											2017-02-19 09:11:08 +08:00
										 |  |  | 				return this.getSourceForAmdOrUmdExternal(this.id, this.optional, request); | 
					
						
							| 
									
										
										
										
											2017-02-18 20:17:16 +08:00
										 |  |  | 			default: | 
					
						
							| 
									
										
										
										
											2017-02-19 09:11:08 +08:00
										 |  |  | 				return this.getSourceForDefaultCase(this.optional, request); | 
					
						
							| 
									
										
										
										
											2017-02-18 20:17:16 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-02-18 21:06:23 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	getSource(sourceString) { | 
					
						
							| 
									
										
										
										
											2017-02-18 20:17:16 +08:00
										 |  |  | 		if(this.useSourceMap) { | 
					
						
							| 
									
										
										
										
											2017-02-18 21:06:23 +08:00
										 |  |  | 			return new OriginalSource(sourceString, this.identifier()); | 
					
						
							| 
									
										
										
										
											2017-02-18 20:17:16 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-02-18 21:06:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		return new RawSource(sourceString); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	source() { | 
					
						
							|  |  |  | 		return this.getSource( | 
					
						
							|  |  |  | 			this.getSourceString() | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2014-03-05 16:58:51 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-02-18 20:17:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	size() { | 
					
						
							|  |  |  | 		return 42; | 
					
						
							| 
									
										
										
										
											2014-03-05 16:58:51 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-09-18 00:18:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	updateHash(hash) { | 
					
						
							|  |  |  | 		hash.update(this.type); | 
					
						
							|  |  |  | 		hash.update(JSON.stringify(this.request)); | 
					
						
							| 
									
										
										
										
											2017-09-18 15:33:37 +08:00
										 |  |  | 		hash.update(JSON.stringify(this.optional)); | 
					
						
							| 
									
										
										
										
											2017-09-18 00:18:40 +08:00
										 |  |  | 		super.updateHash(hash); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-02-18 20:17:16 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-03-05 16:58:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 20:17:16 +08:00
										 |  |  | module.exports = ExternalModule; |