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 ) => {
@@ -24,6 +30,14 @@ const exists = uri => {
24
30
}
25
31
} ;
26
32
33
+ const normalizeId = id => {
34
+ if ( IS_WINDOWS && typeof id === 'string' ) {
35
+ return slash ( id . replace ( VOLUME , '' ) ) ;
36
+ }
37
+
38
+ return id ;
39
+ } ;
40
+
27
41
export default function alias ( options = { } ) {
28
42
const hasResolve = Array . isArray ( options . resolve ) ;
29
43
const resolve = hasResolve ? options . resolve : [ '.js' ] ;
@@ -39,19 +53,22 @@ export default function alias(options = {}) {
39
53
40
54
return {
41
55
resolveId ( importee , importer ) {
56
+ const importeeId = normalizeId ( importee ) ;
57
+ const importerId = normalizeId ( importer ) ;
58
+
42
59
// First match is supposed to be the correct one
43
- const toReplace = aliasKeys . find ( key => matches ( key , importee ) ) ;
60
+ const toReplace = aliasKeys . find ( key => matches ( key , importeeId ) ) ;
44
61
45
62
if ( ! toReplace ) {
46
63
return null ;
47
64
}
48
65
49
66
const entry = options [ toReplace ] ;
50
67
51
- const updatedId = importee . replace ( toReplace , entry ) ;
68
+ const updatedId = importeeId . replace ( toReplace , entry ) ;
52
69
53
70
if ( isFilePath ( updatedId ) ) {
54
- const directory = path . dirname ( importer ) ;
71
+ const directory = path . dirname ( importerId ) ;
55
72
56
73
// Resolve file names
57
74
const filePath = path . resolve ( directory , updatedId ) ;
0 commit comments