1
1
import * as path from 'path' ;
2
2
import * as ts from 'typescript' ;
3
- import { Request , ResolverPlugin , Callback , Tapable } from './webpack' ;
3
+ import {
4
+ ResolverPlugin ,
5
+ Callback ,
6
+ Tapable ,
7
+ NormalModuleFactory ,
8
+ NormalModuleFactoryRequest ,
9
+ } from './webpack' ;
4
10
5
11
6
12
const ModulesInRootPlugin : new ( a : string , b : string , c : string ) => ResolverPlugin
7
13
= require ( 'enhanced-resolve/lib/ModulesInRootPlugin' ) ;
8
14
9
- interface CreateInnerCallback {
10
- ( callback : Callback < any > ,
11
- options : Callback < any > ,
12
- message ?: string ,
13
- messageOptional ? : string ) : Callback < any > ;
15
+ export interface Mapping {
16
+ onlyModule : boolean ;
17
+ alias : string ;
18
+ aliasPattern : RegExp ;
19
+ target : string ;
14
20
}
15
21
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
-
21
22
22
23
function escapeRegExp ( str : string ) : string {
23
- return str . replace ( / [ \- \[ \] \/ \{ \} \( \) \* \+ \? \ .\\ \^ \$ \ |] / g, '\\$&' ) ;
24
+ return str . replace ( / [ \- \[ \] \/ { } ( ) * + ? . \\ ^ $ | ] / g, '\\$&' ) ;
24
25
}
25
26
26
27
27
28
export interface PathsPluginOptions {
29
+ nmf : NormalModuleFactory ;
28
30
tsConfigPath : string ;
29
31
compilerOptions ?: ts . CompilerOptions ;
30
32
compilerHost ?: ts . CompilerHost ;
31
33
}
32
34
33
35
export class PathsPlugin implements Tapable {
36
+ private _nmf : NormalModuleFactory ;
34
37
private _tsConfigPath : string ;
35
38
private _compilerOptions : ts . CompilerOptions ;
36
39
private _host : ts . CompilerHost ;
37
40
38
41
source : string ;
39
42
target : string ;
40
43
41
- private mappings : any ;
44
+ private _mappings : Mapping [ ] ;
42
45
43
46
private _absoluteBaseUrl : string ;
44
47
@@ -76,6 +79,7 @@ export class PathsPlugin implements Tapable {
76
79
this . _host = ts . createCompilerHost ( this . _compilerOptions , false ) ;
77
80
}
78
81
82
+ this . _nmf = options . nmf ;
79
83
this . source = 'described-resolve' ;
80
84
this . target = 'resolve' ;
81
85
@@ -84,7 +88,7 @@ export class PathsPlugin implements Tapable {
84
88
this . _compilerOptions . baseUrl || '.'
85
89
) ;
86
90
87
- this . mappings = [ ] ;
91
+ this . _mappings = [ ] ;
88
92
let paths = this . _compilerOptions . paths || { } ;
89
93
Object . keys ( paths ) . forEach ( alias => {
90
94
let onlyModule = alias . indexOf ( '*' ) === - 1 ;
@@ -99,7 +103,7 @@ export class PathsPlugin implements Tapable {
99
103
aliasPattern = new RegExp ( `^${ withStarCapturing } ` ) ;
100
104
}
101
105
102
- this . mappings . push ( {
106
+ this . _mappings . push ( {
103
107
onlyModule,
104
108
alias,
105
109
aliasPattern,
@@ -116,59 +120,35 @@ export class PathsPlugin implements Tapable {
116
120
resolver . apply ( new ModulesInRootPlugin ( 'module' , this . _absoluteBaseUrl , 'resolve' ) ) ;
117
121
}
118
122
119
- this . mappings . forEach ( ( mapping : any ) => {
120
- resolver . plugin ( this . source , this . createPlugin ( resolver , mapping ) ) ;
121
- } ) ;
122
- }
123
+ this . _nmf . plugin ( 'before-resolve' , ( request : NormalModuleFactoryRequest ,
124
+ callback : Callback < any > ) => {
125
+ for ( let mapping of this . _mappings ) {
126
+ const match = request . request . match ( mapping . aliasPattern ) ;
127
+ if ( ! match ) { continue ; }
123
128
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
- }
134
-
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
- }
129
+ let newRequestStr = mapping . target ;
130
+ if ( ! mapping . onlyModule ) {
131
+ newRequestStr = newRequestStr . replace ( '*' , match [ 1 ] ) ;
132
+ }
164
133
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 ) ;
134
+ const moduleResolver : ts . ResolvedModuleWithFailedLookupLocations =
135
+ ts . nodeModuleNameResolver (
136
+ newRequestStr ,
137
+ this . _absoluteBaseUrl ,
138
+ this . _compilerOptions ,
139
+ this . _host
140
+ ) ;
141
+ const moduleFilePath = moduleResolver . resolvedModule ?
142
+ moduleResolver . resolvedModule . resolvedFileName : '' ;
143
+
144
+ if ( moduleFilePath ) {
145
+ return callback ( null , Object . assign ( { } , request , {
146
+ request : moduleFilePath . includes ( '.d.ts' ) ? newRequestStr : moduleFilePath
147
+ } ) ) ;
148
+ }
171
149
}
172
- } ;
150
+
151
+ return callback ( null , request ) ;
152
+ } ) ;
173
153
}
174
154
}
0 commit comments