@@ -3,31 +3,39 @@ import type { VLQMap, SourceMapStringifyOptions } from './types';
3
3
4
4
import path from 'path' ;
5
5
6
+ // For some reason path.isAbsolute barely works... Regex to the rescue?
7
+ // Apparently windows stuff is under `path.win32`, so yeah windows makes stuff complicated :)
8
+ const ABSOLUTE_PATH_REGEX = / ^ ( [ a - z A - Z ] : ) { 0 , 1 } [ \\ / ] + / ;
9
+ const PATH_SEPARATOR_REGEX = / \\ / g;
10
+
6
11
export function generateInlineMap ( map : string ) : string {
7
12
return `data:application/json;charset=utf-8;base64,${ Buffer . from ( map ) . toString ( 'base64' ) } ` ;
8
13
}
9
14
10
- export function normalizePath ( filepath : string ) : string {
11
- filepath = filepath . replace ( / \\ / g, '/' ) ;
12
-
13
- // Prefix relative paths with ./ as it makes it more clear and probably prevents issues
14
- if ( filepath . length > 0 && filepath [ 0 ] !== '.' && ! path . isAbsolute ( filepath ) ) {
15
- filepath = `./${ filepath } ` ;
16
- }
15
+ export function isAbsolute ( filepath : string ) : boolean {
16
+ return ABSOLUTE_PATH_REGEX . test ( filepath ) ;
17
+ }
17
18
18
- return filepath ;
19
+ export function normalizePath ( filepath : string ) : string {
20
+ return filepath . replace ( ABSOLUTE_PATH_REGEX , '/' ) . replace ( PATH_SEPARATOR_REGEX , '/' ) ;
19
21
}
20
22
21
23
export function relatifyPath ( filepath : string , rootDir : string ) : string {
22
- // Sourcemaps are made for web, so replace backslashes with regular slashes
24
+ rootDir = normalizePath ( rootDir ) ;
23
25
filepath = normalizePath ( filepath ) ;
24
26
25
27
// Make root paths relative to the rootDir
26
28
if ( filepath [ 0 ] === '/' ) {
27
- filepath = normalizePath ( path . relative ( rootDir , filepath ) ) ;
29
+ filepath = path . relative ( rootDir , filepath ) ;
28
30
}
29
31
30
- return filepath ;
32
+ // Prefix relative paths with ./ as it makes it more clear and probably prevents issues
33
+ if ( filepath [ 0 ] !== '.' ) {
34
+ filepath = `./${ filepath } ` ;
35
+ }
36
+
37
+ // Sourcemaps are made for web, so replace backslashes with regular slashes
38
+ return normalizePath ( filepath ) ;
31
39
}
32
40
33
41
export async function partialVlqMapToSourceMap (
0 commit comments