@@ -8,8 +8,8 @@ import { Alias, ResolverFunction, RollupAliasOptions } from '../types';
8
8
const VOLUME = / ^ ( [ A - Z ] : ) / i;
9
9
const IS_WINDOWS = platform ( ) === 'win32' ;
10
10
11
- // Helper functions
12
11
const noop = ( ) => null ;
12
+
13
13
function matches ( pattern : string | RegExp , importee : string ) {
14
14
if ( pattern instanceof RegExp ) {
15
15
return pattern . test ( importee ) ;
@@ -48,10 +48,28 @@ function getEntries({ entries }: RollupAliasOptions): Alias[] {
48
48
} ) ;
49
49
}
50
50
51
+ function getCustomResolver (
52
+ { customResolver } : Alias ,
53
+ options : RollupAliasOptions
54
+ ) : ResolverFunction | null {
55
+ if ( typeof customResolver === 'function' ) {
56
+ return customResolver ;
57
+ }
58
+ if ( customResolver && typeof customResolver . resolveId === 'function' ) {
59
+ return customResolver . resolveId ;
60
+ }
61
+ if ( typeof options . customResolver === 'function' ) {
62
+ return options . customResolver ;
63
+ }
64
+ if ( options . customResolver && typeof options . customResolver . resolveId === 'function' ) {
65
+ return options . customResolver . resolveId ;
66
+ }
67
+ return null ;
68
+ }
69
+
51
70
export default function alias ( options : RollupAliasOptions = { } ) : Plugin {
52
71
const entries = getEntries ( options ) ;
53
72
54
- // No aliases?
55
73
if ( entries . length === 0 ) {
56
74
return {
57
75
name : 'alias' ,
@@ -61,6 +79,19 @@ export default function alias(options: RollupAliasOptions = {}): Plugin {
61
79
62
80
return {
63
81
name : 'alias' ,
82
+ buildStart ( inputOptions ) {
83
+ return Promise . all (
84
+ [ ...entries , options ] . map (
85
+ ( { customResolver } ) =>
86
+ customResolver &&
87
+ typeof customResolver === 'object' &&
88
+ typeof customResolver . buildStart === 'function' &&
89
+ customResolver . buildStart . call ( this , inputOptions )
90
+ )
91
+ ) . then ( ( ) => {
92
+ // enforce void return value
93
+ } ) ;
94
+ } ,
64
95
resolveId ( importee , importer ) {
65
96
const importeeId = normalizeId ( importee ) ;
66
97
const importerId = normalizeId ( importer ) ;
@@ -75,25 +106,9 @@ export default function alias(options: RollupAliasOptions = {}): Plugin {
75
106
importeeId . replace ( matchedEntry . find , matchedEntry . replacement )
76
107
) ;
77
108
78
- let customResolver : ResolverFunction | null = null ;
79
- if ( typeof matchedEntry . customResolver === 'function' ) {
80
- ( { customResolver } = matchedEntry ) ;
81
- } else if (
82
- typeof matchedEntry . customResolver === 'object' &&
83
- typeof matchedEntry . customResolver ! . resolveId === 'function'
84
- ) {
85
- customResolver = matchedEntry . customResolver ! . resolveId ;
86
- } else if ( typeof options . customResolver === 'function' ) {
87
- ( { customResolver } = options ) ;
88
- } else if (
89
- typeof options . customResolver === 'object' &&
90
- typeof options . customResolver ! . resolveId === 'function'
91
- ) {
92
- customResolver = options . customResolver ! . resolveId ;
93
- }
94
-
109
+ const customResolver = getCustomResolver ( matchedEntry , options ) ;
95
110
if ( customResolver ) {
96
- return customResolver ( updatedId , importerId ) ;
111
+ return customResolver . call ( this , updatedId , importerId ) ;
97
112
}
98
113
99
114
return this . resolve ( updatedId , importer , { skipSelf : true } ) . then ( ( resolved ) => {
0 commit comments