| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | const Template = require("./Template"); | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | class JsonpMainTemplatePlugin { | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | 	apply(mainTemplate) { | 
					
						
							|  |  |  | 		mainTemplate.plugin("local-vars", function(source, chunk) { | 
					
						
							|  |  |  | 			if(chunk.chunks.length > 0) { | 
					
						
							|  |  |  | 				return this.asString([ | 
					
						
							|  |  |  | 					source, | 
					
						
							|  |  |  | 					"", | 
					
						
							|  |  |  | 					"// objects to store loaded and loading chunks", | 
					
						
							|  |  |  | 					"var installedChunks = {", | 
					
						
							|  |  |  | 					this.indent( | 
					
						
							|  |  |  | 						chunk.ids.map(id => `${JSON.stringify(id)}: 0`).join(",\n") | 
					
						
							|  |  |  | 					), | 
					
						
							|  |  |  | 					"};" | 
					
						
							|  |  |  | 				]); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			return source; | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 		mainTemplate.plugin("jsonp-script", function(_, chunk, hash) { | 
					
						
							|  |  |  | 			const chunkFilename = this.outputOptions.chunkFilename; | 
					
						
							|  |  |  | 			const chunkMaps = chunk.getChunkMaps(); | 
					
						
							|  |  |  | 			const crossOriginLoading = this.outputOptions.crossOriginLoading; | 
					
						
							|  |  |  | 			const chunkLoadTimeout = this.outputOptions.chunkLoadTimeout || 120000; | 
					
						
							| 
									
										
										
										
											2017-03-07 16:33:43 +08:00
										 |  |  | 			const scriptSrcPath = this.applyPluginsWaterfall("asset-path", JSON.stringify(chunkFilename), { | 
					
						
							|  |  |  | 				hash: `" + ${this.renderCurrentHashCode(hash)} + "`, | 
					
						
							|  |  |  | 				hashWithLength: length => `" + ${this.renderCurrentHashCode(hash, length)} + "`, | 
					
						
							| 
									
										
										
										
											2015-07-17 15:30:37 +08:00
										 |  |  | 				chunk: { | 
					
						
							|  |  |  | 					id: "\" + chunkId + \"", | 
					
						
							| 
									
										
										
										
											2017-03-07 16:33:43 +08:00
										 |  |  | 					hash: `" + ${JSON.stringify(chunkMaps.hash)}[chunkId] + "`, | 
					
						
							|  |  |  | 					hashWithLength(length) { | 
					
						
							|  |  |  | 						const shortChunkHashMap = Object.create(null); | 
					
						
							|  |  |  | 						Object.keys(chunkMaps.hash).forEach(chunkId => { | 
					
						
							| 
									
										
										
										
											2015-07-17 15:30:37 +08:00
										 |  |  | 							if(typeof chunkMaps.hash[chunkId] === "string") | 
					
						
							|  |  |  | 								shortChunkHashMap[chunkId] = chunkMaps.hash[chunkId].substr(0, length); | 
					
						
							|  |  |  | 						}); | 
					
						
							| 
									
										
										
										
											2017-03-07 16:33:43 +08:00
										 |  |  | 						return `" + ${JSON.stringify(shortChunkHashMap)}[chunkId] + "`; | 
					
						
							| 
									
										
										
										
											2015-07-17 15:30:37 +08:00
										 |  |  | 					}, | 
					
						
							| 
									
										
										
										
											2017-03-07 16:33:43 +08:00
										 |  |  | 					name: `" + (${JSON.stringify(chunkMaps.name)}[chunkId]||chunkId) + "` | 
					
						
							| 
									
										
										
										
											2015-07-17 15:30:37 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2017-03-07 16:33:43 +08:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 			return this.asString([ | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | 				"var script = document.createElement('script');", | 
					
						
							|  |  |  | 				"script.type = 'text/javascript';", | 
					
						
							|  |  |  | 				"script.charset = 'utf-8';", | 
					
						
							|  |  |  | 				"script.async = true;", | 
					
						
							|  |  |  | 				`script.timeout = ${chunkLoadTimeout};`, | 
					
						
							| 
									
										
										
										
											2017-02-28 22:34:46 +08:00
										 |  |  | 				crossOriginLoading ? `script.crossOrigin = ${JSON.stringify(crossOriginLoading)};` : "", | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | 				`if (${this.requireFn}.nc) {`, | 
					
						
							|  |  |  | 				this.indent(`script.setAttribute("nonce", ${this.requireFn}.nc);`), | 
					
						
							|  |  |  | 				"}", | 
					
						
							| 
									
										
										
										
											2017-03-07 16:33:43 +08:00
										 |  |  | 				`script.src = ${this.requireFn}.p + ${scriptSrcPath};`, | 
					
						
							| 
									
										
										
										
											2017-02-23 17:41:42 +08:00
										 |  |  | 				`var timeout = setTimeout(onScriptComplete, ${chunkLoadTimeout});`, | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | 				"script.onerror = script.onload = onScriptComplete;", | 
					
						
							|  |  |  | 				"function onScriptComplete() {", | 
					
						
							| 
									
										
										
										
											2015-10-24 18:20:51 +08:00
										 |  |  | 				this.indent([ | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | 					"// avoid mem leaks in IE.", | 
					
						
							|  |  |  | 					"script.onerror = script.onload = null;", | 
					
						
							|  |  |  | 					"clearTimeout(timeout);", | 
					
						
							|  |  |  | 					"var chunk = installedChunks[chunkId];", | 
					
						
							|  |  |  | 					"if(chunk !== 0) {", | 
					
						
							|  |  |  | 					this.indent([ | 
					
						
							| 
									
										
										
										
											2017-04-09 15:45:56 +08:00
										 |  |  | 						"if(chunk) {", | 
					
						
							|  |  |  | 						this.indent("chunk[1](new Error('Loading chunk ' + chunkId + ' failed.'));"), | 
					
						
							|  |  |  | 						"}", | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | 						"installedChunks[chunkId] = undefined;" | 
					
						
							|  |  |  | 					]), | 
					
						
							|  |  |  | 					"}" | 
					
						
							| 
									
										
										
										
											2015-10-24 18:20:51 +08:00
										 |  |  | 				]), | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | 				"};", | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 			]); | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 		mainTemplate.plugin("require-ensure", function(_, chunk, hash) { | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 			return this.asString([ | 
					
						
							| 
									
										
										
										
											2017-04-09 15:45:56 +08:00
										 |  |  | 				"if(installedChunks[chunkId] === 0) {", | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | 				this.indent([ | 
					
						
							|  |  |  | 					"return Promise.resolve();" | 
					
						
							|  |  |  | 				]), | 
					
						
							| 
									
										
										
										
											2017-04-09 15:45:56 +08:00
										 |  |  | 				"}", | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 				"", | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | 				"// a Promise means \"currently loading\".", | 
					
						
							|  |  |  | 				"if(installedChunks[chunkId]) {", | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 				this.indent([ | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | 					"return installedChunks[chunkId][2];" | 
					
						
							| 
									
										
										
										
											2015-10-24 18:20:51 +08:00
										 |  |  | 				]), | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | 				"}", | 
					
						
							|  |  |  | 				"", | 
					
						
							|  |  |  | 				"// setup Promise in chunk cache", | 
					
						
							|  |  |  | 				"var promise = new Promise(function(resolve, reject) {", | 
					
						
							|  |  |  | 				this.indent([ | 
					
						
							|  |  |  | 					"installedChunks[chunkId] = [resolve, reject];" | 
					
						
							|  |  |  | 				]), | 
					
						
							|  |  |  | 				"});", | 
					
						
							|  |  |  | 				"installedChunks[chunkId][2] = promise;", | 
					
						
							|  |  |  | 				"", | 
					
						
							|  |  |  | 				"// start chunk loading", | 
					
						
							|  |  |  | 				"var head = document.getElementsByTagName('head')[0];", | 
					
						
							|  |  |  | 				this.applyPluginsWaterfall("jsonp-script", "", chunk, hash), | 
					
						
							|  |  |  | 				"head.appendChild(script);", | 
					
						
							|  |  |  | 				"", | 
					
						
							|  |  |  | 				"return promise;" | 
					
						
							|  |  |  | 			]); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 		mainTemplate.plugin("require-extensions", function(source, chunk) { | 
					
						
							|  |  |  | 			if(chunk.chunks.length === 0) return source; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 			return this.asString([ | 
					
						
							|  |  |  | 				source, | 
					
						
							|  |  |  | 				"", | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | 				"// on error function for async loading", | 
					
						
							| 
									
										
										
										
											2017-02-23 17:41:42 +08:00
										 |  |  | 				`${this.requireFn}.oe = function(err) { console.error(err); throw err; };` | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | 			]); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 		mainTemplate.plugin("bootstrap", function(source, chunk, hash) { | 
					
						
							|  |  |  | 			if(chunk.chunks.length > 0) { | 
					
						
							|  |  |  | 				var jsonpFunction = this.outputOptions.jsonpFunction; | 
					
						
							|  |  |  | 				return this.asString([ | 
					
						
							|  |  |  | 					source, | 
					
						
							|  |  |  | 					"", | 
					
						
							|  |  |  | 					"// install a JSONP callback for chunk loading", | 
					
						
							| 
									
										
										
										
											2017-02-23 17:41:42 +08:00
										 |  |  | 					`var parentJsonpFunction = window[${JSON.stringify(jsonpFunction)}];`, | 
					
						
							|  |  |  | 					`window[${JSON.stringify(jsonpFunction)}] = function webpackJsonpCallback(chunkIds, moreModules, executeModules) {`, | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 					this.indent([ | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | 						"// add \"moreModules\" to the modules object,", | 
					
						
							|  |  |  | 						"// then flag all \"chunkIds\" as loaded and fire callback", | 
					
						
							|  |  |  | 						"var moduleId, chunkId, i = 0, resolves = [], result;", | 
					
						
							|  |  |  | 						"for(;i < chunkIds.length; i++) {", | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 						this.indent([ | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | 							"chunkId = chunkIds[i];", | 
					
						
							| 
									
										
										
										
											2017-04-09 15:45:56 +08:00
										 |  |  | 							"if(installedChunks[chunkId]) {", | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | 							this.indent("resolves.push(installedChunks[chunkId][0]);"), | 
					
						
							| 
									
										
										
										
											2017-04-09 15:45:56 +08:00
										 |  |  | 							"}", | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | 							"installedChunks[chunkId] = 0;" | 
					
						
							|  |  |  | 						]), | 
					
						
							| 
									
										
										
										
											2017-03-31 21:16:55 +08:00
										 |  |  | 						"}", | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | 						"for(moduleId in moreModules) {", | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 						this.indent([ | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | 							"if(Object.prototype.hasOwnProperty.call(moreModules, moduleId)) {", | 
					
						
							|  |  |  | 							this.indent(this.renderAddModule(hash, chunk, "moduleId", "moreModules[moduleId]")), | 
					
						
							| 
									
										
										
										
											2016-06-04 17:45:07 +08:00
										 |  |  | 							"}" | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 						]), | 
					
						
							| 
									
										
										
										
											2016-06-06 02:35:42 +08:00
										 |  |  | 						"}", | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | 						"if(parentJsonpFunction) parentJsonpFunction(chunkIds, moreModules, executeModules);", | 
					
						
							| 
									
										
										
										
											2017-04-09 15:45:56 +08:00
										 |  |  | 						"while(resolves.length) {", | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | 						this.indent("resolves.shift()();"), | 
					
						
							| 
									
										
										
										
											2017-04-09 15:45:56 +08:00
										 |  |  | 						"}", | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | 						this.entryPointInChildren(chunk) ? [ | 
					
						
							|  |  |  | 							"if(executeModules) {", | 
					
						
							|  |  |  | 							this.indent([ | 
					
						
							|  |  |  | 								"for(i=0; i < executeModules.length; i++) {", | 
					
						
							| 
									
										
										
										
											2017-02-23 17:41:42 +08:00
										 |  |  | 								this.indent(`result = ${this.requireFn}(${this.requireFn}.s = executeModules[i]);`), | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | 								"}" | 
					
						
							|  |  |  | 							]), | 
					
						
							|  |  |  | 							"}", | 
					
						
							|  |  |  | 							"return result;", | 
					
						
							|  |  |  | 						] : "" | 
					
						
							|  |  |  | 					]), | 
					
						
							|  |  |  | 					"};" | 
					
						
							|  |  |  | 				]); | 
					
						
							| 
									
										
										
										
											2014-08-22 19:51:24 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | 			return source; | 
					
						
							| 
									
										
										
										
											2014-08-22 19:51:24 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | 		mainTemplate.plugin("hot-bootstrap", function(source, chunk, hash) { | 
					
						
							|  |  |  | 			const hotUpdateChunkFilename = this.outputOptions.hotUpdateChunkFilename; | 
					
						
							|  |  |  | 			const hotUpdateMainFilename = this.outputOptions.hotUpdateMainFilename; | 
					
						
							|  |  |  | 			const hotUpdateFunction = this.outputOptions.hotUpdateFunction; | 
					
						
							|  |  |  | 			const currentHotUpdateChunkFilename = this.applyPluginsWaterfall("asset-path", JSON.stringify(hotUpdateChunkFilename), { | 
					
						
							|  |  |  | 				hash: `" + ${this.renderCurrentHashCode(hash)} + "`, | 
					
						
							|  |  |  | 				hashWithLength: length => `" + ${this.renderCurrentHashCode(hash, length)} + "`, | 
					
						
							|  |  |  | 				chunk: { | 
					
						
							|  |  |  | 					id: "\" + chunkId + \"" | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 			const currentHotUpdateMainFilename = this.applyPluginsWaterfall("asset-path", JSON.stringify(hotUpdateMainFilename), { | 
					
						
							|  |  |  | 				hash: `" + ${this.renderCurrentHashCode(hash)} + "`, | 
					
						
							|  |  |  | 				hashWithLength: length => `" + ${this.renderCurrentHashCode(hash, length)} + "` | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-02-28 22:25:31 +08:00
										 |  |  | 			const runtimeSource = Template.getFunctionContent(require("./JsonpMainTemplate.runtime.js")) | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | 				.replace(/\/\/\$semicolon/g, ";") | 
					
						
							|  |  |  | 				.replace(/\$require\$/g, this.requireFn) | 
					
						
							|  |  |  | 				.replace(/\$hotMainFilename\$/g, currentHotUpdateMainFilename) | 
					
						
							|  |  |  | 				.replace(/\$hotChunkFilename\$/g, currentHotUpdateChunkFilename) | 
					
						
							|  |  |  | 				.replace(/\$hash\$/g, JSON.stringify(hash)); | 
					
						
							|  |  |  | 			return `${source}
 | 
					
						
							|  |  |  | function hotDisposeChunk(chunkId) { | 
					
						
							|  |  |  | 	delete installedChunks[chunkId]; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | var parentHotUpdateCallback = this[${JSON.stringify(hotUpdateFunction)}]; | 
					
						
							| 
									
										
										
										
											2017-02-28 22:25:31 +08:00
										 |  |  | this[${JSON.stringify(hotUpdateFunction)}] = ${runtimeSource}`;
 | 
					
						
							| 
									
										
										
										
											2014-08-22 19:51:24 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-02-23 00:54:16 +08:00
										 |  |  | 		mainTemplate.plugin("hash", function(hash) { | 
					
						
							|  |  |  | 			hash.update("jsonp"); | 
					
						
							|  |  |  | 			hash.update("4"); | 
					
						
							|  |  |  | 			hash.update(`${this.outputOptions.filename}`); | 
					
						
							|  |  |  | 			hash.update(`${this.outputOptions.chunkFilename}`); | 
					
						
							|  |  |  | 			hash.update(`${this.outputOptions.jsonpFunction}`); | 
					
						
							|  |  |  | 			hash.update(`${this.outputOptions.hotUpdateFunction}`); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | module.exports = JsonpMainTemplatePlugin; |