| 
									
										
										
										
											2020-02-25 04:18:14 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const RuntimeGlobals = require("../RuntimeGlobals"); | 
					
						
							|  |  |  | const RuntimeModule = require("../RuntimeModule"); | 
					
						
							|  |  |  | const Template = require("../Template"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** @typedef {import("./RemoteModule")} RemoteModule */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class RemoteRuntimeModule extends RuntimeModule { | 
					
						
							|  |  |  | 	constructor() { | 
					
						
							|  |  |  | 		super("remotes loading"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @returns {string} runtime code | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	generate() { | 
					
						
							| 
									
										
										
										
											2021-04-13 02:53:25 +08:00
										 |  |  | 		const { compilation, chunkGraph } = this; | 
					
						
							|  |  |  | 		const { runtimeTemplate, moduleGraph } = compilation; | 
					
						
							| 
									
										
										
										
											2020-02-25 04:18:14 +08:00
										 |  |  | 		const chunkToRemotesMapping = {}; | 
					
						
							|  |  |  | 		const idToExternalAndNameMapping = {}; | 
					
						
							|  |  |  | 		for (const chunk of this.chunk.getAllAsyncChunks()) { | 
					
						
							|  |  |  | 			const modules = chunkGraph.getChunkModulesIterableBySourceType( | 
					
						
							|  |  |  | 				chunk, | 
					
						
							|  |  |  | 				"remote" | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 			if (!modules) continue; | 
					
						
							|  |  |  | 			const remotes = (chunkToRemotesMapping[chunk.id] = []); | 
					
						
							|  |  |  | 			for (const m of modules) { | 
					
						
							|  |  |  | 				const module = /** @type {RemoteModule} */ (m); | 
					
						
							|  |  |  | 				const name = module.internalRequest; | 
					
						
							|  |  |  | 				const id = chunkGraph.getModuleId(module); | 
					
						
							| 
									
										
										
										
											2020-05-26 23:11:21 +08:00
										 |  |  | 				const shareScope = module.shareScope; | 
					
						
							|  |  |  | 				const dep = module.dependencies[0]; | 
					
						
							|  |  |  | 				const externalModule = moduleGraph.getModule(dep); | 
					
						
							|  |  |  | 				const externalModuleId = | 
					
						
							|  |  |  | 					externalModule && chunkGraph.getModuleId(externalModule); | 
					
						
							| 
									
										
										
										
											2020-02-25 04:18:14 +08:00
										 |  |  | 				remotes.push(id); | 
					
						
							| 
									
										
										
										
											2020-05-26 23:11:21 +08:00
										 |  |  | 				idToExternalAndNameMapping[id] = [shareScope, name, externalModuleId]; | 
					
						
							| 
									
										
										
										
											2020-02-25 04:18:14 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return Template.asString([ | 
					
						
							|  |  |  | 			`var chunkMapping = ${JSON.stringify( | 
					
						
							|  |  |  | 				chunkToRemotesMapping, | 
					
						
							|  |  |  | 				null, | 
					
						
							|  |  |  | 				"\t" | 
					
						
							|  |  |  | 			)};`,
 | 
					
						
							|  |  |  | 			`var idToExternalAndNameMapping = ${JSON.stringify( | 
					
						
							|  |  |  | 				idToExternalAndNameMapping, | 
					
						
							|  |  |  | 				null, | 
					
						
							|  |  |  | 				"\t" | 
					
						
							|  |  |  | 			)};`,
 | 
					
						
							|  |  |  | 			`${ | 
					
						
							|  |  |  | 				RuntimeGlobals.ensureChunkHandlers | 
					
						
							|  |  |  | 			}.remotes = ${runtimeTemplate.basicFunction("chunkId, promises", [ | 
					
						
							|  |  |  | 				`if(${RuntimeGlobals.hasOwnProperty}(chunkMapping, chunkId)) {`, | 
					
						
							|  |  |  | 				Template.indent([ | 
					
						
							|  |  |  | 					`chunkMapping[chunkId].forEach(${runtimeTemplate.basicFunction("id", [ | 
					
						
							| 
									
										
										
										
											2020-09-16 16:42:50 +08:00
										 |  |  | 						`var getScope = ${RuntimeGlobals.currentRemoteGetScope};`, | 
					
						
							|  |  |  | 						"if(!getScope) getScope = [];", | 
					
						
							| 
									
										
										
										
											2020-02-25 04:18:14 +08:00
										 |  |  | 						"var data = idToExternalAndNameMapping[id];", | 
					
						
							| 
									
										
										
										
											2020-09-16 16:42:50 +08:00
										 |  |  | 						"if(getScope.indexOf(data) >= 0) return;", | 
					
						
							|  |  |  | 						"getScope.push(data);", | 
					
						
							|  |  |  | 						`if(data.p) return promises.push(data.p);`, | 
					
						
							| 
									
										
										
										
											2020-05-05 20:45:18 +08:00
										 |  |  | 						`var onError = ${runtimeTemplate.basicFunction("error", [ | 
					
						
							| 
									
										
										
										
											2020-05-14 21:50:35 +08:00
										 |  |  | 							'if(!error) error = new Error("Container missing");', | 
					
						
							|  |  |  | 							'if(typeof error.message === "string")', | 
					
						
							|  |  |  | 							Template.indent( | 
					
						
							| 
									
										
										
										
											2020-05-26 23:11:21 +08:00
										 |  |  | 								`error.message += '\\nwhile loading "' + data[1] + '" from ' + data[2];` | 
					
						
							| 
									
										
										
										
											2020-05-14 21:50:35 +08:00
										 |  |  | 							), | 
					
						
							| 
									
										
										
										
											2022-04-02 09:00:08 +08:00
										 |  |  | 							`${ | 
					
						
							|  |  |  | 								RuntimeGlobals.moduleFactories | 
					
						
							|  |  |  | 							}[id] = ${runtimeTemplate.basicFunction("", ["throw error;"])}`,
 | 
					
						
							| 
									
										
										
										
											2020-09-16 16:42:50 +08:00
										 |  |  | 							"data.p = 0;" | 
					
						
							| 
									
										
										
										
											2020-05-05 20:45:18 +08:00
										 |  |  | 						])};`,
 | 
					
						
							| 
									
										
										
										
											2020-05-26 23:11:21 +08:00
										 |  |  | 						`var handleFunction = ${runtimeTemplate.basicFunction( | 
					
						
							| 
									
										
										
										
											2020-09-16 16:42:50 +08:00
										 |  |  | 							"fn, arg1, arg2, d, next, first", | 
					
						
							| 
									
										
										
										
											2020-05-14 21:50:35 +08:00
										 |  |  | 							[ | 
					
						
							|  |  |  | 								"try {", | 
					
						
							|  |  |  | 								Template.indent([ | 
					
						
							| 
									
										
										
										
											2020-09-16 16:42:50 +08:00
										 |  |  | 									"var promise = fn(arg1, arg2);", | 
					
						
							| 
									
										
										
										
											2020-05-14 21:50:35 +08:00
										 |  |  | 									"if(promise && promise.then) {", | 
					
						
							|  |  |  | 									Template.indent([ | 
					
						
							| 
									
										
										
										
											2020-05-26 23:11:21 +08:00
										 |  |  | 										`var p = promise.then(${runtimeTemplate.returningFunction( | 
					
						
							| 
									
										
										
										
											2020-09-16 16:42:50 +08:00
										 |  |  | 											"next(result, d)", | 
					
						
							| 
									
										
										
										
											2020-05-26 23:11:21 +08:00
										 |  |  | 											"result" | 
					
						
							|  |  |  | 										)}, onError);`,
 | 
					
						
							| 
									
										
										
										
											2020-09-16 16:42:50 +08:00
										 |  |  | 										`if(first) promises.push(data.p = p); else return p;` | 
					
						
							| 
									
										
										
										
											2020-05-14 21:50:35 +08:00
										 |  |  | 									]), | 
					
						
							|  |  |  | 									"} else {", | 
					
						
							| 
									
										
										
										
											2020-09-16 16:42:50 +08:00
										 |  |  | 									Template.indent(["return next(promise, d, first);"]), | 
					
						
							| 
									
										
										
										
											2020-05-14 21:50:35 +08:00
										 |  |  | 									"}" | 
					
						
							|  |  |  | 								]), | 
					
						
							|  |  |  | 								"} catch(error) {", | 
					
						
							|  |  |  | 								Template.indent(["onError(error);"]), | 
					
						
							|  |  |  | 								"}" | 
					
						
							|  |  |  | 							] | 
					
						
							| 
									
										
										
										
											2020-05-26 23:11:21 +08:00
										 |  |  | 						)}`,
 | 
					
						
							|  |  |  | 						`var onExternal = ${runtimeTemplate.returningFunction( | 
					
						
							| 
									
										
										
										
											2020-09-16 16:42:50 +08:00
										 |  |  | 							`external ? handleFunction(${RuntimeGlobals.initializeSharing}, data[0], 0, external, onInitialized, first) : onError()`, | 
					
						
							| 
									
										
										
										
											2020-05-26 23:11:21 +08:00
										 |  |  | 							"external, _, first" | 
					
						
							|  |  |  | 						)};`,
 | 
					
						
							|  |  |  | 						`var onInitialized = ${runtimeTemplate.returningFunction( | 
					
						
							| 
									
										
										
										
											2020-09-16 16:42:50 +08:00
										 |  |  | 							`handleFunction(external.get, data[1], getScope, 0, onFactory, first)`, | 
					
						
							| 
									
										
										
										
											2020-05-26 23:11:21 +08:00
										 |  |  | 							"_, external, first" | 
					
						
							| 
									
										
										
										
											2020-05-14 21:50:35 +08:00
										 |  |  | 						)};`,
 | 
					
						
							| 
									
										
										
										
											2020-05-05 20:45:18 +08:00
										 |  |  | 						`var onFactory = ${runtimeTemplate.basicFunction("factory", [ | 
					
						
							| 
									
										
										
										
											2020-09-16 16:42:50 +08:00
										 |  |  | 							"data.p = 1;", | 
					
						
							| 
									
										
										
										
											2022-04-02 09:00:08 +08:00
										 |  |  | 							`${ | 
					
						
							|  |  |  | 								RuntimeGlobals.moduleFactories | 
					
						
							|  |  |  | 							}[id] = ${runtimeTemplate.basicFunction("module", [ | 
					
						
							|  |  |  | 								"module.exports = factory();" | 
					
						
							|  |  |  | 							])}`
 | 
					
						
							| 
									
										
										
										
											2020-05-05 20:45:18 +08:00
										 |  |  | 						])};`,
 | 
					
						
							| 
									
										
										
										
											2020-09-16 16:42:50 +08:00
										 |  |  | 						"handleFunction(__webpack_require__, data[2], 0, 0, onExternal, 1);" | 
					
						
							| 
									
										
										
										
											2020-02-25 04:18:14 +08:00
										 |  |  | 					])});`
 | 
					
						
							|  |  |  | 				]), | 
					
						
							|  |  |  | 				"}" | 
					
						
							|  |  |  | 			])}`
 | 
					
						
							|  |  |  | 		]); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = RemoteRuntimeModule; |