Skip to content

Commit 4d95a86

Browse files
authored
fix(dts): preserve importer if resolved to node_modules folder (#1115)
1 parent 515ffae commit 4d95a86

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

packages/plugin-dts/src/utils.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,11 +307,16 @@ export async function redirectDtsImports(
307307
'.mts',
308308
'.cjs',
309309
'.cts',
310+
'.d.ts',
310311
]);
311312

312313
let redirectImportPath = importPath;
313314

314-
if (absoluteImportPath && redirect.path) {
315+
if (
316+
absoluteImportPath &&
317+
!absoluteImportPath.includes('node_modules') &&
318+
redirect.path
319+
) {
315320
const relativeRootDir = path.relative(
316321
normalize(rootDir),
317322
normalize(absoluteImportPath),
@@ -407,7 +412,12 @@ export async function processDtsFiles(
407412
return;
408413
}
409414

410-
const { absoluteBaseUrl, paths, mainFields, addMatchAll } = result;
415+
const { absoluteBaseUrl, paths, addMatchAll } = result;
416+
const mainFields: string[] = [];
417+
/**
418+
* resolve paths priorities:
419+
* see https://github.com/jonaskello/tsconfig-paths/blob/098e066632f5b9f35c956803fe60d17ffc60b688/src/match-path-sync.ts#L18-L26
420+
*/
411421
matchPath = createMatchPath(
412422
absoluteBaseUrl,
413423
paths,

tests/integration/redirect/dts.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ test('redirect.dts.path: true with redirect.dts.extension: false - default', asy
2929
export * from './foo';
3030
export * from './logger';
3131
export type { Foo } from './types';
32+
export { Router } from 'express';
3233
export * from '../../../compile/prebundle-pkg';
3334
export type { Bar } from './types';
3435
export * from './foo';
@@ -77,6 +78,7 @@ test('redirect.dts.path: false with redirect.dts.extension: false', async () =>
7778
export * from '@src/foo';
7879
export * from '@src/logger';
7980
export type { Foo } from '@src/types';
81+
export { Router } from 'express';
8082
export * from 'prebundle-pkg';
8183
export type { Bar } from 'types';
8284
export * from './foo';
@@ -125,6 +127,7 @@ test('redirect.dts.path: true with redirect.dts.extension: true', async () => {
125127
export * from './foo/index.js';
126128
export * from './logger.js';
127129
export type { Foo } from './types.js';
130+
export { Router } from 'express';
128131
export * from '../../../compile/prebundle-pkg';
129132
export type { Bar } from './types.js';
130133
export * from './foo/index.js';
@@ -173,6 +176,7 @@ test('redirect.dts.path: false with dts.redirect.extension: true', async () => {
173176
export * from '@src/foo';
174177
export * from '@src/logger';
175178
export type { Foo } from '@src/types';
179+
export { Router } from 'express';
176180
export * from 'prebundle-pkg';
177181
export type { Bar } from 'types';
178182
export * from './foo/index.js';
@@ -228,6 +232,7 @@ test('redirect.dts.extension: true with dts.autoExtension: true', async () => {
228232
export * from './foo/index.mjs';
229233
export * from './logger.mjs';
230234
export type { Foo } from './types.mjs';
235+
export { Router } from 'express';
231236
export * from '../../compile/prebundle-pkg';
232237
export type { Bar } from './types.mjs';
233238
export * from './foo/index.mjs';
@@ -243,6 +248,7 @@ test('redirect.dts.extension: true with dts.autoExtension: true', async () => {
243248
export * from './foo/index.js';
244249
export * from './logger.js';
245250
export type { Foo } from './types.js';
251+
export { Router } from 'express';
246252
export * from '../../compile/prebundle-pkg';
247253
export type { Bar } from './types.js';
248254
export * from './foo/index.js';

tests/integration/redirect/dts/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export {
1818
export * from '@src/foo';
1919
export * from '@src/logger';
2020
export type { Foo } from '@src/types';
21+
export { Router } from 'express';
2122
export * from 'prebundle-pkg';
2223
export type { Bar } from 'types';
2324
export * from './foo';

tests/integration/redirect/dts/tsconfig.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
"strict": true,
44
"skipLibCheck": true,
55
"paths": {
6-
"*": ["./src/*"],
76
"@src/*": ["./src/*"],
87
"prebundle-pkg": ["./compile/prebundle-pkg"],
9-
"self-entry": ["./src"]
8+
"self-entry": ["./src"],
9+
"express": ["./node_modules/express"],
10+
"*": ["./src/*"]
1011
}
1112
},
1213
"include": ["src/**/*"]

0 commit comments

Comments
 (0)