| 
									
										
										
										
											2012-10-09 06:12:10 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2017-10-06 07:03:25 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2017-04-04 16:48:59 +08:00
										 |  |  | const fs = require("fs"); | 
					
						
							|  |  |  | const path = require("path"); | 
					
						
							| 
									
										
										
										
											2012-04-05 20:59:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-18 21:53:50 +08:00
										 |  |  | function lessStrict(regExpStr) { | 
					
						
							|  |  |  | 	regExpStr = regExpStr | 
					
						
							|  |  |  | 		.replace(/node_modules/g, "(node_modules|~)") | 
					
						
							| 
									
										
										
										
											2017-12-14 17:09:09 +08:00
										 |  |  | 		.replace(/(\\\/|\\\\)/g, "[\\/\\\\]"); | 
					
						
							| 
									
										
										
										
											2013-03-18 21:53:50 +08:00
										 |  |  | 	return regExpStr; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-04 16:48:59 +08:00
										 |  |  | const runtimeRegexp = /(```\s*(?:js|javascript)\n)?(.*)(\/\*\*\*\*\*\*\/ \(function\(modules\) \{ \/\/ webpackBootstrap\n(?:.|\n)*?\n\/\*\*\*\*\*\*\/ \}\)\n\/\**\/\n)/; | 
					
						
							|  |  |  | const timeRegexp = /\s*Time: \d+ms/g; | 
					
						
							| 
									
										
										
										
											2018-02-10 22:09:34 +08:00
										 |  |  | const buildAtRegexp = /\s*Built at: .+/mg; | 
					
						
							| 
									
										
										
										
											2017-12-14 17:09:09 +08:00
										 |  |  | const hashRegexp = /Hash: [a-f0-9]+/g; | 
					
						
							| 
									
										
										
										
											2016-09-07 17:46:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-14 17:09:09 +08:00
										 |  |  | exports.replaceBase = (template) => { | 
					
						
							| 
									
										
										
										
											2012-04-05 20:59:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-04 16:48:59 +08:00
										 |  |  | 	let cwd = process.cwd(); | 
					
						
							|  |  |  | 	let webpack = path.join(__dirname, ".."); | 
					
						
							|  |  |  | 	let webpackParent = path.join(__dirname, "..", ".."); | 
					
						
							| 
									
										
										
										
											2013-03-18 21:53:50 +08:00
										 |  |  | 	cwd = lessStrict(cwd.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&")); | 
					
						
							| 
									
										
										
										
											2012-05-07 03:07:24 +08:00
										 |  |  | 	cwd = new RegExp(cwd, "g"); | 
					
						
							| 
									
										
										
										
											2013-03-18 21:53:50 +08:00
										 |  |  | 	webpack = lessStrict(webpack.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&")); | 
					
						
							|  |  |  | 	webpack = new RegExp(webpack, "g"); | 
					
						
							|  |  |  | 	webpackParent = lessStrict(webpackParent.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&")); | 
					
						
							|  |  |  | 	webpackParent = new RegExp(webpackParent, "g"); | 
					
						
							| 
									
										
										
										
											2016-12-14 19:03:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-04 16:48:59 +08:00
										 |  |  | 	return template | 
					
						
							| 
									
										
										
										
											2016-09-07 17:46:30 +08:00
										 |  |  | 		.replace(/\r\n/g, "\n") | 
					
						
							| 
									
										
										
										
											2017-04-04 16:48:59 +08:00
										 |  |  | 		.replace(/\r/g, "\n") | 
					
						
							| 
									
										
										
										
											2016-09-07 17:46:30 +08:00
										 |  |  | 		.replace(cwd, ".") | 
					
						
							|  |  |  | 		.replace(webpack, "(webpack)") | 
					
						
							|  |  |  | 		.replace(webpackParent, "(webpack)/~") | 
					
						
							| 
									
										
										
										
											2016-12-14 19:03:24 +08:00
										 |  |  | 		.replace(timeRegexp, "") | 
					
						
							| 
									
										
										
										
											2018-02-10 22:09:34 +08:00
										 |  |  | 		.replace(buildAtRegexp, "") | 
					
						
							| 
									
										
										
										
											2017-12-14 17:09:09 +08:00
										 |  |  | 		.replace(hashRegexp, "Hash: 0a1b2c3d4e5f6a7b8c9d") | 
					
						
							| 
									
										
										
										
											2017-04-04 16:48:59 +08:00
										 |  |  | 		.replace(/\.chunkhash\./g, ".[chunkhash].") | 
					
						
							| 
									
										
										
										
											2016-09-07 17:46:30 +08:00
										 |  |  | 		.replace(runtimeRegexp, function(match) { | 
					
						
							|  |  |  | 			match = runtimeRegexp.exec(match); | 
					
						
							| 
									
										
										
										
											2017-04-04 16:48:59 +08:00
										 |  |  | 			const prefix = match[1] ? "" : "```\n"; | 
					
						
							|  |  |  | 			const inner = match[1] ? match[1] : "``` js\n"; | 
					
						
							| 
									
										
										
										
											2017-03-31 02:42:42 +08:00
										 |  |  | 			return prefix + "<details><summary><code>" + match[2] + "/******/ (function(modules) { /* webpackBootstrap */ })</code></summary>\n\n" + inner + match[2] + match[3] + "```\n\n</details>\n\n" + inner; | 
					
						
							| 
									
										
										
										
											2016-09-07 17:46:30 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-12-14 17:09:09 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | exports.needResults = (template, prefix) => { | 
					
						
							|  |  |  | 	const regExp = prefix ? new RegExp(`\\{\\{${prefix}:`) : /\{\{/; | 
					
						
							|  |  |  | 	return regExp.test(template); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-04-04 16:48:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-14 17:09:09 +08:00
										 |  |  | exports.replaceResults = (template, baseDir, stdout, prefix) => { | 
					
						
							|  |  |  | 	const regexp = new RegExp("\\{\\{" + (prefix ? prefix + ":" : "") + "([^:\\}]+)\\}\\}", "g"); | 
					
						
							| 
									
										
										
										
											2017-04-04 16:48:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return template.replace(regexp, function(match) { | 
					
						
							| 
									
										
										
										
											2017-12-14 17:09:09 +08:00
										 |  |  | 		match = match.substr(2 + (prefix ? prefix.length + 1 : 0), match.length - 4 - (prefix ? prefix.length + 1 : 0)); | 
					
						
							| 
									
										
										
										
											2017-04-04 16:48:59 +08:00
										 |  |  | 		if(match === "stdout") | 
					
						
							|  |  |  | 			return stdout; | 
					
						
							|  |  |  | 		return fs.readFileSync(path.join(baseDir, match), "utf-8").replace(/[\r\n]*$/, ""); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2017-12-14 17:09:09 +08:00
										 |  |  | }; |