| 10 | * @returns the converted path |
| 11 | */ |
| 12 | export const normalizePath = (path: string, relativize = true): string => { |
| 13 | if (typeof path !== 'string') { |
| 14 | throw new Error(`invalid path to normalize`); |
| 15 | } |
| 16 | path = normalizeSlashes(path.trim()); |
| 17 | |
| 18 | const components = pathComponents(path, getRootLength(path)); |
| 19 | const reducedComponents = reducePathComponents(components); |
| 20 | const rootPart = reducedComponents[0]; |
| 21 | const secondPart = reducedComponents[1]; |
| 22 | const normalized = rootPart + reducedComponents.slice(1).join('/'); |
| 23 | |
| 24 | if (normalized === '') { |
| 25 | return '.'; |
| 26 | } |
| 27 | if ( |
| 28 | rootPart === '' && |
| 29 | secondPart && |
| 30 | path.includes('/') && |
| 31 | !secondPart.startsWith('.') && |
| 32 | !secondPart.startsWith('@') && |
| 33 | relativize |
| 34 | ) { |
| 35 | return './' + normalized; |
| 36 | } |
| 37 | return normalized; |
| 38 | }; |
| 39 | |
| 40 | const normalizeSlashes = (path: string) => path.replace(backslashRegExp, '/'); |
| 41 | |