Skip to content

Commit 107172f

Browse files
committed
refactor
1 parent 3ca2d35 commit 107172f

File tree

4 files changed

+19
-12
lines changed

4 files changed

+19
-12
lines changed

src/build.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ async function buildSingle(
296296
format,
297297
configFiles,
298298
bundle,
299-
true,
299+
true, // cjsDts
300300
isDualFormat,
301301
),
302302
)

src/features/rolldown.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
type Plugin,
1212
type RolldownPluginOption,
1313
} from 'rolldown'
14+
import { filename_js_to_dts, RE_JS } from 'rolldown-plugin-dts/internal'
1415
import { importGlobPlugin } from 'rolldown/experimental'
1516
import pkg from '../../package.json' with { type: 'json' }
1617
import { mergeUserOptions } from '../config/options.ts'
@@ -349,16 +350,19 @@ export function CjsDtsReexportPlugin(): Plugin {
349350
for (const chunk of Object.values(bundle)) {
350351
if (chunk.type !== 'chunk') continue
351352

352-
const match = chunk.fileName.match(/^(.*)\.(cjs|js)$/)
353-
if (!match) continue
353+
if (!chunk.fileName.endsWith('.cjs') && !chunk.fileName.endsWith('.js'))
354+
continue
354355

355-
const baseName = match[1]
356-
const dCtsName =
357-
match[2] === 'cjs' ? `${baseName}.d.cts` : `${baseName}.d.ts`
358-
const dMtsBasename = path.basename(`${baseName}.d.mts`)
356+
const dMtsBasename = path.basename(
357+
chunk.fileName.replace(RE_JS, '.d.mts'),
358+
)
359359
const content = `export * from './${dMtsBasename}'\n`
360360

361-
this.emitFile({ type: 'asset', fileName: dCtsName, source: content })
361+
this.emitFile({
362+
type: 'prebuilt-chunk',
363+
fileName: filename_js_to_dts(chunk.fileName),
364+
code: content,
365+
})
362366
}
363367
},
364368
}

tests/__snapshots__/cjs-dts-reexport.snap.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## index.cjs
1+
## folder/index.cjs
22

33
```cjs
44
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
@@ -11,14 +11,14 @@ exports.hello = hello;
1111

1212
```
1313

14-
## index.d.cts
14+
## folder/index.d.cts
1515

1616
```cts
1717
export * from './index.d.mts'
1818

1919
```
2020

21-
## index.d.mts
21+
## folder/index.d.mts
2222

2323
```mts
2424
//#region index.d.ts
@@ -27,7 +27,7 @@ declare function hello(): void;
2727
export { hello };
2828
```
2929

30-
## index.mjs
30+
## folder/index.mjs
3131

3232
```mjs
3333
//#region index.ts

tests/e2e.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ test('cjs dts reexport', async (context) => {
108108
context,
109109
files,
110110
options: {
111+
entry: {
112+
'folder/index': 'index.ts',
113+
},
111114
format: ['esm', 'cjs'],
112115
dts: { cjsReexport: true },
113116
},

0 commit comments

Comments
 (0)