@@ -20,7 +20,7 @@ export function resolveWithPaths(
2020 host : ts . CompilerHost ,
2121 cache ?: ts . ModuleResolutionCache ,
2222) {
23- if ( ! request ) {
23+ if ( ! request || ! request . request ) {
2424 callback ( null , request ) ;
2525 return ;
2626 }
@@ -31,6 +31,26 @@ export function resolveWithPaths(
3131 return ;
3232 }
3333
34+ // check if any path mapping rules are relevant
35+ const isPathMapped = compilerOptions . paths && Object . keys ( compilerOptions . paths )
36+ . some ( pattern => {
37+ // can only contain zero or one
38+ const starIndex = pattern . indexOf ( '*' ) ;
39+ if ( starIndex === - 1 ) {
40+ return pattern === request . request ;
41+ } else if ( starIndex === pattern . length - 1 ) {
42+ return request . request . startsWith ( pattern . slice ( 0 , - 1 ) ) ;
43+ } else {
44+ const [ prefix , suffix ] = pattern . split ( '*' ) ;
45+ return request . request . startsWith ( prefix ) && request . request . endsWith ( suffix ) ;
46+ }
47+ } ) ;
48+
49+ if ( ! isPathMapped ) {
50+ callback ( null , request ) ;
51+ return ;
52+ }
53+
3454 const moduleResolver = ts . resolveModuleName (
3555 request . request ,
3656 request . contextInfo . issuer ,
@@ -64,40 +84,6 @@ export function resolveWithPaths(
6484 return ;
6585 }
6686
67- // TypeScript gives `index.ts` and the request is not for the specific file,
68- // check if it is a module
69- const requestFilePath = path . basename ( request . request ) ;
70- if ( path . basename ( moduleFilePath ) === 'index.ts'
71- && requestFilePath !== 'index' && requestFilePath !== 'index.ts' ) {
72- const packageRootPath = path . join ( path . dirname ( moduleFilePath ) , 'package.json' ) ;
73- if ( host . fileExists ( packageRootPath ) ) {
74- // potential module request
75- let isPathMapped = false ;
76- if ( compilerOptions . paths ) {
77- // check if any path mapping rules are relevant
78- isPathMapped = Object . keys ( compilerOptions . paths )
79- . some ( pattern => {
80- // can only contain zero or one
81- const starIndex = pattern . indexOf ( '*' ) ;
82- if ( starIndex === - 1 ) {
83- return pattern === request . request ;
84- } else if ( starIndex === pattern . length - 1 ) {
85- return request . request . startsWith ( pattern . slice ( 0 , - 1 ) ) ;
86- } else {
87- const [ prefix , suffix ] = pattern . split ( '*' ) ;
88- return request . request . startsWith ( prefix ) && request . request . endsWith ( suffix ) ;
89- }
90- } ) ;
91- }
92- if ( ! isPathMapped ) {
93- // path mapping not involved, let webpack handle the module request
94- request . request = path . dirname ( moduleFilePath ) ;
95- callback ( null , request ) ;
96- return ;
97- }
98- }
99- }
100-
10187 request . request = moduleFilePath ;
10288 callback ( null , request ) ;
10389}
0 commit comments