Skip to content

Commit a74aa58

Browse files
committedJul 3, 2024·
fix(esm): implicit resolution with tsconfig paths
fixes #607
1 parent 1603c66 commit a74aa58

File tree

3 files changed

+64
-29
lines changed

3 files changed

+64
-29
lines changed
 

‎src/esm/hook/resolve.ts

+38-29
Original file line numberDiff line numberDiff line change
@@ -92,28 +92,6 @@ const resolveBase: ResolveHook = async (
9292
context,
9393
nextResolve,
9494
) => {
95-
// If directory, can be index.js, index.ts, etc.
96-
97-
// Resolve TS alias
98-
// Bare specifier
99-
if (
100-
!requestAcceptsQuery(specifier)
101-
// TS path alias
102-
&& tsconfigPathsMatcher
103-
&& !context.parentURL?.includes('/node_modules/')
104-
) {
105-
const possiblePaths = tsconfigPathsMatcher(specifier);
106-
for (const possiblePath of possiblePaths) {
107-
try {
108-
return await resolveBase(
109-
pathToFileURL(possiblePath).toString(),
110-
context,
111-
nextResolve,
112-
);
113-
} catch {}
114-
}
115-
}
116-
11795
const isBarePackageName = isBarePackageNamePattern.test(specifier);
11896

11997
// Typescript gives .ts, .cts, or .mts priority over actual .js, .cjs, or .mjs extensions
@@ -160,11 +138,10 @@ const resolveDirectory: ResolveHook = async (
160138
context,
161139
nextResolve,
162140
) => {
163-
const [specifierWithoutQuery, query] = specifier.split('?');
164-
165141
if (isDirectoryPattern.test(specifier)) {
142+
// If directory, can be index.js, index.ts, etc.
166143
return (await resolveExtensions(
167-
`${specifierWithoutQuery}index${query ? `?${query}` : ''}`,
144+
`${specifier}index`,
168145
context,
169146
nextResolve,
170147
true,
@@ -181,7 +158,7 @@ const resolveDirectory: ResolveHook = async (
181158
if (errorPath) {
182159
try {
183160
return (await resolveExtensions(
184-
`${errorPath}/index${query ? `?${query}` : ''}`,
161+
`${errorPath}/index`,
185162
context,
186163
nextResolve,
187164
true,
@@ -201,6 +178,33 @@ const resolveDirectory: ResolveHook = async (
201178
}
202179
};
203180

181+
const resolveTsPaths: ResolveHook = async (
182+
specifier,
183+
context,
184+
nextResolve,
185+
) => {
186+
if (
187+
// Bare specifier
188+
!requestAcceptsQuery(specifier)
189+
// TS path alias
190+
&& tsconfigPathsMatcher
191+
&& !context.parentURL?.includes('/node_modules/')
192+
) {
193+
const possiblePaths = tsconfigPathsMatcher(specifier);
194+
for (const possiblePath of possiblePaths) {
195+
try {
196+
return await resolveDirectory(
197+
pathToFileURL(possiblePath).toString(),
198+
context,
199+
nextResolve,
200+
);
201+
} catch {}
202+
}
203+
}
204+
205+
return resolveDirectory(specifier, context, nextResolve);
206+
};
207+
204208
export const resolve: ResolveHook = async (
205209
specifier,
206210
context,
@@ -219,8 +223,10 @@ export const resolve: ResolveHook = async (
219223
return nextResolve(specifier, context);
220224
}
221225

222-
const resolved = await resolveDirectory(
223-
specifier,
226+
const [cleanSpecifier, query] = specifier.split('?');
227+
228+
const resolved = await resolveTsPaths(
229+
cleanSpecifier,
224230
context,
225231
nextResolve,
226232
);
@@ -237,11 +243,14 @@ export const resolve: ResolveHook = async (
237243
resolved.format = await getFormatFromFileUrl(resolved.url);
238244
}
239245

246+
if (query) {
247+
resolved.url += `?${query}`;
248+
}
249+
240250
// Inherit namespace
241251
if (
242252
requestNamespace
243253
&& !resolved.url.includes(namespaceQuery)
244-
&& requestAcceptsQuery(resolved.url)
245254
) {
246255
resolved.url += (resolved.url.includes('?') ? '&' : '?') + namespaceQuery + requestNamespace;
247256
}

‎tests/fixtures.ts

+8
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,14 @@ export const files = {
251251
${sourcemap.test('cts')}
252252
`,
253253

254+
'tsconfig.json': createTsconfig({
255+
compilerOptions: {
256+
paths: {
257+
'@/*': ['./*'],
258+
},
259+
},
260+
}),
261+
254262
'file.txt': 'hello',
255263

256264
'broken-syntax.ts': 'if',

‎tests/specs/smoke.ts

+18
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@ export default testSuite(async ({ describe }, { tsx, supports, version }: NodeAp
4444
// .js in esm syntax
4545
import * as js from './js/index.js';
4646
import './js/index.js?query=123';
47+
import '@/js/index.js?query=123';
4748
import './js/index';
49+
import '@/js/index';
4850
import './js/';
51+
import '@/js/';
4952
5053
// No double .default.default in Dynamic Import
5154
import/* comment */('./js/index.js').then(m => {
@@ -63,7 +66,9 @@ export default testSuite(async ({ describe }, { tsx, supports, version }: NodeAp
6366
// .json
6467
import * as json from './json/index.json';
6568
import './json/index';
69+
import '@/json/index';
6670
import './json/';
71+
import '@/json/';
6772
6873
// .cjs
6974
import * as cjs from './cjs/index.cjs';
@@ -202,6 +207,7 @@ export default testSuite(async ({ describe }, { tsx, supports, version }: NodeAp
202207
// .js in esm syntax
203208
import * as js from './js/index.js';
204209
import './js/index.js?query=123';
210+
import '@/js/index.js?query=123';
205211
import './js/index';
206212
import './js/';
207213
@@ -265,26 +271,37 @@ export default testSuite(async ({ describe }, { tsx, supports, version }: NodeAp
265271
// .ts
266272
import './ts/index.ts';
267273
import './ts/index.js';
274+
import '@/ts/index.js';
268275
import './ts/index.jsx';
269276
import './ts/index';
277+
import '@/ts/index';
270278
import './ts/';
279+
import '@/ts/';
271280
import './ts/period.in.name';
281+
import '@/ts/period.in.name';
272282
273283
// .jsx
274284
import * as jsx from './jsx/index.jsx';
275285
import './jsx/index.js';
286+
import '@/jsx/index.js';
276287
import './jsx/index';
288+
import '@/jsx/index';
277289
import './jsx/';
290+
import '@/jsx/';
278291
279292
// .tsx
280293
import './tsx/index.tsx';
281294
import './tsx/index.js';
282295
import './tsx/index.jsx';
296+
import '@/tsx/index.jsx';
283297
import './tsx/index';
298+
import '@/tsx/index';
284299
import './tsx/';
300+
import '@/tsx/';
285301
286302
// .cts
287303
import './cts/index.cjs';
304+
import '@/cts/index.cjs';
288305
expectErrors(
289306
// TODO:
290307
// [() => import ('./cts/index.cts'), 'Cannot find module'],
@@ -303,6 +320,7 @@ export default testSuite(async ({ describe }, { tsx, supports, version }: NodeAp
303320
304321
// .mts
305322
import './mts/index.mjs';
323+
import '@/mts/index.mjs';
306324
expectErrors(
307325
// TODO:
308326
// [() => import ('./mts/index.mts'), 'Cannot find module'],

0 commit comments

Comments
 (0)
Please sign in to comment.