Skip to content

Commit ac4dafa

Browse files
committed
feat: respect entryFileNames
1 parent 11ed79d commit ac4dafa

File tree

3 files changed

+60
-19
lines changed

3 files changed

+60
-19
lines changed

rolldown.config.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,23 @@ import { dependencies } from './package.json'
33
import { dts } from './src/index'
44

55
const config = defineConfig({
6-
input: './src/index.ts',
6+
input: ['./src/index.ts', './src/generate.ts'],
77
plugins: [dts()],
88
external: Object.keys(dependencies),
99
platform: 'node',
1010
output: [
11-
{ dir: 'temp/esm', format: 'es' },
12-
// { dir: 'temp/cjs', format: 'cjs' },
11+
{
12+
dir: 'temp/esm',
13+
format: 'es',
14+
entryFileNames: '[name].mjs',
15+
chunkFileNames: '[name]-[hash].mjs',
16+
},
17+
// {
18+
// dir: 'temp/cjs',
19+
// format: 'cjs',
20+
// entryFileNames: '[name].cjs',
21+
// chunkFileNames: '[name]-[hash].cjs',
22+
// },
1323
],
1424
})
1525

src/generate.ts

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { basename } from 'node:path'
1+
import { basename, extname } from 'node:path'
22
import { createResolver } from 'dts-resolver'
33
import { isolatedDeclaration as oxcIsolatedDeclaration } from 'oxc-transform'
44
import {
55
filename_dts_to,
66
filename_ts_to_dts,
77
isRelative,
88
RE_DTS,
9+
RE_JS,
910
RE_NODE_MODULES,
1011
RE_TS,
1112
} from './utils/filename'
@@ -16,11 +17,19 @@ const meta = { dtsFile: true } as const
1617

1718
export function createGeneratePlugin({
1819
isolatedDeclaration,
19-
inputAlias = {},
20+
inputAlias,
2021
resolve = false,
2122
}: Pick<Options, 'isolatedDeclaration' | 'inputAlias' | 'resolve'>): Plugin {
22-
const dtsMap = new Map<string, string>()
23-
const inputAliasMap = new Map<string, string>(Object.entries(inputAlias))
23+
const dtsMap = new Map<
24+
string,
25+
{
26+
code: string
27+
src: string
28+
}
29+
>()
30+
const inputAliasMap = new Map<string, string>(
31+
inputAlias && Object.entries(inputAlias),
32+
)
2433
const resolver = createResolver()
2534

2635
let inputOption: Record<string, string> | undefined
@@ -33,6 +42,22 @@ export function createGeneratePlugin({
3342
}
3443
},
3544

45+
outputOptions(options) {
46+
return {
47+
...options,
48+
entryFileNames(chunk) {
49+
const original =
50+
(typeof options.entryFileNames === 'function'
51+
? options.entryFileNames(chunk)
52+
: options.entryFileNames) || '[name].js'
53+
if (chunk.name.endsWith('.d')) {
54+
return original.replace(RE_JS, '.$1ts')
55+
}
56+
return original
57+
},
58+
}
59+
},
60+
3661
transform: {
3762
order: 'pre',
3863
filter: {
@@ -47,27 +72,26 @@ export function createGeneratePlugin({
4772
code,
4873
isolatedDeclaration,
4974
)
50-
if (errors.length) {
51-
return this.error(errors[0])
52-
}
75+
if (errors.length) return this.error(errors[0])
5376

5477
const dtsId = filename_ts_to_dts(id)
55-
dtsMap.set(dtsId, dtsCode)
78+
dtsMap.set(dtsId, {
79+
code: dtsCode,
80+
src: id,
81+
})
5682

5783
const mod = this.getModuleInfo(id)
5884
if (mod?.isEntry) {
59-
let fileName = basename(dtsId)
60-
61-
if (inputAliasMap.has(fileName)) {
62-
fileName = inputAliasMap.get(fileName)!
85+
let name: string | undefined = basename(dtsId, extname(dtsId))
86+
if (inputAliasMap.has(name)) {
87+
name = inputAliasMap.get(name)!
6388
} else if (inputAliasMap.has(dtsId)) {
64-
fileName = inputAliasMap.get(dtsId)!
89+
name = inputAliasMap.get(dtsId)!
6590
}
66-
6791
this.emitFile({
6892
type: 'chunk',
6993
id: dtsId,
70-
fileName,
94+
name,
7195
})
7296
}
7397
},
@@ -146,7 +170,7 @@ export function createGeneratePlugin({
146170
handler(id) {
147171
if (dtsMap.has(id)) {
148172
return {
149-
code: dtsMap.get(id)!,
173+
code: dtsMap.get(id)!.code,
150174
moduleSideEffects: false,
151175
}
152176
}

src/types.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
declare module 'rolldown' {
2+
export interface CustomPluginOptions {
3+
dtsFile?: boolean
4+
}
5+
}
6+
7+
export {}

0 commit comments

Comments
 (0)