Skip to content

Commit 89cb72a

Browse files
feat: get tsconfigPath and entryPath from enviroment config (#67)
Co-authored-by: Wei <[email protected]>
1 parent cfcc89f commit 89cb72a

File tree

12 files changed

+73
-64
lines changed

12 files changed

+73
-64
lines changed

e2e/cases/dts/__snapshots__/index.test.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3-
exports[`dts when bundle: false > basic - bundleless dts 1`] = `
3+
exports[`dts when bundle: false > basic 1`] = `
44
{
55
"./dist/esm/index.d.ts": "export * from './utils/numbers';
66
export * from './utils/strings';
@@ -20,7 +20,7 @@ export declare const str3 = "str3";
2020
}
2121
`;
2222

23-
exports[`dts when bundle: true > basic - bundle dts 1`] = `
23+
exports[`dts when bundle: true > basic 1`] = `
2424
{
2525
"esm": "export declare const num1 = 1;
2626

e2e/cases/dts/index.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { buildAndGetResults } from '@e2e/helper';
33
import { describe, expect, test } from 'vitest';
44

55
describe('dts when bundle: false', () => {
6-
test('basic - bundleless dts', async () => {
6+
test('basic', async () => {
77
const fixturePath = join(__dirname, 'bundle-false');
88
const { files, contents } = await buildAndGetResults(
99
fixturePath,
@@ -16,22 +16,22 @@ describe('dts when bundle: false', () => {
1616
expect(contents.esm).toMatchSnapshot();
1717
});
1818

19-
test('dts false - bundleless dts', async () => {
19+
test('dts false', async () => {
2020
const fixturePath = join(__dirname, 'bundle-false');
2121
const { files } = await buildAndGetResults(
2222
fixturePath,
23-
'rslib.false.config.ts',
23+
'dtsFalse.config.ts',
2424
'dts',
2525
);
2626

2727
expect(files.esm).toBe(undefined);
2828
});
2929

30-
test('distPath - bundleless dts', async () => {
30+
test('distPath', async () => {
3131
const fixturePath = join(__dirname, 'bundle-false');
3232
const { files } = await buildAndGetResults(
3333
fixturePath,
34-
'rslib.distpath.config.ts',
34+
'distPath.config.ts',
3535
'dts',
3636
);
3737
expect(files.esm?.length).toBe(4);
@@ -40,7 +40,7 @@ describe('dts when bundle: false', () => {
4040
});
4141

4242
describe('dts when bundle: true', () => {
43-
test('basic - bundle dts', async () => {
43+
test('basic', async () => {
4444
const fixturePath = join(__dirname, 'bundle');
4545
const { entryFiles, entries } = await buildAndGetResults(
4646
fixturePath,
@@ -52,22 +52,22 @@ describe('dts when bundle: true', () => {
5252
expect(entries).toMatchSnapshot();
5353
});
5454

55-
test('dts false - bundle dts', async () => {
55+
test('dts false', async () => {
5656
const fixturePath = join(__dirname, 'bundle');
5757
const { entryFiles } = await buildAndGetResults(
5858
fixturePath,
59-
'rslib.false.config.ts',
59+
'dtsFalse.config.ts',
6060
'dts',
6161
);
6262

6363
expect(entryFiles.esm).toEqual(undefined);
6464
});
6565

66-
test('distPath - bundle dts', async () => {
66+
test('distPath', async () => {
6767
const fixturePath = join(__dirname, 'bundle');
6868
const { entryFiles } = await buildAndGetResults(
6969
fixturePath,
70-
'rslib.distpath.config.ts',
70+
'distPath.config.ts',
7171
'dts',
7272
);
7373

packages/core/src/config.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,6 @@ const composeBundleConfig = (bundle = true): RsbuildConfig => {
325325

326326
const composeDtsConfig = async (
327327
libConfig: LibConfig,
328-
entryConfig: RsbuildConfig,
329328
): Promise<RsbuildConfig> => {
330329
const { dts, bundle, output } = libConfig;
331330

@@ -337,9 +336,6 @@ const composeDtsConfig = async (
337336
pluginDts({
338337
bundle: dts?.bundle ?? bundle,
339338
distPath: dts?.distPath ?? output?.distPath?.root ?? './dist',
340-
tsconfigPath: dts?.tsconfigPath,
341-
// TODO: temporarily use main as dts entry
342-
entryPath: entryConfig.source?.entry?.main as string,
343339
}),
344340
],
345341
};
@@ -408,8 +404,7 @@ async function composeLibRsbuildConfig(
408404
config.bundle,
409405
dirname(configPath),
410406
);
411-
412-
const dtsConfig = await composeDtsConfig(config, entryConfig);
407+
const dtsConfig = await composeDtsConfig(config);
413408

414409
return mergeRsbuildConfig(
415410
formatConfig,

packages/core/src/types/config/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export type Dts =
2727
| {
2828
bundle: boolean;
2929
distPath?: string;
30-
tsconfigPath?: string;
3130
}
3231
| false;
3332

packages/plugin-dts/src/apiExtractor.ts

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,43 @@ export function bundleDts(options: BundleOptions): void {
2020
entry = 'index.d.ts',
2121
tsconfigPath = 'tsconfig.json',
2222
} = options;
23-
const untrimmedFilePath = join(cwd, relative(cwd, outDir), 'index.d.ts');
24-
const internalConfig = {
25-
mainEntryPointFilePath: entry,
26-
// TODO: use !externals
27-
// bundledPackages: [],
28-
dtsRollup: {
29-
enabled: true,
30-
untrimmedFilePath,
31-
},
32-
compiler: {
33-
tsconfigFilePath: join(cwd, tsconfigPath),
34-
},
35-
projectFolder: cwd,
36-
};
23+
try {
24+
const untrimmedFilePath = join(cwd, relative(cwd, outDir), 'index.d.ts');
25+
const internalConfig = {
26+
mainEntryPointFilePath: entry,
27+
// TODO: use !externals
28+
// bundledPackages: [],
29+
dtsRollup: {
30+
enabled: true,
31+
untrimmedFilePath,
32+
},
33+
compiler: {
34+
tsconfigFilePath: tsconfigPath.includes(cwd)
35+
? tsconfigPath
36+
: join(cwd, tsconfigPath),
37+
},
38+
projectFolder: cwd,
39+
};
3740

38-
const extractorConfig = ExtractorConfig.prepare({
39-
configObject: internalConfig,
40-
configObjectFullPath: undefined,
41-
packageJsonFullPath: join(cwd, 'package.json'),
42-
});
41+
const extractorConfig = ExtractorConfig.prepare({
42+
configObject: internalConfig,
43+
configObjectFullPath: undefined,
44+
packageJsonFullPath: join(cwd, 'package.json'),
45+
});
4346

44-
const extractorResult: ExtractorResult = Extractor.invoke(extractorConfig, {
45-
localBuild: true,
46-
});
47+
const extractorResult: ExtractorResult = Extractor.invoke(extractorConfig, {
48+
localBuild: true,
49+
});
4750

48-
if (!extractorResult.succeeded) {
51+
if (!extractorResult.succeeded) {
52+
throw new Error('API Extractor rollup error');
53+
}
54+
55+
logger.info(
56+
`API Extractor writing package typings succeeded: ${untrimmedFilePath}`,
57+
);
58+
} catch (e) {
59+
logger.error('API Extractor', e);
4960
throw new Error('API Extractor rollup error');
5061
}
51-
52-
logger.info(
53-
`API Extractor writing package typings succeeded: ${untrimmedFilePath}`,
54-
);
5562
}

packages/plugin-dts/src/dts.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import { emitDts } from './tsc';
77
import { ensureTempDeclarationDir, loadTsconfig } from './utils';
88

99
export async function generateDts(data: DtsGenOptions): Promise<void> {
10-
const { options: pluginOptions, cwd, isWatch, name } = data;
10+
const { tsconfigPath, distPath, bundle, entryPath, cwd, isWatch, name } =
11+
data;
1112
logger.start(`Generating DTS... ${color.gray(`(${name})`)}`);
12-
const { tsconfigPath, distPath, bundle, entryPath } = pluginOptions;
1313
const configPath = ts.findConfigFile(cwd, ts.sys.fileExists, tsconfigPath);
1414
if (!configPath) {
1515
logger.error(`tsconfig.json not found in ${cwd}`);
@@ -38,7 +38,10 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
3838
declarationDir!,
3939
relativePath,
4040
basename(entrySourcePath),
41-
).replace(/\.(m?js|jsx?|m?ts|tsx?|c?js)$/, '.d.ts');
41+
).replace(
42+
/\.(js|mjs|jsx|ts|mts|tsx|cjs|cts|cjsx|ctsx|mjsx|mtsx)$/,
43+
'.d.ts',
44+
);
4245
}
4346

4447
const bundleDtsIfNeeded = async () => {
@@ -77,7 +80,7 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
7780
}
7881

7982
process.on('message', async (data: DtsGenOptions) => {
80-
if (!data.options) {
83+
if (!data.cwd) {
8184
return;
8285
}
8386

0 commit comments

Comments
 (0)