| 
									
										
										
										
											2021-07-01 14:19:50 +02:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-05 11:20:21 +02:00
										 |  |  | // From https://github.com/tleunen/babel-plugin-module-resolver/blob/master/src/utils.js
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-01 14:19:50 +02:00
										 |  |  | const path = require('path'); | 
					
						
							|  |  |  | const resolve = require('resolve'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const nodeResolvePath = (modulePath, basedir, extensions) => { | 
					
						
							|  |  |  |   try { | 
					
						
							|  |  |  |     return resolve.sync(modulePath, { basedir, extensions }); | 
					
						
							|  |  |  |   } catch (e) { | 
					
						
							|  |  |  |     return null; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  | const toPosixPath = (modulePath) => { | 
					
						
							| 
									
										
										
										
											2021-07-01 14:19:50 +02:00
										 |  |  |   return modulePath.replace(/\\/g, '/'); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  | const isRelativePath = (nodePath) => { | 
					
						
							|  |  |  |   return nodePath.match(/^\.?\.\//); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-01 14:19:50 +02:00
										 |  |  | const stripExtension = (modulePath, stripExtensions) => { | 
					
						
							|  |  |  |   let name = path.basename(modulePath); | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  |   stripExtensions.some((extension) => { | 
					
						
							| 
									
										
										
										
											2021-07-01 14:19:50 +02:00
										 |  |  |     if (name.endsWith(extension)) { | 
					
						
							|  |  |  |       name = name.slice(0, name.length - extension.length); | 
					
						
							|  |  |  |       return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   return name; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const replaceExtension = (modulePath, opts) => { | 
					
						
							|  |  |  |   const filename = stripExtension(modulePath, opts.extensions); | 
					
						
							|  |  |  |   return path.join(path.dirname(modulePath), filename); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  | const toLocalPath = (modulePath) => { | 
					
						
							| 
									
										
										
										
											2021-07-01 14:19:50 +02:00
										 |  |  |   let localPath = modulePath.replace(/\/index$/, ''); // remove trailing /index
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!isRelativePath(localPath)) { | 
					
						
							|  |  |  |     localPath = `./${localPath}`; // insert `./` to make it a relative path
 | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return localPath; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							|  |  |  |   nodeResolvePath, | 
					
						
							|  |  |  |   replaceExtension, | 
					
						
							|  |  |  |   stripExtension, | 
					
						
							|  |  |  |   toPosixPath, | 
					
						
							|  |  |  |   toLocalPath, | 
					
						
							|  |  |  |   isRelativePath, | 
					
						
							|  |  |  | }; |