| 
									
										
										
										
											2013-01-31 01:49:25 +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-01-13 11:53:12 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | const { SyncWaterfallHook } = require("tapable"); | 
					
						
							|  |  |  | const Compilation = require("../Compilation"); | 
					
						
							|  |  |  | const RuntimeGlobals = require("../RuntimeGlobals"); | 
					
						
							|  |  |  | const Template = require("../Template"); | 
					
						
							|  |  |  | const JsonpChunkLoadingRuntimeModule = require("./JsonpChunkLoadingRuntimeModule"); | 
					
						
							| 
									
										
										
										
											2017-01-13 11:53:12 +08:00
										 |  |  | const JsonpChunkTemplatePlugin = require("./JsonpChunkTemplatePlugin"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | /** @typedef {import("../Chunk")} Chunk */ | 
					
						
							|  |  |  | /** @typedef {import("../Compilation")} Compilation */ | 
					
						
							| 
									
										
										
										
											2018-11-09 05:59:19 +08:00
										 |  |  | /** @typedef {import("../Compiler")} Compiler */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @typedef {Object} JsonpCompilationPluginHooks | 
					
						
							| 
									
										
										
										
											2018-12-09 19:54:17 +08:00
										 |  |  |  * @property {SyncWaterfallHook<[string, Chunk, string]>} jsonpScript | 
					
						
							|  |  |  |  * @property {SyncWaterfallHook<[string, Chunk, string]>} linkPreload | 
					
						
							|  |  |  |  * @property {SyncWaterfallHook<[string, Chunk, string]>} linkPrefetch | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** @type {WeakMap<Compilation, JsonpCompilationPluginHooks>} */ | 
					
						
							|  |  |  | const compilationHooksMap = new WeakMap(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-13 11:53:12 +08:00
										 |  |  | class JsonpTemplatePlugin { | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {Compilation} compilation the compilation | 
					
						
							|  |  |  | 	 * @returns {JsonpCompilationPluginHooks} hooks | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	static getCompilationHooks(compilation) { | 
					
						
							|  |  |  | 		if (!(compilation instanceof Compilation)) { | 
					
						
							|  |  |  | 			throw new TypeError( | 
					
						
							|  |  |  | 				"The 'compilation' argument must be an instance of MainTemplate" | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		let hooks = compilationHooksMap.get(compilation); | 
					
						
							|  |  |  | 		if (hooks === undefined) { | 
					
						
							|  |  |  | 			hooks = { | 
					
						
							|  |  |  | 				jsonpScript: new SyncWaterfallHook(["source", "chunk", "hash"]), | 
					
						
							|  |  |  | 				linkPreload: new SyncWaterfallHook(["source", "chunk", "hash"]), | 
					
						
							|  |  |  | 				linkPrefetch: new SyncWaterfallHook(["source", "chunk", "hash"]) | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 			compilationHooksMap.set(compilation, hooks); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return hooks; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-09 05:59:19 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-01-13 11:53:12 +08:00
										 |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		compiler.hooks.thisCompilation.tap("JsonpTemplatePlugin", compilation => { | 
					
						
							| 
									
										
										
										
											2018-08-14 17:18:22 +08:00
										 |  |  | 			new JsonpChunkTemplatePlugin(compilation).apply( | 
					
						
							|  |  |  | 				compilation.chunkTemplate | 
					
						
							|  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 			const mainTemplate = compilation.mainTemplate; | 
					
						
							|  |  |  | 			const needEntryDeferringCode = chunk => { | 
					
						
							|  |  |  | 				for (const chunkGroup of chunk.groupsIterable) { | 
					
						
							|  |  |  | 					if (chunkGroup.chunks.length > 1) return true; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				return false; | 
					
						
							|  |  |  | 			}; | 
					
						
							| 
									
										
										
										
											2019-05-20 20:46:31 +08:00
										 |  |  | 			const needPrefetchingCode = chunk => { | 
					
						
							|  |  |  | 				const allPrefetchChunks = chunk.getChildIdsByOrdersMap( | 
					
						
							|  |  |  | 					compilation.chunkGraph, | 
					
						
							|  |  |  | 					true | 
					
						
							|  |  |  | 				).prefetch; | 
					
						
							|  |  |  | 				return allPrefetchChunks && Object.keys(allPrefetchChunks).length; | 
					
						
							|  |  |  | 			}; | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			const { | 
					
						
							|  |  |  | 				jsonpScript, | 
					
						
							|  |  |  | 				linkPreload, | 
					
						
							|  |  |  | 				linkPrefetch | 
					
						
							|  |  |  | 			} = JsonpTemplatePlugin.getCompilationHooks(compilation); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			jsonpScript.tap("JsonpTemplatePlugin", (_, 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};`, | 
					
						
							|  |  |  | 					`if (${RuntimeGlobals.scriptNonce}) {`, | 
					
						
							|  |  |  | 					Template.indent( | 
					
						
							|  |  |  | 						`script.setAttribute("nonce", ${RuntimeGlobals.scriptNonce});` | 
					
						
							|  |  |  | 					), | 
					
						
							|  |  |  | 					"}", | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | 					`script.src = url;`, | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 					crossOriginLoading | 
					
						
							|  |  |  | 						? Template.asString([ | 
					
						
							|  |  |  | 								"if (script.src.indexOf(window.location.origin + '/') !== 0) {", | 
					
						
							|  |  |  | 								Template.indent( | 
					
						
							|  |  |  | 									`script.crossOrigin = ${JSON.stringify(crossOriginLoading)};` | 
					
						
							|  |  |  | 								), | 
					
						
							|  |  |  | 								"}" | 
					
						
							|  |  |  | 						  ]) | 
					
						
							|  |  |  | 						: "", | 
					
						
							| 
									
										
										
										
											2019-05-20 20:46:31 +08:00
										 |  |  | 					"// create error before stack unwound to get useful stacktrace later", | 
					
						
							|  |  |  | 					"var error = new Error();", | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 					"onScriptComplete = function (event) {", | 
					
						
							|  |  |  | 					Template.indent([ | 
					
						
							|  |  |  | 						"// avoid mem leaks in IE.", | 
					
						
							|  |  |  | 						"script.onerror = script.onload = null;", | 
					
						
							|  |  |  | 						"clearTimeout(timeout);", | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | 						"var reportError = loadingEnded();", | 
					
						
							|  |  |  | 						"if(reportError) {", | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 						Template.indent([ | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | 							"var errorType = event && (event.type === 'load' ? 'missing' : event.type);", | 
					
						
							|  |  |  | 							"var realSrc = event && event.target && event.target.src;", | 
					
						
							| 
									
										
										
										
											2019-05-20 20:46:31 +08:00
										 |  |  | 							"error.message = 'Loading chunk ' + chunkId + ' failed.\\n(' + errorType + ': ' + realSrc + ')';", | 
					
						
							| 
									
										
										
										
											2019-06-13 16:51:12 +08:00
										 |  |  | 							"error.name = 'ChunkLoadError';", | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | 							"error.type = errorType;", | 
					
						
							|  |  |  | 							"error.request = realSrc;", | 
					
						
							|  |  |  | 							"reportError(error);" | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 						]), | 
					
						
							|  |  |  | 						"}" | 
					
						
							|  |  |  | 					]), | 
					
						
							|  |  |  | 					"};", | 
					
						
							|  |  |  | 					"var timeout = setTimeout(function(){", | 
					
						
							|  |  |  | 					Template.indent([ | 
					
						
							|  |  |  | 						"onScriptComplete({ type: 'timeout', target: script });" | 
					
						
							|  |  |  | 					]), | 
					
						
							|  |  |  | 					`}, ${chunkLoadTimeout});`, | 
					
						
							|  |  |  | 					"script.onerror = script.onload = onScriptComplete;" | 
					
						
							|  |  |  | 				]); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 			linkPreload.tap("JsonpTemplatePlugin", (_, chunk, hash) => { | 
					
						
							|  |  |  | 				const crossOriginLoading = | 
					
						
							|  |  |  | 					mainTemplate.outputOptions.crossOriginLoading; | 
					
						
							|  |  |  | 				const jsonpScriptType = mainTemplate.outputOptions.jsonpScriptType; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				return Template.asString([ | 
					
						
							|  |  |  | 					"var link = document.createElement('link');", | 
					
						
							|  |  |  | 					jsonpScriptType | 
					
						
							|  |  |  | 						? `link.type = ${JSON.stringify(jsonpScriptType)};` | 
					
						
							|  |  |  | 						: "", | 
					
						
							|  |  |  | 					"link.charset = 'utf-8';", | 
					
						
							|  |  |  | 					`if (${RuntimeGlobals.scriptNonce}) {`, | 
					
						
							|  |  |  | 					Template.indent( | 
					
						
							|  |  |  | 						`link.setAttribute("nonce", ${RuntimeGlobals.scriptNonce});` | 
					
						
							|  |  |  | 					), | 
					
						
							|  |  |  | 					"}", | 
					
						
							|  |  |  | 					'link.rel = "preload";', | 
					
						
							|  |  |  | 					'link.as = "script";', | 
					
						
							| 
									
										
										
										
											2019-06-13 16:51:12 +08:00
										 |  |  | 					`link.href = ${RuntimeGlobals.publicPath} + ${RuntimeGlobals.getChunkScriptFilename}(chunkId);`, | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 					crossOriginLoading | 
					
						
							|  |  |  | 						? Template.asString([ | 
					
						
							|  |  |  | 								"if (link.href.indexOf(window.location.origin + '/') !== 0) {", | 
					
						
							|  |  |  | 								Template.indent( | 
					
						
							|  |  |  | 									`link.crossOrigin = ${JSON.stringify(crossOriginLoading)};` | 
					
						
							|  |  |  | 								), | 
					
						
							|  |  |  | 								"}" | 
					
						
							|  |  |  | 						  ]) | 
					
						
							|  |  |  | 						: "" | 
					
						
							|  |  |  | 				]); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 			linkPrefetch.tap("JsonpTemplatePlugin", (_, chunk, hash) => { | 
					
						
							|  |  |  | 				const crossOriginLoading = | 
					
						
							|  |  |  | 					mainTemplate.outputOptions.crossOriginLoading; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				return Template.asString([ | 
					
						
							|  |  |  | 					"var link = document.createElement('link');", | 
					
						
							|  |  |  | 					crossOriginLoading | 
					
						
							|  |  |  | 						? `link.crossOrigin = ${JSON.stringify(crossOriginLoading)};` | 
					
						
							|  |  |  | 						: "", | 
					
						
							|  |  |  | 					`if (${RuntimeGlobals.scriptNonce}) {`, | 
					
						
							|  |  |  | 					Template.indent( | 
					
						
							|  |  |  | 						`link.setAttribute("nonce", ${RuntimeGlobals.scriptNonce});` | 
					
						
							|  |  |  | 					), | 
					
						
							|  |  |  | 					"}", | 
					
						
							|  |  |  | 					'link.rel = "prefetch";', | 
					
						
							|  |  |  | 					'link.as = "script";', | 
					
						
							| 
									
										
										
										
											2019-06-13 16:51:12 +08:00
										 |  |  | 					`link.href = ${RuntimeGlobals.publicPath} + ${RuntimeGlobals.getChunkScriptFilename}(chunkId);` | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 				]); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			const onceForChunkSet = new WeakSet(); | 
					
						
							|  |  |  | 			const handler = (chunk, set) => { | 
					
						
							|  |  |  | 				if (onceForChunkSet.has(chunk)) return; | 
					
						
							|  |  |  | 				onceForChunkSet.add(chunk); | 
					
						
							|  |  |  | 				set.add(RuntimeGlobals.moduleFactories); | 
					
						
							|  |  |  | 				compilation.addRuntimeModule( | 
					
						
							|  |  |  | 					chunk, | 
					
						
							|  |  |  | 					new JsonpChunkLoadingRuntimeModule( | 
					
						
							|  |  |  | 						chunk, | 
					
						
							|  |  |  | 						compilation.chunkGraph, | 
					
						
							|  |  |  | 						compilation.outputOptions, | 
					
						
							|  |  |  | 						set, | 
					
						
							|  |  |  | 						jsonpScript, | 
					
						
							|  |  |  | 						linkPreload, | 
					
						
							|  |  |  | 						linkPrefetch | 
					
						
							|  |  |  | 					) | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 			compilation.hooks.runtimeRequirementInTree | 
					
						
							|  |  |  | 				.for(RuntimeGlobals.ensureChunkHandlers) | 
					
						
							|  |  |  | 				.tap("JsonpTemplatePlugin", handler); | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | 			compilation.hooks.runtimeRequirementInTree | 
					
						
							|  |  |  | 				.for(RuntimeGlobals.hmrDownloadUpdateHandlers) | 
					
						
							|  |  |  | 				.tap("JsonpTemplatePlugin", handler); | 
					
						
							|  |  |  | 			compilation.hooks.runtimeRequirementInTree | 
					
						
							|  |  |  | 				.for(RuntimeGlobals.hmrDownloadManifest) | 
					
						
							|  |  |  | 				.tap("JsonpTemplatePlugin", handler); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 			compilation.hooks.runtimeRequirementInTree | 
					
						
							|  |  |  | 				.for(RuntimeGlobals.ensureChunkHandlers) | 
					
						
							|  |  |  | 				.tap("JsonpTemplatePlugin", (chunk, set) => { | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | 					set.add(RuntimeGlobals.publicPath); | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 					set.add(RuntimeGlobals.getChunkScriptFilename); | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			compilation.hooks.runtimeRequirementInTree | 
					
						
							|  |  |  | 				.for(RuntimeGlobals.hmrDownloadUpdateHandlers) | 
					
						
							|  |  |  | 				.tap("JsonpTemplatePlugin", (chunk, set) => { | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 					set.add(RuntimeGlobals.publicPath); | 
					
						
							| 
									
										
										
										
											2018-11-28 20:07:40 +08:00
										 |  |  | 					set.add(RuntimeGlobals.getChunkUpdateScriptFilename); | 
					
						
							|  |  |  | 					set.add(RuntimeGlobals.moduleCache); | 
					
						
							|  |  |  | 					set.add(RuntimeGlobals.hmrModuleData); | 
					
						
							|  |  |  | 					set.add(RuntimeGlobals.moduleFactories); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			compilation.hooks.runtimeRequirementInTree | 
					
						
							|  |  |  | 				.for(RuntimeGlobals.hmrDownloadManifest) | 
					
						
							|  |  |  | 				.tap("JsonpTemplatePlugin", (chunk, set) => { | 
					
						
							|  |  |  | 					set.add(RuntimeGlobals.publicPath); | 
					
						
							|  |  |  | 					set.add(RuntimeGlobals.getUpdateManifestFilename); | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			compilation.hooks.additionalTreeRuntimeRequirements.tap( | 
					
						
							|  |  |  | 				"JsonpTemplatePlugin", | 
					
						
							|  |  |  | 				(chunk, set) => { | 
					
						
							| 
									
										
										
										
											2019-05-20 20:46:31 +08:00
										 |  |  | 					const withDefer = needEntryDeferringCode(chunk); | 
					
						
							|  |  |  | 					if (withDefer || needPrefetchingCode(chunk)) { | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 						set.add(RuntimeGlobals.startup); | 
					
						
							| 
									
										
										
										
											2019-05-20 20:46:31 +08:00
										 |  |  | 						if (withDefer) set.add(RuntimeGlobals.startupNoDefault); | 
					
						
							| 
									
										
										
										
											2018-12-29 19:48:59 +08:00
										 |  |  | 						handler(chunk, set); | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2017-01-13 11:53:12 +08:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | module.exports = JsonpTemplatePlugin; |