| 
									
										
										
										
											2014-06-03 03:23:53 +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 00:54:16 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 19:05:58 +08:00
										 |  |  | const { SyncWaterfallHook } = require("tapable"); | 
					
						
							| 
									
										
										
										
											2018-07-31 00:54:54 +08:00
										 |  |  | const HotModuleReplacementPlugin = require("../HotModuleReplacementPlugin"); | 
					
						
							| 
									
										
										
										
											2018-07-09 14:40:10 +08:00
										 |  |  | const MainTemplate = require("../MainTemplate"); | 
					
						
							| 
									
										
										
										
											2017-11-12 01:48:29 +08:00
										 |  |  | const Template = require("../Template"); | 
					
						
							| 
									
										
										
										
											2018-08-14 22:40:37 +08:00
										 |  |  | const getEntryInfo = require("./JsonpHelpers").getEntryInfo; | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-14 17:18:22 +08:00
										 |  |  | /** @typedef {import("../Compilation")} Compilation */ | 
					
						
							| 
									
										
										
										
											2018-08-14 22:40:37 +08:00
										 |  |  | /** @typedef {import("../MainTemplate")} MainTemplate */ | 
					
						
							| 
									
										
										
										
											2018-08-14 17:18:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-06 19:11:11 +08:00
										 |  |  | const mainTemplateHooksMap = new WeakMap(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | class JsonpMainTemplatePlugin { | 
					
						
							| 
									
										
										
										
											2018-07-31 00:54:54 +08:00
										 |  |  | 	static getMainTemplateHooks(mainTemplate) { | 
					
						
							| 
									
										
										
										
											2018-07-09 14:40:10 +08:00
										 |  |  | 		if (!(mainTemplate instanceof MainTemplate)) { | 
					
						
							|  |  |  | 			throw new TypeError( | 
					
						
							|  |  |  | 				"The 'mainTemplate' argument must be an instance of MainTemplate" | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-07-06 19:11:11 +08:00
										 |  |  | 		let hooks = mainTemplateHooksMap.get(mainTemplate); | 
					
						
							|  |  |  | 		if (hooks === undefined) { | 
					
						
							|  |  |  | 			hooks = { | 
					
						
							|  |  |  | 				jsonpScript: new SyncWaterfallHook(["source", "chunk", "hash"]), | 
					
						
							|  |  |  | 				linkPreload: new SyncWaterfallHook(["source", "chunk", "hash"]), | 
					
						
							|  |  |  | 				linkPrefetch: new SyncWaterfallHook(["source", "chunk", "hash"]) | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 			mainTemplateHooksMap.set(mainTemplate, hooks); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return hooks; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-31 00:54:54 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {Compilation} compilation the compilation | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	constructor(compilation) { | 
					
						
							|  |  |  | 		this.compilation = compilation; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-14 22:40:37 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {MainTemplate} mainTemplate the main template | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | 	apply(mainTemplate) { | 
					
						
							| 
									
										
										
										
											2018-01-20 00:06:59 +08:00
										 |  |  | 		const needChunkOnDemandLoadingCode = chunk => { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			for (const chunkGroup of chunk.groupsIterable) { | 
					
						
							|  |  |  | 				if (chunkGroup.getNumberOfChildren() > 0) return true; | 
					
						
							| 
									
										
										
										
											2018-01-20 00:06:59 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			return false; | 
					
						
							|  |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | 		const needChunkLoadingCode = chunk => { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			for (const chunkGroup of chunk.groupsIterable) { | 
					
						
							|  |  |  | 				if (chunkGroup.chunks.length > 1) return true; | 
					
						
							|  |  |  | 				if (chunkGroup.getNumberOfChildren() > 0) return true; | 
					
						
							| 
									
										
										
										
											2018-01-20 00:06:59 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			return false; | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 		const needEntryDeferringCode = chunk => { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			for (const chunkGroup of chunk.groupsIterable) { | 
					
						
							|  |  |  | 				if (chunkGroup.chunks.length > 1) return true; | 
					
						
							| 
									
										
										
										
											2018-01-20 00:06:59 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			return false; | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2018-06-02 01:51:39 +08:00
										 |  |  | 		const needPrefetchingCode = chunk => { | 
					
						
							| 
									
										
										
										
											2018-08-14 17:18:22 +08:00
										 |  |  | 			const allPrefetchChunks = chunk.getChildIdsByOrdersMap( | 
					
						
							|  |  |  | 				this.compilation.chunkGraph, | 
					
						
							|  |  |  | 				true | 
					
						
							|  |  |  | 			).prefetch; | 
					
						
							| 
									
										
										
										
											2018-06-02 01:51:39 +08:00
										 |  |  | 			return allPrefetchChunks && Object.keys(allPrefetchChunks).length; | 
					
						
							|  |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2018-05-27 05:27:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-16 16:27:22 +08:00
										 |  |  | 		const getScriptSrcPath = (hash, chunk, chunkIdExpression) => { | 
					
						
							|  |  |  | 			const chunkFilename = mainTemplate.outputOptions.chunkFilename; | 
					
						
							|  |  |  | 			const chunkMaps = chunk.getChunkMaps(); | 
					
						
							|  |  |  | 			return mainTemplate.getAssetPath(JSON.stringify(chunkFilename), { | 
					
						
							|  |  |  | 				hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`, | 
					
						
							|  |  |  | 				hashWithLength: length => | 
					
						
							|  |  |  | 					`" + ${mainTemplate.renderCurrentHashCode(hash, length)} + "`, | 
					
						
							|  |  |  | 				chunk: { | 
					
						
							|  |  |  | 					id: `" + ${chunkIdExpression} + "`, | 
					
						
							|  |  |  | 					hash: `" + ${JSON.stringify( | 
					
						
							|  |  |  | 						chunkMaps.hash | 
					
						
							|  |  |  | 					)}[${chunkIdExpression}] + "`,
 | 
					
						
							|  |  |  | 					hashWithLength(length) { | 
					
						
							|  |  |  | 						const shortChunkHashMap = Object.create(null); | 
					
						
							|  |  |  | 						for (const chunkId of Object.keys(chunkMaps.hash)) { | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 							if (typeof chunkMaps.hash[chunkId] === "string") { | 
					
						
							| 
									
										
										
										
											2018-04-16 16:27:22 +08:00
										 |  |  | 								shortChunkHashMap[chunkId] = chunkMaps.hash[chunkId].substr( | 
					
						
							|  |  |  | 									0, | 
					
						
							|  |  |  | 									length | 
					
						
							|  |  |  | 								); | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 							} | 
					
						
							| 
									
										
										
										
											2018-04-16 16:27:22 +08:00
										 |  |  | 						} | 
					
						
							|  |  |  | 						return `" + ${JSON.stringify( | 
					
						
							|  |  |  | 							shortChunkHashMap | 
					
						
							|  |  |  | 						)}[${chunkIdExpression}] + "`;
 | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					name: `" + (${JSON.stringify( | 
					
						
							|  |  |  | 						chunkMaps.name | 
					
						
							|  |  |  | 					)}[${chunkIdExpression}]||${chunkIdExpression}) + "`,
 | 
					
						
							|  |  |  | 					contentHash: { | 
					
						
							|  |  |  | 						javascript: `" + ${JSON.stringify( | 
					
						
							|  |  |  | 							chunkMaps.contentHash.javascript | 
					
						
							|  |  |  | 						)}[${chunkIdExpression}] + "`
 | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					contentHashWithLength: { | 
					
						
							|  |  |  | 						javascript: length => { | 
					
						
							|  |  |  | 							const shortContentHashMap = {}; | 
					
						
							|  |  |  | 							const contentHash = chunkMaps.contentHash.javascript; | 
					
						
							|  |  |  | 							for (const chunkId of Object.keys(contentHash)) { | 
					
						
							|  |  |  | 								if (typeof contentHash[chunkId] === "string") { | 
					
						
							|  |  |  | 									shortContentHashMap[chunkId] = contentHash[chunkId].substr( | 
					
						
							|  |  |  | 										0, | 
					
						
							|  |  |  | 										length | 
					
						
							|  |  |  | 									); | 
					
						
							|  |  |  | 								} | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 							return `" + ${JSON.stringify( | 
					
						
							|  |  |  | 								shortContentHashMap | 
					
						
							|  |  |  | 							)}[${chunkIdExpression}] + "`;
 | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				contentHashType: "javascript" | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		mainTemplate.hooks.localVars.tap( | 
					
						
							|  |  |  | 			"JsonpMainTemplatePlugin", | 
					
						
							| 
									
										
										
										
											2018-04-16 16:27:22 +08:00
										 |  |  | 			(source, chunk, hash) => { | 
					
						
							| 
									
										
										
										
											2018-05-04 16:43:49 +08:00
										 |  |  | 				const extraCode = []; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				if (needChunkLoadingCode(chunk)) { | 
					
						
							| 
									
										
										
										
											2018-05-04 16:43:49 +08:00
										 |  |  | 					extraCode.push( | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						"", | 
					
						
							|  |  |  | 						"// object to store loaded and loading chunks", | 
					
						
							| 
									
										
										
										
											2018-04-16 16:27:22 +08:00
										 |  |  | 						"// undefined = chunk not loaded, null = chunk preloaded/prefetched", | 
					
						
							|  |  |  | 						"// Promise = chunk loading, 0 = chunk loaded", | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						"var installedChunks = {", | 
					
						
							|  |  |  | 						Template.indent( | 
					
						
							|  |  |  | 							chunk.ids.map(id => `${JSON.stringify(id)}: 0`).join(",\n") | 
					
						
							|  |  |  | 						), | 
					
						
							|  |  |  | 						"};", | 
					
						
							| 
									
										
										
										
											2018-05-04 16:43:49 +08:00
										 |  |  | 						"", | 
					
						
							|  |  |  | 						needEntryDeferringCode(chunk) ? "var deferredModules = [];" : "" | 
					
						
							|  |  |  | 					); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				if (needChunkOnDemandLoadingCode(chunk)) { | 
					
						
							|  |  |  | 					extraCode.push( | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						"", | 
					
						
							| 
									
										
										
										
											2018-04-16 16:27:22 +08:00
										 |  |  | 						"// script path function", | 
					
						
							|  |  |  | 						"function jsonpScriptSrc(chunkId) {", | 
					
						
							|  |  |  | 						Template.indent([ | 
					
						
							| 
									
										
										
										
											2018-11-05 20:13:05 +08:00
										 |  |  | 							`return __webpack_require__.p + ${getScriptSrcPath( | 
					
						
							| 
									
										
										
										
											2018-04-16 16:27:22 +08:00
										 |  |  | 								hash, | 
					
						
							|  |  |  | 								chunk, | 
					
						
							|  |  |  | 								"chunkId" | 
					
						
							|  |  |  | 							)}`
 | 
					
						
							|  |  |  | 						]), | 
					
						
							| 
									
										
										
										
											2018-05-04 16:43:49 +08:00
										 |  |  | 						"}" | 
					
						
							|  |  |  | 					); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-05-04 16:43:49 +08:00
										 |  |  | 				if (extraCode.length === 0) return source; | 
					
						
							|  |  |  | 				return Template.asString([source, ...extraCode]); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2018-04-16 16:27:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-31 00:54:54 +08:00
										 |  |  | 		const { | 
					
						
							|  |  |  | 			jsonpScript, | 
					
						
							|  |  |  | 			linkPreload, | 
					
						
							|  |  |  | 			linkPrefetch | 
					
						
							|  |  |  | 		} = JsonpMainTemplatePlugin.getMainTemplateHooks(mainTemplate); | 
					
						
							| 
									
										
										
										
											2018-07-06 19:11:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-31 00:54:54 +08:00
										 |  |  | 		const { hotBootstrap } = HotModuleReplacementPlugin.getMainTemplateHooks( | 
					
						
							|  |  |  | 			mainTemplate | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2018-04-16 16:27:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-31 00:54:54 +08:00
										 |  |  | 		jsonpScript.tap("JsonpMainTemplatePlugin", (_, chunk, hash) => { | 
					
						
							|  |  |  | 			const crossOriginLoading = mainTemplate.outputOptions.crossOriginLoading; | 
					
						
							|  |  |  | 			const chunkLoadTimeout = mainTemplate.outputOptions.chunkLoadTimeout; | 
					
						
							|  |  |  | 			const jsonpScriptType = mainTemplate.outputOptions.jsonpScriptType; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			return Template.asString([ | 
					
						
							|  |  |  | 				"var script = document.createElement('script');", | 
					
						
							|  |  |  | 				"var onScriptComplete;", | 
					
						
							|  |  |  | 				jsonpScriptType | 
					
						
							|  |  |  | 					? `script.type = ${JSON.stringify(jsonpScriptType)};` | 
					
						
							|  |  |  | 					: "", | 
					
						
							|  |  |  | 				"script.charset = 'utf-8';", | 
					
						
							|  |  |  | 				`script.timeout = ${chunkLoadTimeout / 1000};`, | 
					
						
							| 
									
										
										
										
											2018-11-05 20:13:05 +08:00
										 |  |  | 				`if (__webpack_require__.nc) {`, | 
					
						
							| 
									
										
										
										
											2018-07-31 00:54:54 +08:00
										 |  |  | 				Template.indent( | 
					
						
							| 
									
										
										
										
											2018-11-05 20:13:05 +08:00
										 |  |  | 					`script.setAttribute("nonce", __webpack_require__.nc);` | 
					
						
							| 
									
										
										
										
											2018-07-31 00:54:54 +08:00
										 |  |  | 				), | 
					
						
							|  |  |  | 				"}", | 
					
						
							|  |  |  | 				"script.src = jsonpScriptSrc(chunkId);", | 
					
						
							|  |  |  | 				crossOriginLoading | 
					
						
							|  |  |  | 					? Template.asString([ | 
					
						
							|  |  |  | 							"if (script.src.indexOf(window.location.origin + '/') !== 0) {", | 
					
						
							|  |  |  | 							Template.indent( | 
					
						
							|  |  |  | 								`script.crossOrigin = ${JSON.stringify(crossOriginLoading)};` | 
					
						
							|  |  |  | 							), | 
					
						
							|  |  |  | 							"}" | 
					
						
							|  |  |  | 					  ]) | 
					
						
							|  |  |  | 					: "", | 
					
						
							|  |  |  | 				"onScriptComplete = function (event) {", | 
					
						
							|  |  |  | 				Template.indent([ | 
					
						
							|  |  |  | 					"// avoid mem leaks in IE.", | 
					
						
							|  |  |  | 					"script.onerror = script.onload = null;", | 
					
						
							|  |  |  | 					"clearTimeout(timeout);", | 
					
						
							|  |  |  | 					"var chunk = installedChunks[chunkId];", | 
					
						
							|  |  |  | 					"if(chunk !== 0) {", | 
					
						
							| 
									
										
										
										
											2017-12-07 16:42:33 +08:00
										 |  |  | 					Template.indent([ | 
					
						
							| 
									
										
										
										
											2018-07-31 00:54:54 +08:00
										 |  |  | 						"if(chunk) {", | 
					
						
							| 
									
										
										
										
											2017-12-07 16:42:33 +08:00
										 |  |  | 						Template.indent([ | 
					
						
							| 
									
										
										
										
											2018-07-31 00:54:54 +08:00
										 |  |  | 							"var errorType = event && (event.type === 'load' ? 'missing' : event.type);", | 
					
						
							|  |  |  | 							"var realSrc = event && event.target && event.target.src;", | 
					
						
							|  |  |  | 							"var error = new Error('Loading chunk ' + chunkId + ' failed.\\n(' + errorType + ': ' + realSrc + ')');", | 
					
						
							|  |  |  | 							"error.type = errorType;", | 
					
						
							|  |  |  | 							"error.request = realSrc;", | 
					
						
							|  |  |  | 							"chunk[1](error);" | 
					
						
							| 
									
										
										
										
											2017-10-30 20:56:57 +08:00
										 |  |  | 						]), | 
					
						
							| 
									
										
										
										
											2018-07-31 00:54:54 +08:00
										 |  |  | 						"}", | 
					
						
							|  |  |  | 						"installedChunks[chunkId] = undefined;" | 
					
						
							| 
									
										
										
										
											2018-05-28 15:58:17 +08:00
										 |  |  | 					]), | 
					
						
							| 
									
										
										
										
											2018-07-31 00:54:54 +08:00
										 |  |  | 					"}" | 
					
						
							|  |  |  | 				]), | 
					
						
							|  |  |  | 				"};", | 
					
						
							|  |  |  | 				"var timeout = setTimeout(function(){", | 
					
						
							|  |  |  | 				Template.indent([ | 
					
						
							|  |  |  | 					"onScriptComplete({ type: 'timeout', target: script });" | 
					
						
							|  |  |  | 				]), | 
					
						
							|  |  |  | 				`}, ${chunkLoadTimeout});`, | 
					
						
							|  |  |  | 				"script.onerror = script.onload = onScriptComplete;" | 
					
						
							|  |  |  | 			]); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 		linkPreload.tap("JsonpMainTemplatePlugin", (_, chunk, hash) => { | 
					
						
							|  |  |  | 			const crossOriginLoading = mainTemplate.outputOptions.crossOriginLoading; | 
					
						
							|  |  |  | 			const jsonpScriptType = mainTemplate.outputOptions.jsonpScriptType; | 
					
						
							| 
									
										
										
										
											2018-04-16 16:27:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-31 00:54:54 +08:00
										 |  |  | 			return Template.asString([ | 
					
						
							|  |  |  | 				"var link = document.createElement('link');", | 
					
						
							|  |  |  | 				jsonpScriptType | 
					
						
							|  |  |  | 					? `link.type = ${JSON.stringify(jsonpScriptType)};` | 
					
						
							|  |  |  | 					: "", | 
					
						
							|  |  |  | 				"link.charset = 'utf-8';", | 
					
						
							| 
									
										
										
										
											2018-11-05 20:13:05 +08:00
										 |  |  | 				`if (__webpack_require__.nc) {`, | 
					
						
							|  |  |  | 				Template.indent(`link.setAttribute("nonce", __webpack_require__.nc);`), | 
					
						
							| 
									
										
										
										
											2018-07-31 00:54:54 +08:00
										 |  |  | 				"}", | 
					
						
							|  |  |  | 				'link.rel = "preload";', | 
					
						
							|  |  |  | 				'link.as = "script";', | 
					
						
							|  |  |  | 				"link.href = jsonpScriptSrc(chunkId);", | 
					
						
							|  |  |  | 				crossOriginLoading | 
					
						
							|  |  |  | 					? Template.asString([ | 
					
						
							|  |  |  | 							"if (link.href.indexOf(window.location.origin + '/') !== 0) {", | 
					
						
							|  |  |  | 							Template.indent( | 
					
						
							|  |  |  | 								`link.crossOrigin = ${JSON.stringify(crossOriginLoading)};` | 
					
						
							|  |  |  | 							), | 
					
						
							|  |  |  | 							"}" | 
					
						
							|  |  |  | 					  ]) | 
					
						
							|  |  |  | 					: "" | 
					
						
							|  |  |  | 			]); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 		linkPrefetch.tap("JsonpMainTemplatePlugin", (_, chunk, hash) => { | 
					
						
							|  |  |  | 			const crossOriginLoading = mainTemplate.outputOptions.crossOriginLoading; | 
					
						
							| 
									
										
										
										
											2018-06-04 23:49:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-31 00:54:54 +08:00
										 |  |  | 			return Template.asString([ | 
					
						
							|  |  |  | 				"var link = document.createElement('link');", | 
					
						
							|  |  |  | 				crossOriginLoading | 
					
						
							|  |  |  | 					? `link.crossOrigin = ${JSON.stringify(crossOriginLoading)};` | 
					
						
							|  |  |  | 					: "", | 
					
						
							| 
									
										
										
										
											2018-11-05 20:13:05 +08:00
										 |  |  | 				`if (__webpack_require__.nc) {`, | 
					
						
							|  |  |  | 				Template.indent(`link.setAttribute("nonce", __webpack_require__.nc);`), | 
					
						
							| 
									
										
										
										
											2018-07-31 00:54:54 +08:00
										 |  |  | 				"}", | 
					
						
							|  |  |  | 				'link.rel = "prefetch";', | 
					
						
							|  |  |  | 				'link.as = "script";', | 
					
						
							|  |  |  | 				"link.href = jsonpScriptSrc(chunkId);" | 
					
						
							|  |  |  | 			]); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2018-04-16 16:27:22 +08:00
										 |  |  | 		mainTemplate.hooks.requireEnsure.tap( | 
					
						
							|  |  |  | 			"JsonpMainTemplatePlugin load", | 
					
						
							| 
									
										
										
										
											2018-08-14 22:40:37 +08:00
										 |  |  | 			(source, chunkIdExpression, { chunk, hash }) => { | 
					
						
							| 
									
										
										
										
											2017-12-07 16:42:33 +08:00
										 |  |  | 				return Template.asString([ | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | 					source, | 
					
						
							|  |  |  | 					"", | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					"// JSONP chunk loading for javascript", | 
					
						
							|  |  |  | 					"", | 
					
						
							| 
									
										
										
										
											2018-08-14 22:40:37 +08:00
										 |  |  | 					`var installedChunkData = installedChunks[${chunkIdExpression}];`, | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					'if(installedChunkData !== 0) { // 0 means "already installed".', | 
					
						
							| 
									
										
										
										
											2017-12-07 16:42:33 +08:00
										 |  |  | 					Template.indent([ | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						"", | 
					
						
							|  |  |  | 						'// a Promise means "currently loading".', | 
					
						
							|  |  |  | 						"if(installedChunkData) {", | 
					
						
							|  |  |  | 						Template.indent(["promises.push(installedChunkData[2]);"]), | 
					
						
							|  |  |  | 						"} else {", | 
					
						
							| 
									
										
										
										
											2018-01-20 00:06:59 +08:00
										 |  |  | 						Template.indent([ | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 							"// setup Promise in chunk cache", | 
					
						
							|  |  |  | 							"var promise = new Promise(function(resolve, reject) {", | 
					
						
							| 
									
										
										
										
											2017-12-07 16:42:33 +08:00
										 |  |  | 							Template.indent([ | 
					
						
							| 
									
										
										
										
											2018-08-14 22:40:37 +08:00
										 |  |  | 								`installedChunkData = installedChunks[${chunkIdExpression}] = [resolve, reject];` | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | 							]), | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 							"});", | 
					
						
							|  |  |  | 							"promises.push(installedChunkData[2] = promise);", | 
					
						
							|  |  |  | 							"", | 
					
						
							|  |  |  | 							"// start chunk loading", | 
					
						
							|  |  |  | 							"var head = document.getElementsByTagName('head')[0];", | 
					
						
							| 
									
										
										
										
											2018-07-31 00:54:54 +08:00
										 |  |  | 							jsonpScript.call("", chunk, hash), | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 							"head.appendChild(script);" | 
					
						
							| 
									
										
										
										
											2018-01-20 00:06:59 +08:00
										 |  |  | 						]), | 
					
						
							|  |  |  | 						"}" | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					]), | 
					
						
							|  |  |  | 					"}" | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | 				]); | 
					
						
							| 
									
										
										
										
											2014-08-22 19:51:24 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		); | 
					
						
							| 
									
										
										
										
											2018-04-16 16:27:22 +08:00
										 |  |  | 		mainTemplate.hooks.requireEnsure.tap( | 
					
						
							| 
									
										
										
										
											2018-08-14 22:40:37 +08:00
										 |  |  | 			// TODO typing are broken
 | 
					
						
							| 
									
										
										
										
											2018-08-16 19:12:41 +08:00
										 |  |  | 			/** @type {TODO} */ ({ | 
					
						
							| 
									
										
										
										
											2018-04-16 16:27:22 +08:00
										 |  |  | 				name: "JsonpMainTemplatePlugin preload", | 
					
						
							|  |  |  | 				stage: 10 | 
					
						
							| 
									
										
										
										
											2018-08-14 22:40:37 +08:00
										 |  |  | 			}), | 
					
						
							|  |  |  | 			(source, chunkIdExpression, { chunk, hash }) => { | 
					
						
							| 
									
										
										
										
											2018-08-14 17:18:22 +08:00
										 |  |  | 				const chunkMap = chunk.getChildIdsByOrdersMap( | 
					
						
							|  |  |  | 					this.compilation.chunkGraph | 
					
						
							|  |  |  | 				).preload; | 
					
						
							| 
									
										
										
										
											2018-04-16 16:27:22 +08:00
										 |  |  | 				if (!chunkMap || Object.keys(chunkMap).length === 0) return source; | 
					
						
							|  |  |  | 				return Template.asString([ | 
					
						
							|  |  |  | 					source, | 
					
						
							|  |  |  | 					"", | 
					
						
							|  |  |  | 					"// chunk preloadng for javascript", | 
					
						
							|  |  |  | 					"", | 
					
						
							| 
									
										
										
										
											2018-06-04 23:49:55 +08:00
										 |  |  | 					`var chunkPreloadMap = ${JSON.stringify(chunkMap, null, "\t")};`, | 
					
						
							| 
									
										
										
										
											2018-04-16 16:27:22 +08:00
										 |  |  | 					"", | 
					
						
							| 
									
										
										
										
											2018-08-14 22:40:37 +08:00
										 |  |  | 					`var chunkPreloadData = chunkPreloadMap[${chunkIdExpression}];`, | 
					
						
							| 
									
										
										
										
											2018-04-17 01:35:02 +08:00
										 |  |  | 					"if(chunkPreloadData) {", | 
					
						
							| 
									
										
										
										
											2018-04-16 16:27:22 +08:00
										 |  |  | 					Template.indent([ | 
					
						
							| 
									
										
										
										
											2018-04-17 01:35:02 +08:00
										 |  |  | 						"var head = document.getElementsByTagName('head')[0];", | 
					
						
							|  |  |  | 						"chunkPreloadData.forEach(function(chunkId) {", | 
					
						
							| 
									
										
										
										
											2018-04-16 16:27:22 +08:00
										 |  |  | 						Template.indent([ | 
					
						
							| 
									
										
										
										
											2018-04-17 01:35:02 +08:00
										 |  |  | 							"if(installedChunks[chunkId] === undefined) {", | 
					
						
							| 
									
										
										
										
											2018-04-16 16:27:22 +08:00
										 |  |  | 							Template.indent([ | 
					
						
							| 
									
										
										
										
											2018-04-17 01:35:02 +08:00
										 |  |  | 								"installedChunks[chunkId] = null;", | 
					
						
							| 
									
										
										
										
											2018-07-31 00:54:54 +08:00
										 |  |  | 								linkPreload.call("", chunk, hash), | 
					
						
							| 
									
										
										
										
											2018-04-17 01:35:02 +08:00
										 |  |  | 								"head.appendChild(link);" | 
					
						
							| 
									
										
										
										
											2018-04-16 16:27:22 +08:00
										 |  |  | 							]), | 
					
						
							| 
									
										
										
										
											2018-04-17 01:35:02 +08:00
										 |  |  | 							"}" | 
					
						
							| 
									
										
										
										
											2018-04-16 16:27:22 +08:00
										 |  |  | 						]), | 
					
						
							| 
									
										
										
										
											2018-04-17 01:35:02 +08:00
										 |  |  | 						"});" | 
					
						
							| 
									
										
										
										
											2018-04-16 16:27:22 +08:00
										 |  |  | 					]), | 
					
						
							|  |  |  | 					"}" | 
					
						
							|  |  |  | 				]); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		mainTemplate.hooks.requireExtensions.tap( | 
					
						
							|  |  |  | 			"JsonpMainTemplatePlugin", | 
					
						
							| 
									
										
										
										
											2018-08-14 22:40:37 +08:00
										 |  |  | 			(source, { chunk }) => { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				if (!needChunkOnDemandLoadingCode(chunk)) return source; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-07 16:42:33 +08:00
										 |  |  | 				return Template.asString([ | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					source, | 
					
						
							| 
									
										
										
										
											2017-04-23 00:59:15 +08:00
										 |  |  | 					"", | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					"// on error function for async loading", | 
					
						
							| 
									
										
										
										
											2018-11-05 20:13:05 +08:00
										 |  |  | 					`__webpack_require__.oe = function(err) { console.error(err); throw err; };` | 
					
						
							| 
									
										
										
										
											2017-04-23 00:59:15 +08:00
										 |  |  | 				]); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		); | 
					
						
							|  |  |  | 		mainTemplate.hooks.bootstrap.tap( | 
					
						
							|  |  |  | 			"JsonpMainTemplatePlugin", | 
					
						
							| 
									
										
										
										
											2018-09-15 19:10:58 +08:00
										 |  |  | 			(source, renderContext) => { | 
					
						
							| 
									
										
										
										
											2018-08-14 22:40:37 +08:00
										 |  |  | 				const { chunk, hash } = renderContext; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				if (needChunkLoadingCode(chunk)) { | 
					
						
							|  |  |  | 					const withDefer = needEntryDeferringCode(chunk); | 
					
						
							| 
									
										
										
										
											2018-06-02 01:51:39 +08:00
										 |  |  | 					const withPrefetch = needPrefetchingCode(chunk); | 
					
						
							| 
									
										
										
										
											2018-01-20 00:06:59 +08:00
										 |  |  | 					return Template.asString([ | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						source, | 
					
						
							|  |  |  | 						"", | 
					
						
							|  |  |  | 						"// install a JSONP callback for chunk loading", | 
					
						
							|  |  |  | 						"function webpackJsonpCallback(data) {", | 
					
						
							|  |  |  | 						Template.indent([ | 
					
						
							|  |  |  | 							"var chunkIds = data[0];", | 
					
						
							| 
									
										
										
										
											2018-03-21 13:48:08 +08:00
										 |  |  | 							"var moreModules = data[1];", | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 							withDefer ? "var executeModules = data[2];" : "", | 
					
						
							| 
									
										
										
										
											2018-06-04 23:49:55 +08:00
										 |  |  | 							withPrefetch ? "var prefetchChunks = data[3] || [];" : "", | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 							'// add "moreModules" to the modules object,', | 
					
						
							|  |  |  | 							'// then flag all "chunkIds" as loaded and fire callback', | 
					
						
							|  |  |  | 							"var moduleId, chunkId, i = 0, resolves = [];", | 
					
						
							|  |  |  | 							"for(;i < chunkIds.length; i++) {", | 
					
						
							|  |  |  | 							Template.indent([ | 
					
						
							|  |  |  | 								"chunkId = chunkIds[i];", | 
					
						
							|  |  |  | 								"if(installedChunks[chunkId]) {", | 
					
						
							|  |  |  | 								Template.indent("resolves.push(installedChunks[chunkId][0]);"), | 
					
						
							|  |  |  | 								"}", | 
					
						
							|  |  |  | 								"installedChunks[chunkId] = 0;" | 
					
						
							|  |  |  | 							]), | 
					
						
							|  |  |  | 							"}", | 
					
						
							|  |  |  | 							"for(moduleId in moreModules) {", | 
					
						
							|  |  |  | 							Template.indent([ | 
					
						
							|  |  |  | 								"if(Object.prototype.hasOwnProperty.call(moreModules, moduleId)) {", | 
					
						
							|  |  |  | 								Template.indent( | 
					
						
							|  |  |  | 									mainTemplate.renderAddModule( | 
					
						
							|  |  |  | 										"moduleId", | 
					
						
							| 
									
										
										
										
											2018-08-14 22:40:37 +08:00
										 |  |  | 										"moreModules[moduleId]", | 
					
						
							|  |  |  | 										renderContext | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 									) | 
					
						
							|  |  |  | 								), | 
					
						
							|  |  |  | 								"}" | 
					
						
							|  |  |  | 							]), | 
					
						
							|  |  |  | 							"}", | 
					
						
							|  |  |  | 							"if(parentJsonpFunction) parentJsonpFunction(data);", | 
					
						
							| 
									
										
										
										
											2018-06-04 23:49:55 +08:00
										 |  |  | 							withPrefetch | 
					
						
							|  |  |  | 								? Template.asString([ | 
					
						
							| 
									
										
										
										
											2018-06-02 01:51:39 +08:00
										 |  |  | 										"// chunk prefetching for javascript", | 
					
						
							|  |  |  | 										"var head = document.getElementsByTagName('head')[0];", | 
					
						
							|  |  |  | 										"prefetchChunks.forEach(function(chunkId) {", | 
					
						
							|  |  |  | 										Template.indent([ | 
					
						
							|  |  |  | 											"if(installedChunks[chunkId] === undefined) {", | 
					
						
							|  |  |  | 											Template.indent([ | 
					
						
							|  |  |  | 												"installedChunks[chunkId] = null;", | 
					
						
							| 
									
										
										
										
											2018-07-31 00:54:54 +08:00
										 |  |  | 												linkPrefetch.call("", chunk, hash), | 
					
						
							| 
									
										
										
										
											2018-06-02 01:51:39 +08:00
										 |  |  | 												"head.appendChild(link);" | 
					
						
							|  |  |  | 											]), | 
					
						
							|  |  |  | 											"}" | 
					
						
							|  |  |  | 										]), | 
					
						
							|  |  |  | 										"});" | 
					
						
							| 
									
										
										
										
											2018-06-04 23:49:55 +08:00
										 |  |  | 								  ]) | 
					
						
							|  |  |  | 								: "", | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 							"while(resolves.length) {", | 
					
						
							|  |  |  | 							Template.indent("resolves.shift()();"), | 
					
						
							|  |  |  | 							"}", | 
					
						
							|  |  |  | 							withDefer | 
					
						
							|  |  |  | 								? Template.asString([ | 
					
						
							|  |  |  | 										"", | 
					
						
							|  |  |  | 										"// add entry modules from loaded chunk to deferred list", | 
					
						
							|  |  |  | 										"deferredModules.push.apply(deferredModules, executeModules || []);", | 
					
						
							|  |  |  | 										"", | 
					
						
							|  |  |  | 										"// run deferred modules when all chunks ready", | 
					
						
							|  |  |  | 										"return checkDeferredModules();" | 
					
						
							| 
									
										
										
										
											2018-03-26 22:56:10 +08:00
										 |  |  | 								  ]) | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 								: "" | 
					
						
							|  |  |  | 						]), | 
					
						
							|  |  |  | 						"};", | 
					
						
							|  |  |  | 						withDefer | 
					
						
							|  |  |  | 							? Template.asString([ | 
					
						
							|  |  |  | 									"function checkDeferredModules() {", | 
					
						
							|  |  |  | 									Template.indent([ | 
					
						
							|  |  |  | 										"var result;", | 
					
						
							|  |  |  | 										"for(var i = 0; i < deferredModules.length; i++) {", | 
					
						
							|  |  |  | 										Template.indent([ | 
					
						
							|  |  |  | 											"var deferredModule = deferredModules[i];", | 
					
						
							| 
									
										
										
										
											2018-03-13 16:45:23 +08:00
										 |  |  | 											"var fulfilled = true;", | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 											"for(var j = 1; j < deferredModule.length; j++) {", | 
					
						
							|  |  |  | 											Template.indent([ | 
					
						
							|  |  |  | 												"var depId = deferredModule[j];", | 
					
						
							| 
									
										
										
										
											2018-03-13 16:45:23 +08:00
										 |  |  | 												"if(installedChunks[depId] !== 0) fulfilled = false;" | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 											]), | 
					
						
							|  |  |  | 											"}", | 
					
						
							| 
									
										
										
										
											2018-03-13 16:45:23 +08:00
										 |  |  | 											"if(fulfilled) {", | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 											Template.indent([ | 
					
						
							|  |  |  | 												"deferredModules.splice(i--, 1);", | 
					
						
							|  |  |  | 												"result = " + | 
					
						
							| 
									
										
										
										
											2018-11-05 20:13:05 +08:00
										 |  |  | 													"__webpack_require__(" + | 
					
						
							|  |  |  | 													"__webpack_require__.s = deferredModule[0]);" | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 											]), | 
					
						
							|  |  |  | 											"}" | 
					
						
							|  |  |  | 										]), | 
					
						
							|  |  |  | 										"}", | 
					
						
							|  |  |  | 										"return result;" | 
					
						
							|  |  |  | 									]), | 
					
						
							|  |  |  | 									"}" | 
					
						
							| 
									
										
										
										
											2018-03-26 22:56:10 +08:00
										 |  |  | 							  ]) | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 							: "" | 
					
						
							| 
									
										
										
										
											2018-01-20 00:06:59 +08:00
										 |  |  | 					]); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				} | 
					
						
							|  |  |  | 				return source; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 		mainTemplate.hooks.beforeStartup.tap( | 
					
						
							|  |  |  | 			"JsonpMainTemplatePlugin", | 
					
						
							|  |  |  | 			(source, chunk, hash) => { | 
					
						
							|  |  |  | 				if (needChunkLoadingCode(chunk)) { | 
					
						
							|  |  |  | 					var jsonpFunction = mainTemplate.outputOptions.jsonpFunction; | 
					
						
							|  |  |  | 					var globalObject = mainTemplate.outputOptions.globalObject; | 
					
						
							| 
									
										
										
										
											2018-01-20 00:06:59 +08:00
										 |  |  | 					return Template.asString([ | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						`var jsonpArray = ${globalObject}[${JSON.stringify( | 
					
						
							|  |  |  | 							jsonpFunction | 
					
						
							|  |  |  | 						)}] = ${globalObject}[${JSON.stringify(jsonpFunction)}] || [];`,
 | 
					
						
							|  |  |  | 						"var oldJsonpFunction = jsonpArray.push.bind(jsonpArray);", | 
					
						
							|  |  |  | 						"jsonpArray.push = webpackJsonpCallback;", | 
					
						
							|  |  |  | 						"jsonpArray = jsonpArray.slice();", | 
					
						
							|  |  |  | 						"for(var i = 0; i < jsonpArray.length; i++) webpackJsonpCallback(jsonpArray[i]);", | 
					
						
							|  |  |  | 						"var parentJsonpFunction = oldJsonpFunction;", | 
					
						
							|  |  |  | 						"", | 
					
						
							|  |  |  | 						source | 
					
						
							| 
									
										
										
										
											2018-01-20 00:06:59 +08:00
										 |  |  | 					]); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				return source; | 
					
						
							| 
									
										
										
										
											2018-01-20 00:06:59 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		); | 
					
						
							| 
									
										
										
										
											2018-06-05 00:02:17 +08:00
										 |  |  | 		mainTemplate.hooks.beforeStartup.tap( | 
					
						
							| 
									
										
										
										
											2018-05-31 05:36:53 +08:00
										 |  |  | 			"JsonpMainTemplatePlugin", | 
					
						
							|  |  |  | 			(source, chunk, hash) => { | 
					
						
							| 
									
										
										
										
											2018-08-14 17:18:22 +08:00
										 |  |  | 				const chunkGraph = this.compilation.chunkGraph; | 
					
						
							| 
									
										
										
										
											2018-08-22 17:49:27 +08:00
										 |  |  | 				const prefetchChunks = chunk.getChildIdsByOrders(chunkGraph).prefetch; | 
					
						
							| 
									
										
										
										
											2018-06-02 01:51:39 +08:00
										 |  |  | 				if ( | 
					
						
							|  |  |  | 					needChunkLoadingCode(chunk) && | 
					
						
							|  |  |  | 					prefetchChunks && | 
					
						
							|  |  |  | 					prefetchChunks.length | 
					
						
							|  |  |  | 				) { | 
					
						
							| 
									
										
										
										
											2018-05-31 05:36:53 +08:00
										 |  |  | 					return Template.asString([ | 
					
						
							|  |  |  | 						source, | 
					
						
							| 
									
										
										
										
											2018-06-04 23:49:55 +08:00
										 |  |  | 						`webpackJsonpCallback([[], {}, 0, ${JSON.stringify( | 
					
						
							|  |  |  | 							prefetchChunks | 
					
						
							|  |  |  | 						)}]);`
 | 
					
						
							| 
									
										
										
										
											2018-05-31 05:36:53 +08:00
										 |  |  | 					]); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				return source; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		mainTemplate.hooks.startup.tap( | 
					
						
							|  |  |  | 			"JsonpMainTemplatePlugin", | 
					
						
							| 
									
										
										
										
											2018-08-14 22:40:37 +08:00
										 |  |  | 			(source, { chunk, chunkGraph }) => { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				if (needEntryDeferringCode(chunk)) { | 
					
						
							| 
									
										
										
										
											2018-08-14 22:40:37 +08:00
										 |  |  | 					if (chunkGraph.getNumberOfEntryModules(chunk) > 0) { | 
					
						
							|  |  |  | 						const entries = getEntryInfo(chunkGraph, chunk); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						return Template.asString([ | 
					
						
							|  |  |  | 							"// add entry module to deferred list", | 
					
						
							|  |  |  | 							`deferredModules.push(${entries | 
					
						
							|  |  |  | 								.map(e => JSON.stringify(e)) | 
					
						
							|  |  |  | 								.join(", ")});`,
 | 
					
						
							|  |  |  | 							"// run deferred modules when ready", | 
					
						
							|  |  |  | 							"return checkDeferredModules();" | 
					
						
							|  |  |  | 						]); | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						return Template.asString([ | 
					
						
							|  |  |  | 							"// run deferred modules from other chunks", | 
					
						
							|  |  |  | 							"checkDeferredModules();" | 
					
						
							|  |  |  | 						]); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				return source; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2018-07-31 00:54:54 +08:00
										 |  |  | 		hotBootstrap.tap("JsonpMainTemplatePlugin", (source, chunk, hash) => { | 
					
						
							|  |  |  | 			const globalObject = mainTemplate.outputOptions.globalObject; | 
					
						
							|  |  |  | 			const hotUpdateChunkFilename = | 
					
						
							|  |  |  | 				mainTemplate.outputOptions.hotUpdateChunkFilename; | 
					
						
							|  |  |  | 			const hotUpdateMainFilename = | 
					
						
							|  |  |  | 				mainTemplate.outputOptions.hotUpdateMainFilename; | 
					
						
							|  |  |  | 			const crossOriginLoading = mainTemplate.outputOptions.crossOriginLoading; | 
					
						
							|  |  |  | 			const hotUpdateFunction = mainTemplate.outputOptions.hotUpdateFunction; | 
					
						
							|  |  |  | 			const currentHotUpdateChunkFilename = mainTemplate.getAssetPath( | 
					
						
							|  |  |  | 				JSON.stringify(hotUpdateChunkFilename), | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`, | 
					
						
							|  |  |  | 					hashWithLength: length => | 
					
						
							|  |  |  | 						`" + ${mainTemplate.renderCurrentHashCode(hash, length)} + "`, | 
					
						
							|  |  |  | 					chunk: { | 
					
						
							|  |  |  | 						id: '" + chunkId + "' | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2018-07-31 00:54:54 +08:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 			const currentHotUpdateMainFilename = mainTemplate.getAssetPath( | 
					
						
							|  |  |  | 				JSON.stringify(hotUpdateMainFilename), | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`, | 
					
						
							|  |  |  | 					hashWithLength: length => | 
					
						
							|  |  |  | 						`" + ${mainTemplate.renderCurrentHashCode(hash, length)} + "` | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 			const runtimeSource = Template.getFunctionContent( | 
					
						
							|  |  |  | 				require("./JsonpMainTemplate.runtime") | 
					
						
							|  |  |  | 			) | 
					
						
							|  |  |  | 				.replace(/\/\/\$semicolon/g, ";") | 
					
						
							|  |  |  | 				.replace( | 
					
						
							|  |  |  | 					/\$crossOriginLoading\$/g, | 
					
						
							| 
									
										
										
										
											2018-10-18 02:38:00 +08:00
										 |  |  | 					crossOriginLoading ? JSON.stringify(crossOriginLoading) : "null" | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				) | 
					
						
							| 
									
										
										
										
											2018-07-31 00:54:54 +08:00
										 |  |  | 				.replace(/\$hotMainFilename\$/g, currentHotUpdateMainFilename) | 
					
						
							|  |  |  | 				.replace(/\$hotChunkFilename\$/g, currentHotUpdateChunkFilename) | 
					
						
							|  |  |  | 				.replace(/\$hash\$/g, JSON.stringify(hash)); | 
					
						
							|  |  |  | 			return `${source}
 | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | function hotDisposeChunk(chunkId) { | 
					
						
							|  |  |  | 	delete installedChunks[chunkId]; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | var parentHotUpdateCallback = ${globalObject}[${JSON.stringify( | 
					
						
							| 
									
										
										
										
											2018-07-31 00:54:54 +08:00
										 |  |  | 				hotUpdateFunction | 
					
						
							|  |  |  | 			)}]; | 
					
						
							| 
									
										
										
										
											2017-12-28 01:46:37 +08:00
										 |  |  | ${globalObject}[${JSON.stringify(hotUpdateFunction)}] = ${runtimeSource}`;
 | 
					
						
							| 
									
										
										
										
											2018-07-31 00:54:54 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-12-23 14:13:13 +08:00
										 |  |  | 		mainTemplate.hooks.hash.tap("JsonpMainTemplatePlugin", hash => { | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | 			hash.update("jsonp"); | 
					
						
							| 
									
										
										
										
											2018-09-14 05:25:26 +08:00
										 |  |  | 			hash.update("6"); | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | module.exports = JsonpMainTemplatePlugin; |