Skip to content

Commit 4e2fb8d

Browse files
authored
feat: support DTS bundle file name (#75)
1 parent 5e5aeff commit 4e2fb8d

File tree

6 files changed

+91
-13
lines changed

6 files changed

+91
-13
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { generateBundleCjsConfig, generateBundleEsmConfig } from '@e2e/helper';
2+
import { defineConfig } from '@rslib/core';
3+
4+
export default defineConfig({
5+
lib: [
6+
generateBundleEsmConfig(__dirname, {
7+
dts: {
8+
bundle: true,
9+
},
10+
}),
11+
generateBundleCjsConfig(__dirname),
12+
],
13+
source: {
14+
entry: {
15+
bundleName: './src/index.ts',
16+
},
17+
},
18+
});

e2e/cases/dts/index.test.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ describe('dts when bundle: true', () => {
9090
'dts',
9191
);
9292

93-
expect(entryFiles.esm).toEqual('./dist/esm/index.d.ts');
93+
expect(entryFiles.esm).toEqual('./dist/esm/main.d.ts');
9494
expect(entries).toMatchSnapshot();
9595
});
9696

@@ -113,7 +113,7 @@ describe('dts when bundle: true', () => {
113113
'dts',
114114
);
115115

116-
expect(entryFiles.esm).toEqual('./dist/custom/index.d.ts');
116+
expect(entryFiles.esm).toEqual('./dist/custom/main.d.ts');
117117
});
118118

119119
test('abortOnError: false', async () => {
@@ -135,6 +135,17 @@ describe('dts when bundle: true', () => {
135135
'dts',
136136
);
137137

138-
expect(entryFiles.cjs).toEqual('./dist/cjs/index.d.cts');
138+
expect(entryFiles.cjs).toEqual('./dist/cjs/main.d.cts');
139+
});
140+
141+
test('bundleName -- set source.entry', async () => {
142+
const fixturePath = join(__dirname, 'bundle');
143+
const { entryFiles } = await buildAndGetResults(
144+
fixturePath,
145+
'bundleName.config.ts',
146+
'dts',
147+
);
148+
149+
expect(entryFiles.esm).toEqual('./dist/esm/bundleName.d.ts');
139150
});
140151
});

packages/plugin-dts/src/apiExtractor.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import {
66
} from '@microsoft/api-extractor';
77
import { logger } from '@rsbuild/core';
88
import color from 'picocolors';
9+
import type { DtsEntry } from 'src';
910
import { getTimeCost } from './utils';
1011

1112
export type BundleOptions = {
1213
name: string;
1314
cwd: string;
1415
outDir: string;
1516
dtsExtension: string;
16-
entry?: string;
17+
dtsEntry: DtsEntry;
1718
tsconfigPath?: string;
1819
};
1920

