1
1
import path from 'path' ;
2
+ import { pathToFileURL } from 'url' ;
2
3
import {
3
4
transform ,
4
5
transformDynamicImport ,
5
6
resolveTsPath ,
6
7
} from '@esbuild-kit/core-utils' ;
7
8
import {
8
- tsconfigRaw ,
9
9
sourcemaps ,
10
+ tsconfigRaw ,
11
+ tsconfigPathsMatcher ,
10
12
tsExtensionsPattern ,
11
13
getFormatFromExtension ,
12
14
type ModuleFormat ,
@@ -77,11 +79,14 @@ async function tryDirectory(
77
79
}
78
80
}
79
81
82
+ const fileProtocol = 'file://' ;
83
+ const isPathPattern = / ^ \. { 0 , 2 } \/ / ;
84
+
80
85
export const resolve : resolve = async function (
81
86
specifier ,
82
87
context ,
83
88
defaultResolve ,
84
- resursiveCall ,
89
+ recursiveCall ,
85
90
) {
86
91
// Added in v12.20.0
87
92
// https://nodejs.org/api/esm.html#esm_node_imports
@@ -94,6 +99,27 @@ export const resolve: resolve = async function (
94
99
return await tryDirectory ( specifier , context , defaultResolve ) ;
95
100
}
96
101
102
+ const isPath = (
103
+ specifier . startsWith ( fileProtocol )
104
+ || isPathPattern . test ( specifier )
105
+ ) ;
106
+
107
+ if (
108
+ tsconfigPathsMatcher
109
+ && ! isPath // bare specifier
110
+ ) {
111
+ const possiblePaths = tsconfigPathsMatcher ( specifier ) ;
112
+ for ( const possiblePath of possiblePaths ) {
113
+ try {
114
+ return await resolve (
115
+ pathToFileURL ( possiblePath ) . toString ( ) ,
116
+ context ,
117
+ defaultResolve ,
118
+ ) ;
119
+ } catch { }
120
+ }
121
+ }
122
+
97
123
/**
98
124
* Typescript gives .ts, .cts, or .mts priority over actual .js, .cjs, or .mjs extensions
99
125
*/
@@ -117,7 +143,8 @@ export const resolve: resolve = async function (
117
143
} catch ( error ) {
118
144
if (
119
145
( error instanceof Error )
120
- && ! resursiveCall
146
+ && isPath
147
+ && ! recursiveCall
121
148
) {
122
149
if ( ( error as any ) . code === 'ERR_UNSUPPORTED_DIR_IMPORT' ) {
123
150
return await tryDirectory ( specifier , context , defaultResolve ) ;
@@ -140,7 +167,7 @@ export const resolve: resolve = async function (
140
167
141
168
let { format } = resolved ;
142
169
143
- if ( resolved . url . startsWith ( 'file:' ) ) {
170
+ if ( resolved . url . startsWith ( fileProtocol ) ) {
144
171
format = getFormatFromExtension ( resolved . url ) ?? format ;
145
172
146
173
if ( ! format ) {
0 commit comments