| 
									
										
										
										
											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-10 06:28:15 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const path = require("path"); | 
					
						
							| 
									
										
										
										
											2017-07-01 03:46:59 +08:00
										 |  |  | const NORMALIZE_SLASH_DIRECTION_REGEXP = /\\/g; | 
					
						
							|  |  |  | const PATH_CHARS_REGEXP = /[-[\]{}()*+?.,\\^$|#\s]/g; | 
					
						
							| 
									
										
										
										
											2017-07-06 22:54:50 +08:00
										 |  |  | const SEPARATOR_REGEXP = /[/\\]$/; | 
					
						
							| 
									
										
										
										
											2017-07-02 07:13:51 +08:00
										 |  |  | const FRONT_OR_BACK_BANG_REGEXP = /^!|!$/g; | 
					
						
							| 
									
										
										
										
											2017-07-02 07:18:29 +08:00
										 |  |  | const INDEX_JS_REGEXP = /\/index.js(!|\?|\(query\))/g; | 
					
						
							| 
									
										
										
										
											2018-06-01 20:54:54 +08:00
										 |  |  | const MATCH_RESOURCE_REGEXP = /!=!/; | 
					
						
							| 
									
										
										
										
											2017-07-01 03:46:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | const normalizeBackSlashDirection = request => { | 
					
						
							| 
									
										
										
										
											2017-07-01 03:46:59 +08:00
										 |  |  | 	return request.replace(NORMALIZE_SLASH_DIRECTION_REGEXP, "/"); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | const createRegExpForPath = path => { | 
					
						
							| 
									
										
										
										
											2017-07-02 07:13:51 +08:00
										 |  |  | 	const regexpTypePartial = path.replace(PATH_CHARS_REGEXP, "\\$&"); | 
					
						
							|  |  |  | 	return new RegExp(`(^|!)${regexpTypePartial}`, "g"); | 
					
						
							| 
									
										
										
										
											2017-07-01 04:07:02 +08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-01-10 06:28:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class RequestShortener { | 
					
						
							|  |  |  | 	constructor(directory) { | 
					
						
							| 
									
										
										
										
											2017-07-01 03:46:59 +08:00
										 |  |  | 		directory = normalizeBackSlashDirection(directory); | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 		if (SEPARATOR_REGEXP.test(directory)) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			directory = directory.substr(0, directory.length - 1); | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-01-10 06:28:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		if (directory) { | 
					
						
							| 
									
										
										
										
											2017-07-02 07:13:51 +08:00
										 |  |  | 			this.currentDirectoryRegExp = createRegExpForPath(directory); | 
					
						
							| 
									
										
										
										
											2017-01-10 06:28:15 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-05 08:02:48 +08:00
										 |  |  | 		const dirname = path.dirname(directory); | 
					
						
							| 
									
										
										
										
											2018-02-26 10:43:28 +08:00
										 |  |  | 		const endsWithSeparator = SEPARATOR_REGEXP.test(dirname); | 
					
						
							|  |  |  | 		const parentDirectory = endsWithSeparator | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			? dirname.substr(0, dirname.length - 1) | 
					
						
							|  |  |  | 			: dirname; | 
					
						
							|  |  |  | 		if (parentDirectory && parentDirectory !== directory) { | 
					
						
							| 
									
										
										
										
											2017-07-02 07:13:51 +08:00
										 |  |  | 			this.parentDirectoryRegExp = createRegExpForPath(parentDirectory); | 
					
						
							| 
									
										
										
										
											2017-01-10 06:28:15 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		if (__dirname.length >= 2) { | 
					
						
							| 
									
										
										
										
											2017-07-01 03:46:59 +08:00
										 |  |  | 			const buildins = normalizeBackSlashDirection(path.join(__dirname, "..")); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			const buildinsAsModule = | 
					
						
							|  |  |  | 				this.currentDirectoryRegExp && | 
					
						
							|  |  |  | 				this.currentDirectoryRegExp.test(buildins); | 
					
						
							| 
									
										
										
										
											2017-01-10 06:28:15 +08:00
										 |  |  | 			this.buildinsAsModule = buildinsAsModule; | 
					
						
							| 
									
										
										
										
											2017-07-02 07:13:51 +08:00
										 |  |  | 			this.buildinsRegExp = createRegExpForPath(buildins); | 
					
						
							| 
									
										
										
										
											2017-01-10 06:28:15 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-11-24 20:11:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		this.cache = new Map(); | 
					
						
							| 
									
										
										
										
											2013-07-11 05:20:07 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-10 06:28:15 +08:00
										 |  |  | 	shorten(request) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		if (!request) return request; | 
					
						
							| 
									
										
										
										
											2017-11-24 20:11:31 +08:00
										 |  |  | 		const cacheEntry = this.cache.get(request); | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 		if (cacheEntry !== undefined) { | 
					
						
							|  |  |  | 			return cacheEntry; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-11-24 20:11:31 +08:00
										 |  |  | 		let result = normalizeBackSlashDirection(request); | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 		if (this.buildinsAsModule && this.buildinsRegExp) { | 
					
						
							| 
									
										
										
										
											2017-11-24 20:11:31 +08:00
										 |  |  | 			result = result.replace(this.buildinsRegExp, "!(webpack)"); | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		if (this.currentDirectoryRegExp) { | 
					
						
							| 
									
										
										
										
											2017-11-24 20:11:31 +08:00
										 |  |  | 			result = result.replace(this.currentDirectoryRegExp, "!."); | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		if (this.parentDirectoryRegExp) { | 
					
						
							| 
									
										
										
										
											2017-11-24 20:11:31 +08:00
										 |  |  | 			result = result.replace(this.parentDirectoryRegExp, "!.."); | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		if (!this.buildinsAsModule && this.buildinsRegExp) { | 
					
						
							| 
									
										
										
										
											2017-11-24 20:11:31 +08:00
										 |  |  | 			result = result.replace(this.buildinsRegExp, "!(webpack)"); | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-11-24 20:11:31 +08:00
										 |  |  | 		result = result.replace(INDEX_JS_REGEXP, "$1"); | 
					
						
							|  |  |  | 		result = result.replace(FRONT_OR_BACK_BANG_REGEXP, ""); | 
					
						
							| 
									
										
										
										
											2018-06-01 20:54:54 +08:00
										 |  |  | 		result = result.replace(MATCH_RESOURCE_REGEXP, " = "); | 
					
						
							| 
									
										
										
										
											2017-11-24 20:11:31 +08:00
										 |  |  | 		this.cache.set(request, result); | 
					
						
							|  |  |  | 		return result; | 
					
						
							| 
									
										
										
										
											2013-07-11 05:20:07 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-10 06:28:15 +08:00
										 |  |  | module.exports = RequestShortener; |