@@ -23,18 +24,22 @@ export async function bundleDts(options: BundleOptions): Promise<void> {
2324
cwd,
2425
outDir,
2526
dtsExtension,
26-
entry = 'index.d.ts',
27+
dtsEntry = {
28+
name: 'index',
29+
path: 'index.d.ts',
30+
},
2731
tsconfigPath = 'tsconfig.json',
2832
} = options;
2933
try {
3034
const start = Date.now();
3135
const untrimmedFilePath = join(
3236
cwd,
3337
relative(cwd, outDir),
34-
`index${dtsExtension}`,
38+
`${dtsEntry.name}${dtsExtension}`,
3539
);
40+
const mainEntryPointFilePath = dtsEntry.path!;
3641
const internalConfig = {
37-
mainEntryPointFilePath: entry,
42+
mainEntryPointFilePath,
3843
// TODO: use !externals
3944
// bundledPackages: [],
4045
dtsRollup: {

packages/plugin-dts/src/dts.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
1010
const {
1111
bundle,
1212
distPath,
13-
entryPath,
13+
dtsEntry,
1414
tsconfigPath,
1515
name,
1616
cwd,
@@ -37,6 +37,7 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
3737
};
3838

3939
const declarationDir = getDeclarationDir(bundle!, distPath);
40+
const { name: entryName, path: entryPath } = dtsEntry;
4041
let entry = '';
4142

4243
if (bundle === true && entryPath) {
@@ -59,7 +60,10 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
5960
name,
6061
cwd,
6162
outDir,
62-
entry,
63+
dtsEntry: {
64+
name: entryName,
65+
path: entry,
66+
},
6367
tsconfigPath,
6468
dtsExtension,
6569
});
@@ -99,6 +103,7 @@ process.on('message', async (data: DtsGenOptions) => {
99103
try {
100104
await generateDts(data);
101105
} catch (e) {
106+
logger.error(e);
102107
process.send!('error');
103108
process.exit(1);
104109
}

packages/plugin-dts/src/index.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { fork } from 'node:child_process';
22
import { extname, join } from 'node:path';
33
import { type RsbuildPlugin, logger } from '@rsbuild/core';
4+
import { processSourceEntry } from './utils';
45

56
export type PluginDtsOptions = {
67
bundle?: boolean;
@@ -9,11 +10,16 @@ export type PluginDtsOptions = {
910
dtsExtension?: string;
1011
};
1112

13+
export type DtsEntry = {
14+
name?: string;
15+
path?: string;
16+
};
17+
1218
export type DtsGenOptions = PluginDtsOptions & {
1319
name: string;
1420
cwd: string;
1521
isWatch: boolean;
16-
entryPath?: string;
22+
dtsEntry: DtsEntry;
1723
tsconfigPath?: string;
1824
};
1925

@@ -54,10 +60,16 @@ export const pluginDts = (options: PluginDtsOptions): RsbuildPlugin => ({
5460
stdio: 'inherit',
5561
});
5662

63+
// TODO: @microsoft/api-extractor only support single entry to bundle DTS
64+
// use first element of Record<string, string> type entry config
65+
const dtsEntry = processSourceEntry(
66+
options.bundle!,
67+
config.source?.entry,
68+
);
69+
5770
const dtsGenOptions: DtsGenOptions = {
5871
...options,
59-
// TODO: temporarily use main as dts entry, only accept single entry
60-
entryPath: config.source?.entry?.main as string,
72+
dtsEntry,
6173
tsconfigPath: config.source.tsconfigPath,
6274
name: environment.name,
6375
cwd: api.context.rootPath,

packages/plugin-dts/src/utils.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import fs from 'node:fs';
22
import path from 'node:path';
3-
import { logger } from '@rsbuild/core';
3+
import { type RsbuildConfig, logger } from '@rsbuild/core';
44
import fg from 'fast-glob';
55
import color from 'picocolors';
6+
import type { DtsEntry } from 'src';
67
import * as ts from 'typescript';
78

89
export function loadTsconfig(tsconfigPath: string): ts.ParsedCommandLine {
@@ -70,3 +71,29 @@ export async function processDtsFiles(
7071
}
7172
}
7273
}
74+
75+
export function processSourceEntry(
76+
bundle: boolean,
77+
entryConfig: NonNullable<RsbuildConfig['source']>['entry'],
78+
): DtsEntry {
79+
if (!bundle) {
80+
return {
81+
name: undefined,
82+
path: undefined,
83+
};
84+
}
85+
86+
if (
87+
entryConfig &&
88+
Object.values(entryConfig).every((val) => typeof val === 'string')
89+
) {
90+
return {
91+
name: Object.keys(entryConfig)[0] as string,
92+
path: Object.values(entryConfig)[0] as string,
93+
};
94+
}
95+
96+
throw new Error(
97+
'@microsoft/api-extractor only support single entry of Record<string, string> type to bundle DTS, please check your entry config.',
98+
);
99+
}

0 commit comments

Comments
 (0)