Skip to content

Commit 7b446ed

Browse files
committed
fix: patch cts & mts for import source
1 parent b8b4b2c commit 7b446ed

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/index.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,12 @@ export function dts(): Plugin {
6363
return {
6464
...options,
6565
resolve: {
66-
extensions: ['.d.ts'],
67-
extensionAlias: { '.js': ['.d.ts'] },
66+
extensions: ['.d.ts', '.d.mts', '.d.cts'],
67+
extensionAlias: {
68+
'.js': ['.d.ts'],
69+
'.mjs': ['.d.mts'],
70+
'.cjs': ['.d.cts'],
71+
},
6872
...options.resolve,
6973
},
7074
onLog(level, log, defaultHandler) {
@@ -386,17 +390,20 @@ function patchVariableDeclarator(
386390
}
387391
}
388392

393+
const RE_D_TS = /\.d\.([cm]?)ts$/
394+
389395
// patch `.d.ts` suffix in import source to `.js`
390396
function patchImportSource(s: MagicStringAST, node: Node) {
391397
if (
392398
(node.type === 'ImportDeclaration' ||
393399
node.type === 'ExportAllDeclaration' ||
394400
node.type === 'ExportNamedDeclaration') &&
395-
node.source?.value.endsWith('.d.ts')
401+
node.source?.value &&
402+
RE_D_TS.test(node.source.value)
396403
) {
397404
s.overwriteNode(
398405
node.source,
399-
JSON.stringify(`${node.source.value.slice(0, -4)}js`),
406+
JSON.stringify(node.source.value.replace(RE_D_TS, '.$1js')),
400407
)
401408
return true
402409
}

0 commit comments

Comments
 (0)