11import * as path from 'path' ;
22import * 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' ;
410
511
612const ModulesInRootPlugin : new ( a : string , b : string , c : string ) => ResolverPlugin
713 = require ( 'enhanced-resolve/lib/ModulesInRootPlugin' ) ;
814
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 ;
1420}
1521
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-
2122
2223function escapeRegExp ( str : string ) : string {
2324 return str . replace ( / [ \- \[ \] \/ \{ \} \( \) \* \+ \? \. \\ \^ \$ \| ] / g, '\\$&' ) ;
2425}
2526
2627
2728export interface PathsPluginOptions {
29+ nmf : NormalModuleFactory ;
2830 tsConfigPath : string ;
2931 compilerOptions ?: ts . CompilerOptions ;
3032 compilerHost ?: ts . CompilerHost ;
3133}
3234
3335export class PathsPlugin implements Tapable {
36+ private _nmf : NormalModuleFactory ;
3437 private _tsConfigPath : string ;
3538 private _compilerOptions : ts . CompilerOptions ;
3639 private _host : ts . CompilerHost ;
3740
3841 source : string ;
3942 target : string ;
4043
41- private mappings : any ;
44+ private _mappings : Mapping [ ] ;
4245
4346 private _absoluteBaseUrl : string ;
4447
@@ -76,6 +79,7 @@ export class PathsPlugin implements Tapable {
7679 this . _host = ts . createCompilerHost ( this . _compilerOptions , false ) ;
7780 }
7881
82+ this . _nmf = options . nmf ;
7983 this . source = 'described-resolve' ;
8084 this . target = 'resolve' ;
8185
@@ -84,7 +88,7 @@ export class PathsPlugin implements Tapable {
8488 this . _compilerOptions . baseUrl || '.'
8589 ) ;
8690
87- this . mappings = [ ] ;
91+ this . _mappings = [ ] ;
8892 let paths = this . _compilerOptions . paths || { } ;
8993 Object . keys ( paths ) . forEach ( alias => {
9094 let onlyModule = alias . indexOf ( '*' ) === - 1 ;
@@ -99,7 +103,7 @@ export class PathsPlugin implements Tapable {
99103 aliasPattern = new RegExp ( `^${ withStarCapturing } ` ) ;
100104 }
101105
102- this . mappings . push ( {
106+ this . _mappings . push ( {
103107 onlyModule,
104108 alias,
105109 aliasPattern,
@@ -116,59 +120,35 @@ export class PathsPlugin implements Tapable {
116120 resolver . apply ( new ModulesInRootPlugin ( 'module' , this . _absoluteBaseUrl , 'resolve' ) ) ;
117121 }
118122
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 ; }
123128
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+ }
164133
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+ }
171149 }
172- } ;
150+
151+ return callback ( null , request ) ;
152+ } ) ;
173153 }
174154}
0 commit comments