Skip to content

Commit 8ebd8b5

Browse files
authored
fix: Don't replace .d.ts paths (#234)
* Don't replace .d.ts paths * Add explicit isDTS detection * Fix path reconstruction
1 parent cdd3e1f commit 8ebd8b5

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

src/utils/trie.ts

+25-3
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,27 @@ export class TrieNode<T> {
7979
prefix: alias.replace(/\*$/, ''),
8080
// Normalize paths.
8181
paths: paths[alias].map((path) => {
82-
path = path
83-
.replace(/\*$/, '')
84-
.replace(/\.([mc])?ts(x)?$/, '.$1js$2');
82+
path = path.replace(/\*$/, '');
83+
84+
const dotIndex = path.lastIndexOf('.');
85+
const beforeDot = path.slice(0, dotIndex);
86+
const afterDot = path.slice(dotIndex);
87+
88+
// Refuse to normalize extensions for paths that look like "a.b/c" or "a.b\c".
89+
// Even if the current system is Linux the original author could've written a Windows path.
90+
if (!afterDot.includes('/') && !afterDot.includes('\\')) {
91+
const extension = afterDot;
92+
let normalizedExtension = afterDot;
93+
if (!isDTS(extension)) {
94+
normalizedExtension = extension.replace(
95+
/\.([mc])?ts(x)?$/,
96+
'.$1js$2'
97+
);
98+
}
99+
100+
path = beforeDot + normalizedExtension;
101+
}
102+
85103
if (isAbsolute(path)) {
86104
path = relative(
87105
resolve(config.configDir, config.baseUrl),
@@ -114,3 +132,7 @@ export class TrieNode<T> {
114132
return aliasTrie;
115133
}
116134
}
135+
136+
function isDTS(extension: string): boolean {
137+
return /\.d(\..*)?\.[mc]?ts(x)?$/.test(extension);
138+
}

0 commit comments

Comments
 (0)