| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2017-02-21 07:05:34 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 17:37:25 +08:00
										 |  |  | const Template = require("../Template"); | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 07:05:34 +08:00
										 |  |  | class WebWorkerMainTemplatePlugin { | 
					
						
							|  |  |  | 	apply(mainTemplate) { | 
					
						
							| 
									
										
										
										
											2018-01-20 00:06:59 +08:00
										 |  |  | 		const needChunkOnDemandLoadingCode = chunk => { | 
					
						
							|  |  |  | 			for(const chunkGroup of chunk.groupsIterable) { | 
					
						
							|  |  |  | 				if(chunkGroup.getNumberOfChildren() > 0) return true; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			return false; | 
					
						
							|  |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2017-12-14 04:35:39 +08:00
										 |  |  | 		mainTemplate.hooks.localVars.tap("WebWorkerMainTemplatePlugin", (source, chunk) => { | 
					
						
							| 
									
										
										
										
											2018-01-20 00:06:59 +08:00
										 |  |  | 			if(needChunkOnDemandLoadingCode(chunk)) { | 
					
						
							| 
									
										
										
										
											2017-12-07 16:42:33 +08:00
										 |  |  | 				return Template.asString([ | 
					
						
							| 
									
										
										
										
											2017-02-21 07:05:34 +08:00
										 |  |  | 					source, | 
					
						
							|  |  |  | 					"", | 
					
						
							|  |  |  | 					"// object to store loaded chunks", | 
					
						
							|  |  |  | 					"// \"1\" means \"already loaded\"", | 
					
						
							|  |  |  | 					"var installedChunks = {", | 
					
						
							| 
									
										
										
										
											2017-12-07 16:42:33 +08:00
										 |  |  | 					Template.indent( | 
					
						
							| 
									
										
										
										
											2017-06-20 13:40:27 +08:00
										 |  |  | 						chunk.ids.map((id) => `${id}: 1`).join(",\n") | 
					
						
							| 
									
										
										
										
											2017-02-21 07:05:34 +08:00
										 |  |  | 					), | 
					
						
							| 
									
										
										
										
											2017-05-24 15:44:35 +08:00
										 |  |  | 					"};" | 
					
						
							| 
									
										
										
										
											2017-02-21 07:05:34 +08:00
										 |  |  | 				]); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			return source; | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-12-14 04:35:39 +08:00
										 |  |  | 		mainTemplate.hooks.requireEnsure.tap("WebWorkerMainTemplatePlugin", (_, chunk, hash) => { | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | 			const chunkFilename = mainTemplate.outputOptions.chunkFilename; | 
					
						
							| 
									
										
										
										
											2017-12-07 16:42:33 +08:00
										 |  |  | 			return Template.asString([ | 
					
						
							| 
									
										
										
										
											2017-11-21 19:56:55 +08:00
										 |  |  | 				"promises.push(Promise.resolve().then(function() {", | 
					
						
							| 
									
										
										
										
											2017-12-07 16:42:33 +08:00
										 |  |  | 				Template.indent([ | 
					
						
							| 
									
										
										
										
											2017-05-24 15:44:35 +08:00
										 |  |  | 					"// \"1\" is the signal for \"already loaded\"", | 
					
						
							|  |  |  | 					"if(!installedChunks[chunkId]) {", | 
					
						
							| 
									
										
										
										
											2017-12-07 16:42:33 +08:00
										 |  |  | 					Template.indent([ | 
					
						
							| 
									
										
										
										
											2017-05-24 15:44:35 +08:00
										 |  |  | 						"importScripts(" + | 
					
						
							| 
									
										
										
										
											2017-11-29 01:43:01 +08:00
										 |  |  | 						mainTemplate.getAssetPath(JSON.stringify(chunkFilename), { | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | 							hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`, | 
					
						
							|  |  |  | 							hashWithLength: (length) => `" + ${mainTemplate.renderCurrentHashCode(hash, length)} + "`, | 
					
						
							| 
									
										
										
										
											2017-05-24 15:44:35 +08:00
										 |  |  | 							chunk: { | 
					
						
							|  |  |  | 								id: "\" + chunkId + \"" | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						}) + ");" | 
					
						
							|  |  |  | 					]), | 
					
						
							|  |  |  | 					"}", | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 				]), | 
					
						
							| 
									
										
										
										
											2017-11-21 19:56:55 +08:00
										 |  |  | 				"}));" | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 			]); | 
					
						
							| 
									
										
										
										
											2015-10-18 04:40:31 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-12-14 04:35:39 +08:00
										 |  |  | 		mainTemplate.hooks.bootstrap.tap("WebWorkerMainTemplatePlugin", (source, chunk, hash) => { | 
					
						
							| 
									
										
										
										
											2018-01-20 00:06:59 +08:00
										 |  |  | 			if(needChunkOnDemandLoadingCode(chunk)) { | 
					
						
							| 
									
										
										
										
											2017-12-28 01:46:37 +08:00
										 |  |  | 				const chunkCallbackName = mainTemplate.outputOptions.chunkCallbackName; | 
					
						
							|  |  |  | 				const globalObject = mainTemplate.outputOptions.globalObject; | 
					
						
							| 
									
										
										
										
											2017-12-07 16:42:33 +08:00
										 |  |  | 				return Template.asString([ | 
					
						
							| 
									
										
										
										
											2017-02-21 07:05:34 +08:00
										 |  |  | 					source, | 
					
						
							| 
									
										
										
										
											2017-12-28 01:46:37 +08:00
										 |  |  | 					`${globalObject}[${JSON.stringify(chunkCallbackName)}] = function webpackChunkCallback(chunkIds, moreModules) {`, | 
					
						
							| 
									
										
										
										
											2017-12-07 16:42:33 +08:00
										 |  |  | 					Template.indent([ | 
					
						
							| 
									
										
										
										
											2017-02-21 07:05:34 +08:00
										 |  |  | 						"for(var moduleId in moreModules) {", | 
					
						
							| 
									
										
										
										
											2017-12-07 16:42:33 +08:00
										 |  |  | 						Template.indent(mainTemplate.renderAddModule(hash, chunk, "moduleId", "moreModules[moduleId]")), | 
					
						
							| 
									
										
										
										
											2017-02-21 07:05:34 +08:00
										 |  |  | 						"}", | 
					
						
							|  |  |  | 						"while(chunkIds.length)", | 
					
						
							| 
									
										
										
										
											2017-12-07 16:42:33 +08:00
										 |  |  | 						Template.indent("installedChunks[chunkIds.pop()] = 1;") | 
					
						
							| 
									
										
										
										
											2017-02-21 07:05:34 +08:00
										 |  |  | 					]), | 
					
						
							|  |  |  | 					"};" | 
					
						
							|  |  |  | 				]); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			return source; | 
					
						
							| 
									
										
										
										
											2015-10-18 04:40:31 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-12-14 04:35:39 +08:00
										 |  |  | 		mainTemplate.hooks.hotBootstrap.tap("WebWorkerMainTemplatePlugin", (source, chunk, hash) => { | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | 			const hotUpdateChunkFilename = mainTemplate.outputOptions.hotUpdateChunkFilename; | 
					
						
							|  |  |  | 			const hotUpdateMainFilename = mainTemplate.outputOptions.hotUpdateMainFilename; | 
					
						
							| 
									
										
										
										
											2017-12-28 01:46:37 +08:00
										 |  |  | 			const hotUpdateFunction = mainTemplate.outputOptions.hotUpdateFunction; | 
					
						
							|  |  |  | 			const globalObject = mainTemplate.outputOptions.globalObject; | 
					
						
							| 
									
										
										
										
											2017-11-29 01:43:01 +08:00
										 |  |  | 			const currentHotUpdateChunkFilename = mainTemplate.getAssetPath(JSON.stringify(hotUpdateChunkFilename), { | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | 				hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`, | 
					
						
							|  |  |  | 				hashWithLength: (length) => `" + ${mainTemplate.renderCurrentHashCode(hash, length)} + "`, | 
					
						
							| 
									
										
										
										
											2017-02-21 07:05:34 +08:00
										 |  |  | 				chunk: { | 
					
						
							|  |  |  | 					id: "\" + chunkId + \"" | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-11-29 01:43:01 +08:00
										 |  |  | 			const currentHotUpdateMainFilename = mainTemplate.getAssetPath(JSON.stringify(hotUpdateMainFilename), { | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | 				hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`, | 
					
						
							|  |  |  | 				hashWithLength: (length) => `" + ${mainTemplate.renderCurrentHashCode(hash, length)} + "`, | 
					
						
							| 
									
										
										
										
											2017-02-21 07:05:34 +08:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2015-10-18 04:40:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 07:05:34 +08:00
										 |  |  | 			return source + "\n" + | 
					
						
							| 
									
										
										
										
											2017-12-28 01:46:37 +08:00
										 |  |  | 				`var parentHotUpdateCallback = ${globalObject}[${JSON.stringify(hotUpdateFunction)}];\n` + | 
					
						
							|  |  |  | 				`${globalObject}[${JSON.stringify(hotUpdateFunction)}] = ` + | 
					
						
							| 
									
										
										
										
											2017-06-20 13:40:27 +08:00
										 |  |  | 				Template.getFunctionContent(require("./WebWorkerMainTemplate.runtime.js")) | 
					
						
							| 
									
										
										
										
											2017-02-21 07:05:34 +08:00
										 |  |  | 				.replace(/\/\/\$semicolon/g, ";") | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | 				.replace(/\$require\$/g, mainTemplate.requireFn) | 
					
						
							| 
									
										
										
										
											2017-02-21 07:05:34 +08:00
										 |  |  | 				.replace(/\$hotMainFilename\$/g, currentHotUpdateMainFilename) | 
					
						
							|  |  |  | 				.replace(/\$hotChunkFilename\$/g, currentHotUpdateChunkFilename) | 
					
						
							|  |  |  | 				.replace(/\$hash\$/g, JSON.stringify(hash)); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-12-14 04:35:39 +08:00
										 |  |  | 		mainTemplate.hooks.hash.tap("WebWorkerMainTemplatePlugin", hash => { | 
					
						
							| 
									
										
										
										
											2017-02-21 07:05:34 +08:00
										 |  |  | 			hash.update("webworker"); | 
					
						
							|  |  |  | 			hash.update("3"); | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | 			hash.update(`${mainTemplate.outputOptions.publicPath}`); | 
					
						
							|  |  |  | 			hash.update(`${mainTemplate.outputOptions.filename}`); | 
					
						
							|  |  |  | 			hash.update(`${mainTemplate.outputOptions.chunkFilename}`); | 
					
						
							|  |  |  | 			hash.update(`${mainTemplate.outputOptions.chunkCallbackName}`); | 
					
						
							| 
									
										
										
										
											2017-12-28 01:46:37 +08:00
										 |  |  | 			hash.update(`${mainTemplate.outputOptions.globalObject}`); | 
					
						
							| 
									
										
										
										
											2017-02-21 07:05:34 +08:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | module.exports = WebWorkerMainTemplatePlugin; |