Skip to content

Commit e110f93

Browse files
authored
fix: rootDir in bundle DTS (#78)
1 parent 9d40d43 commit e110f93

File tree

9 files changed

+50
-16
lines changed

9 files changed

+50
-16
lines changed
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{
22
"extends": "@rslib/tsconfig/base",
33
"compilerOptions": {
4-
"baseUrl": "./",
5-
"rootDir": "src"
4+
"baseUrl": "./"
65
},
76
"include": ["src"]
87
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{
22
"extends": "@rslib/tsconfig/base",
33
"compilerOptions": {
4-
"baseUrl": "./",
5-
"rootDir": "src"
4+
"baseUrl": "./"
65
},
76
"include": ["src"]
87
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{
22
"extends": "@rslib/tsconfig/base",
33
"compilerOptions": {
4-
"baseUrl": "./",
5-
"rootDir": "src"
4+
"baseUrl": "./"
65
},
76
"include": ["src"]
87
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{
22
"extends": "@rslib/tsconfig/base",
33
"compilerOptions": {
4-
"baseUrl": "./",
5-
"rootDir": "src"
4+
"baseUrl": "./"
65
},
76
"include": ["src"]
87
}

packages/plugin-dts/src/apiExtractor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export async function bundleDts(options: BundleOptions): Promise<void> {
7373
`API Extractor bundle DTS succeeded: ${color.cyan(untrimmedFilePath)} in ${getTimeCost(start)} ${color.gray(`(${name})`)}`,
7474
);
7575
} catch (e) {
76-
logger.error('API Extractor', e);
77-
throw new Error('API Extractor rollup error');
76+
logger.error('API Extractor Error');
77+
throw new Error(`${e}`);
7878
}
7979
}

packages/plugin-dts/src/dts.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import color from 'picocolors';
55
import type { DtsGenOptions } from 'src';
66
import * as ts from 'typescript';
77
import { emitDts } from './tsc';
8-
import { ensureTempDeclarationDir, loadTsconfig } from './utils';
8+
import {
9+
calcLongestCommonPath,
10+
ensureTempDeclarationDir,
11+
loadTsconfig,
12+
} from './utils';
913

1014
const isObject = (obj: unknown): obj is Record<string, any> =>
1115
Object.prototype.toString.call(obj) === '[object Object]';
@@ -115,8 +119,12 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
115119
logger.error(`tsconfig.json not found in ${cwd}`);
116120
throw new Error();
117121
}
118-
const { options: rawCompilerOptions } = loadTsconfig(configPath);
119-
const rootDir = rawCompilerOptions.rootDir ?? 'src';
122+
const { options: rawCompilerOptions, fileNames } = loadTsconfig(configPath);
123+
const rootDir =
124+
rawCompilerOptions.rootDir ??
125+
(await calcLongestCommonPath(fileNames)) ??
126+
dirname(configPath);
127+
120128
const outDir = distPath
121129
? distPath
122130
: rawCompilerOptions.declarationDir || './dist';
@@ -178,7 +186,6 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
178186
name,
179187
cwd,
180188
configPath,
181-
rootDir,
182189
declarationDir,
183190
dtsExtension,
184191
},

packages/plugin-dts/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ export const PLUGIN_DTS_NAME = 'rsbuild:dts';
4141
// use ts compiler API to generate bundleless dts
4242
// use ts compiler API and api-extractor to generate dts bundle
4343
// TODO: support incremental build, to build one or more projects and their dependencies
44-
// TODO: support autoExtension for dts files
4544
// TODO: deal alias in dts
4645
export const pluginDts = (options: PluginDtsOptions): RsbuildPlugin => ({
4746
name: PLUGIN_DTS_NAME,

packages/plugin-dts/src/tsc.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export type EmitDtsOptions = {
1212
name: string;
1313
cwd: string;
1414
configPath: string;
15-
rootDir: string;
1615
declarationDir: string;
1716
dtsExtension: string;
1817
};

packages/plugin-dts/src/utils.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from 'node:fs';
2+
import fsP from 'node:fs/promises';
23
import path from 'node:path';
34
import { type RsbuildConfig, logger } from '@rsbuild/core';
45
import fg from 'fast-glob';
@@ -97,3 +98,35 @@ export function processSourceEntry(
9798
'@microsoft/api-extractor only support single entry of Record<string, string> type to bundle DTS, please check your entry config.',
9899
);
99100
}
101+
102+
// same as @rslib/core, we should extract into a single published package to share
103+
export async function calcLongestCommonPath(
104+
absPaths: string[],
105+
): Promise<string | null> {
106+
if (absPaths.length === 0) {
107+
return null;
108+
}
109+
110+
const splitPaths = absPaths.map((p) => p.split(path.sep));
111+
let lcaFragments = splitPaths[0]!;
112+
for (let i = 1; i < splitPaths.length; i++) {
113+
const currentPath = splitPaths[i]!;
114+
const minLength = Math.min(lcaFragments.length, currentPath.length);
115+
116+
let j = 0;
117+
while (j < minLength && lcaFragments[j] === currentPath[j]) {
118+
j++;
119+
}
120+
121+
lcaFragments = lcaFragments.slice(0, j);
122+
}
123+
124+
let lca = lcaFragments.length > 0 ? lcaFragments.join(path.sep) : '/';
125+
126+
const stats = await fsP.stat(lca);
127+
if (stats?.isFile()) {
128+
lca = path.dirname(lca);
129+
}
130+
131+
return lca;
132+
}

0 commit comments

Comments
 (0)