1
- import path from 'path' ;
1
+ import { posix as path } from 'path' ;
2
+ import { platform } from 'os' ;
2
3
import fs from 'fs' ;
3
4
5
+ import slash from 'slash' ;
6
+
7
+ const VOLUME = / ^ ( [ A - Z ] : ) / ;
8
+ const IS_WINDOWS = platform ( ) === 'win32' ;
9
+
4
10
// Helper functions
5
11
const noop = ( ) => null ;
6
12
const matches = ( key , importee ) => {
@@ -15,7 +21,7 @@ const matches = (key, importee) => {
15
21
return importeeStartsWithKey && importeeHasSlashAfterKey ;
16
22
} ;
17
23
const endsWith = ( needle , haystack ) => haystack . slice ( - needle . length ) === needle ;
18
- const isFilePath = id => new RegExp ( `^\\ .?\\ ${ path . sep } ` ) . test ( id ) ;
24
+ const isFilePath = id => / ^ \ .? \/ / . test ( id ) ;
19
25
const exists = uri => {
20
26
try {
21
27
return fs . statSync ( uri ) . isFile ( ) ;
@@ -24,6 +30,24 @@ const exists = uri => {
24
30
}
25
31
} ;
26
32
33
+ const getVolume = id => {
34
+ let volume = '' ;
35
+
36
+ if ( IS_WINDOWS && typeof id === 'string' ) {
37
+ [ volume ] = id . match ( VOLUME ) || [ '' ] ;
38
+ }
39
+
40
+ return volume ;
41
+ } ;
42
+
43
+ const normalizeId = id => {
44
+ if ( IS_WINDOWS && typeof id === 'string' ) {
45
+ return slash ( id . replace ( VOLUME , '' ) ) ;
46
+ }
47
+
48
+ return id ;
49
+ } ;
50
+
27
51
export default function alias ( options = { } ) {
28
52
const hasResolve = Array . isArray ( options . resolve ) ;
29
53
const resolve = hasResolve ? options . resolve : [ '.js' ] ;
@@ -39,39 +63,43 @@ export default function alias(options = {}) {
39
63
40
64
return {
41
65
resolveId ( importee , importer ) {
66
+ const volume = getVolume ( importee ) || getVolume ( importer ) ;
67
+ const importeeId = normalizeId ( importee ) ;
68
+ const importerId = normalizeId ( importer ) ;
69
+
42
70
// First match is supposed to be the correct one
43
- const toReplace = aliasKeys . find ( key => matches ( key , importee ) ) ;
71
+ const toReplace = aliasKeys . find ( key => matches ( key , importeeId ) ) ;
44
72
45
73
if ( ! toReplace ) {
46
74
return null ;
47
75
}
48
76
49
77
const entry = options [ toReplace ] ;
50
78
51
- const updatedId = importee . replace ( toReplace , entry ) ;
79
+ const updatedId = importeeId . replace ( toReplace , entry ) ;
52
80
53
81
if ( isFilePath ( updatedId ) ) {
54
- const directory = path . dirname ( importer ) ;
82
+ const directory = path . dirname ( importerId ) ;
55
83
56
84
// Resolve file names
57
85
const filePath = path . resolve ( directory , updatedId ) ;
58
86
const match = resolve . map ( ext => `${ filePath } ${ ext } ` )
59
87
. find ( exists ) ;
60
88
61
89
if ( match ) {
62
- return match ;
90
+ return path . join ( volume , match ) ;
63
91
}
64
92
65
93
// To keep the previous behaviour we simply return the file path
66
94
// with extension
67
95
if ( endsWith ( '.js' , filePath ) ) {
68
- return filePath ;
96
+ return path . join ( volume , filePath ) ;
69
97
}
70
98
71
- return filePath + '.js' ;
99
+ return path . join ( volume , filePath + '.js' ) ;
72
100
}
73
101
74
- return updatedId ;
102
+ return path . join ( volume , updatedId ) ;
75
103
} ,
76
104
} ;
77
105
}
0 commit comments