1
1
import * as path from 'path' ;
2
2
import * as ts from 'typescript' ;
3
- import { Request , ResolverPlugin , Callback , Tapable } from './webpack' ;
4
-
5
-
6
- const ModulesInRootPlugin : new ( a : string , b : string , c : string ) => ResolverPlugin
7
- = require ( 'enhanced-resolve/lib/ModulesInRootPlugin' ) ;
8
-
9
- interface CreateInnerCallback {
10
- ( callback : Callback < any > ,
11
- options : Callback < any > ,
12
- message ?: string ,
13
- messageOptional ?: string ) : Callback < any > ;
14
- }
15
-
16
- const createInnerCallback : CreateInnerCallback
17
- = require ( 'enhanced-resolve/lib/createInnerCallback' ) ;
18
- const getInnerRequest : ( resolver : ResolverPlugin , request : Request ) => string
19
- = require ( 'enhanced-resolve/lib/getInnerRequest' ) ;
20
-
3
+ import { Callback , Tapable , NormalModuleFactory , NormalModuleFactoryRequest } from './webpack' ;
21
4
22
5
function escapeRegExp ( str : string ) : string {
23
6
return str . replace ( / [ \- \[ \] \/ \{ \} \( \) \* \+ \? \. \\ \^ \$ \| ] / g, '\\$&' ) ;
24
7
}
25
8
26
-
27
9
export interface PathsPluginOptions {
10
+ nmf : NormalModuleFactory ;
28
11
tsConfigPath : string ;
29
12
compilerOptions ?: ts . CompilerOptions ;
30
13
compilerHost ?: ts . CompilerHost ;
31
14
}
32
15
33
16
export class PathsPlugin implements Tapable {
17
+ private _nmf : NormalModuleFactory ;
34
18
private _tsConfigPath : string ;
35
19
private _compilerOptions : ts . CompilerOptions ;
36
20
private _host : ts . CompilerHost ;
37
-
38
- source : string ;
39
- target : string ;
40
-
41
- private mappings : any ;
42
-
43
21
private _absoluteBaseUrl : string ;
22
+ private _mappings : any [ ] = [ ] ;
44
23
45
24
private static _loadOptionsFromTsConfig ( tsConfigPath : string , host ?: ts . CompilerHost ) :
46
25
ts . CompilerOptions {
@@ -76,15 +55,13 @@ export class PathsPlugin implements Tapable {
76
55
this . _host = ts . createCompilerHost ( this . _compilerOptions , false ) ;
77
56
}
78
57
79
- this . source = 'described-resolve' ;
80
- this . target = 'resolve' ;
81
-
82
58
this . _absoluteBaseUrl = path . resolve (
83
59
path . dirname ( this . _tsConfigPath ) ,
84
60
this . _compilerOptions . baseUrl || '.'
85
61
) ;
86
62
87
- this . mappings = [ ] ;
63
+ this . _nmf = options . nmf ;
64
+
88
65
let paths = this . _compilerOptions . paths || { } ;
89
66
Object . keys ( paths ) . forEach ( alias => {
90
67
let onlyModule = alias . indexOf ( '*' ) === - 1 ;
@@ -99,7 +76,7 @@ export class PathsPlugin implements Tapable {
99
76
aliasPattern = new RegExp ( `^${ withStarCapturing } ` ) ;
100
77
}
101
78
102
- this . mappings . push ( {
79
+ this . _mappings . push ( {
103
80
onlyModule,
104
81
alias,
105
82
aliasPattern,
@@ -109,66 +86,36 @@ export class PathsPlugin implements Tapable {
109
86
} ) ;
110
87
}
111
88
112
- apply ( resolver : ResolverPlugin ) : void {
113
- let baseUrl = this . _compilerOptions . baseUrl || '.' ;
114
-
115
- if ( baseUrl ) {
116
- resolver . apply ( new ModulesInRootPlugin ( 'module' , this . _absoluteBaseUrl , 'resolve' ) ) ;
117
- }
118
-
119
- this . mappings . forEach ( ( mapping : any ) => {
120
- resolver . plugin ( this . source , this . createPlugin ( resolver , mapping ) ) ;
121
- } ) ;
122
- }
123
-
124
- resolve ( resolver : ResolverPlugin , mapping : any , request : any , callback : Callback < any > ) : any {
125
- let innerRequest = getInnerRequest ( resolver , request ) ;
126
- if ( ! innerRequest ) {
127
- return callback ( ) ;
128
- }
129
-
130
- let match = innerRequest . match ( mapping . aliasPattern ) ;
131
- if ( ! match ) {
132
- return callback ( ) ;
133
- }
89
+ apply ( ) : void {
90
+ this . _nmf . plugin ( 'before-resolve' , ( request : NormalModuleFactoryRequest ,
91
+ callback : Callback < any > ) => {
92
+ for ( let mapping of this . _mappings ) {
93
+ const match = request . request . match ( mapping . aliasPattern ) ;
94
+ if ( ! match ) { continue ; }
134
95
135
- let newRequestStr = mapping . target ;
136
- if ( ! mapping . onlyModule ) {
137
- newRequestStr = newRequestStr . replace ( '*' , match [ 1 ] ) ;
138
- }
139
- if ( newRequestStr [ 0 ] === '.' ) {
140
- newRequestStr = path . resolve ( this . _absoluteBaseUrl , newRequestStr ) ;
141
- }
142
-
143
- let newRequest = Object . assign ( { } , request , {
144
- request : newRequestStr
145
- } ) as Request ;
146
-
147
- return resolver . doResolve (
148
- this . target ,
149
- newRequest ,
150
- `aliased with mapping '${ innerRequest } ': '${ mapping . alias } ' to '${ newRequestStr } '` ,
151
- createInnerCallback (
152
- function ( err , result ) {
153
- if ( arguments . length > 0 ) {
154
- return callback ( err , result ) ;
155
- }
156
-
157
- // don't allow other aliasing or raw request
158
- callback ( null , null ) ;
159
- } ,
160
- callback
161
- )
162
- ) ;
163
- }
96
+ let newRequestStr = mapping . target ;
97
+ if ( ! mapping . onlyModule ) {
98
+ newRequestStr = newRequestStr . replace ( '*' , match [ 1 ] ) ;
99
+ }
164
100
165
- createPlugin ( resolver : ResolverPlugin , mapping : any ) : any {
166
- return ( request : any , callback : Callback < any > ) => {
167
- try {
168
- this . resolve ( resolver , mapping , request , callback ) ;
169
- } catch ( err ) {
170
- callback ( err ) ;
101
+ const moduleResolver : ts . ResolvedModuleWithFailedLookupLocations =
102
+ ts . nodeModuleNameResolver (
103
+ newRequestStr ,
104
+ this . _absoluteBaseUrl ,
105
+ this . _compilerOptions ,
106
+ this . _host
107
+ ) ;
108
+ const moduleFilePath = moduleResolver . resolvedModule ?
109
+ moduleResolver . resolvedModule . resolvedFileName : '' ;
110
+
111
+ if ( moduleFilePath ) {
112
+ return callback ( null , Object . assign ( { } , request , {
113
+ request : moduleFilePath . includes ( '.d.ts' ) ? newRequestStr : moduleFilePath
114
+ } ) ) ;
115
+ }
171
116
}
172
- } ;
117
+
118
+ return callback ( null , request ) ;
119
+ } ) ;
173
120
}
174
121
}
0 commit comments