| 
									
										
										
										
											2017-04-05 21:38:15 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | const path = require("path"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-21 05:37:42 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @typedef {Object} MakeRelativePathsCache | 
					
						
							|  |  |  |  * @property {Map<string, Map<string, string>>=} relativePaths | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-04-15 05:31:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @param {string} maybeAbsolutePath path to check | 
					
						
							| 
									
										
										
										
											2018-05-08 20:31:51 +08:00
										 |  |  |  * @returns {boolean} returns true if path is "Absolute Path"-like | 
					
						
							| 
									
										
										
										
											2018-04-15 05:31:11 +08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | const looksLikeAbsolutePath = maybeAbsolutePath => { | 
					
						
							| 
									
										
										
										
											2018-05-21 16:46:09 +08:00
										 |  |  | 	if (/^\/.*\/$/.test(maybeAbsolutePath)) { | 
					
						
							|  |  |  | 		// this 'path' is actually a regexp generated by dynamic requires.
 | 
					
						
							|  |  |  | 		// Don't treat it as an absolute path.
 | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-04-05 21:38:15 +08:00
										 |  |  | 	return /^(?:[a-z]:\\|\/)/i.test(maybeAbsolutePath); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-15 05:31:11 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @param {string} p path to normalize | 
					
						
							| 
									
										
										
										
											2018-05-08 20:31:51 +08:00
										 |  |  |  * @returns {string} normalized version of path | 
					
						
							| 
									
										
										
										
											2018-04-15 05:31:11 +08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | const normalizePathSeparator = p => p.replace(/\\/g, "/"); | 
					
						
							| 
									
										
										
										
											2017-04-07 21:37:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-15 05:31:11 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @param {string} context context for relative path | 
					
						
							|  |  |  |  * @param {string} identifier identifier for path | 
					
						
							| 
									
										
										
										
											2018-05-08 20:31:51 +08:00
										 |  |  |  * @returns {string} a converted relative path | 
					
						
							| 
									
										
										
										
											2018-04-15 05:31:11 +08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-07-18 04:30:29 +08:00
										 |  |  | const _makePathsRelative = (context, identifier) => { | 
					
						
							|  |  |  | 	return identifier | 
					
						
							|  |  |  | 		.split(/([|! ])/) | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		.map( | 
					
						
							|  |  |  | 			str => | 
					
						
							|  |  |  | 				looksLikeAbsolutePath(str) | 
					
						
							|  |  |  | 					? normalizePathSeparator(path.relative(context, str)) | 
					
						
							|  |  |  | 					: str | 
					
						
							|  |  |  | 		) | 
					
						
							| 
									
										
										
										
											2017-07-18 04:30:29 +08:00
										 |  |  | 		.join(""); | 
					
						
							| 
									
										
										
										
											2017-07-18 04:47:18 +08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-07-12 07:19:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-15 05:31:11 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @param {string} context context used to create relative path | 
					
						
							|  |  |  |  * @param {string} identifier identifier used to create relative path | 
					
						
							|  |  |  |  * @param {MakeRelativePathsCache=} cache the cache object being set | 
					
						
							| 
									
										
										
										
											2018-05-08 20:31:51 +08:00
										 |  |  |  * @returns {string} the returned relative path | 
					
						
							| 
									
										
										
										
											2018-04-15 05:31:11 +08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-07-18 04:30:29 +08:00
										 |  |  | exports.makePathsRelative = (context, identifier, cache) => { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 	if (!cache) return _makePathsRelative(context, identifier); | 
					
						
							| 
									
										
										
										
											2017-07-18 04:30:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 	const relativePaths = | 
					
						
							|  |  |  | 		cache.relativePaths || (cache.relativePaths = new Map()); | 
					
						
							| 
									
										
										
										
											2017-07-12 07:19:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-18 14:48:39 +08:00
										 |  |  | 	let cachedResult; | 
					
						
							| 
									
										
										
										
											2017-07-18 15:10:00 +08:00
										 |  |  | 	let contextCache = relativePaths.get(context); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 	if (typeof contextCache === "undefined") { | 
					
						
							|  |  |  | 		relativePaths.set(context, (contextCache = new Map())); | 
					
						
							| 
									
										
										
										
											2017-07-18 14:48:39 +08:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2017-07-18 15:10:00 +08:00
										 |  |  | 		cachedResult = contextCache.get(identifier); | 
					
						
							| 
									
										
										
										
											2017-07-18 14:48:39 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-07-12 07:19:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 	if (typeof cachedResult !== "undefined") { | 
					
						
							| 
									
										
										
										
											2017-07-18 14:48:39 +08:00
										 |  |  | 		return cachedResult; | 
					
						
							| 
									
										
										
										
											2017-07-12 07:19:37 +08:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2017-07-18 14:48:39 +08:00
										 |  |  | 		const relativePath = _makePathsRelative(context, identifier); | 
					
						
							| 
									
										
										
										
											2017-07-12 07:19:37 +08:00
										 |  |  | 		contextCache.set(identifier, relativePath); | 
					
						
							|  |  |  | 		return relativePath; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-04-05 21:38:15 +08:00
										 |  |  | }; |