Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: rootDir in bundle DTS #78

Merged
merged 4 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions e2e/cases/dts/bundle-false/__fixtures__/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"extends": "@rslib/tsconfig/base",
"compilerOptions": {
"baseUrl": "./",
"rootDir": "src"
"baseUrl": "./"
},
"include": ["src"]
}
3 changes: 1 addition & 2 deletions e2e/cases/dts/bundle-false/abort-on-error/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"extends": "@rslib/tsconfig/base",
"compilerOptions": {
"baseUrl": "./",
"rootDir": "src"
"baseUrl": "./"
},
"include": ["src"]
}
3 changes: 1 addition & 2 deletions e2e/cases/dts/bundle/__fixtures__/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"extends": "@rslib/tsconfig/base",
"compilerOptions": {
"baseUrl": "./",
"rootDir": "src"
"baseUrl": "./"
},
"include": ["src"]
}
3 changes: 1 addition & 2 deletions e2e/cases/dts/bundle/abort-on-error/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"extends": "@rslib/tsconfig/base",
"compilerOptions": {
"baseUrl": "./",
"rootDir": "src"
"baseUrl": "./"
},
"include": ["src"]
}
4 changes: 2 additions & 2 deletions packages/plugin-dts/src/apiExtractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export async function bundleDts(options: BundleOptions): Promise<void> {
`API Extractor bundle DTS succeeded: ${color.cyan(untrimmedFilePath)} in ${getTimeCost(start)} ${color.gray(`(${name})`)}`,
);
} catch (e) {
logger.error('API Extractor', e);
throw new Error('API Extractor rollup error');
logger.error('API Extractor Error');
throw new Error(`${e}`);
}
}
15 changes: 11 additions & 4 deletions packages/plugin-dts/src/dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import color from 'picocolors';
import type { DtsGenOptions } from 'src';
import * as ts from 'typescript';
import { emitDts } from './tsc';
import { ensureTempDeclarationDir, loadTsconfig } from './utils';
import {
calcLongestCommonPath,
ensureTempDeclarationDir,
loadTsconfig,
} from './utils';

const isObject = (obj: unknown): obj is Record<string, any> =>
Object.prototype.toString.call(obj) === '[object Object]';
Expand Down Expand Up @@ -115,8 +119,12 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
logger.error(`tsconfig.json not found in ${cwd}`);
throw new Error();
}
const { options: rawCompilerOptions } = loadTsconfig(configPath);
const rootDir = rawCompilerOptions.rootDir ?? 'src';
const { options: rawCompilerOptions, fileNames } = loadTsconfig(configPath);
const rootDir =
rawCompilerOptions.rootDir ??
(await calcLongestCommonPath(fileNames)) ??
dirname(configPath);

const outDir = distPath
? distPath
: rawCompilerOptions.declarationDir || './dist';
Expand Down Expand Up @@ -178,7 +186,6 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
name,
cwd,
configPath,
rootDir,
declarationDir,
dtsExtension,
},
Expand Down
1 change: 0 additions & 1 deletion packages/plugin-dts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export const PLUGIN_DTS_NAME = 'rsbuild:dts';
// use ts compiler API to generate bundleless dts
// use ts compiler API and api-extractor to generate dts bundle
// TODO: support incremental build, to build one or more projects and their dependencies
// TODO: support autoExtension for dts files
// TODO: deal alias in dts
export const pluginDts = (options: PluginDtsOptions): RsbuildPlugin => ({
name: PLUGIN_DTS_NAME,
Expand Down
1 change: 0 additions & 1 deletion packages/plugin-dts/src/tsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export type EmitDtsOptions = {
name: string;
cwd: string;
configPath: string;
rootDir: string;
declarationDir: string;
dtsExtension: string;
};
Expand Down
33 changes: 33 additions & 0 deletions packages/plugin-dts/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'node:fs';
import fsP from 'node:fs/promises';
import path from 'node:path';
import { type RsbuildConfig, logger } from '@rsbuild/core';
import fg from 'fast-glob';
Expand Down Expand Up @@ -97,3 +98,35 @@ export function processSourceEntry(
'@microsoft/api-extractor only support single entry of Record<string, string> type to bundle DTS, please check your entry config.',
);
}

// same as @rslib/core, we should extract into a single published package to share
export async function calcLongestCommonPath(
absPaths: string[],
): Promise<string | null> {
if (absPaths.length === 0) {
return null;
}

const splitPaths = absPaths.map((p) => p.split(path.sep));
let lcaFragments = splitPaths[0]!;
for (let i = 1; i < splitPaths.length; i++) {
const currentPath = splitPaths[i]!;
const minLength = Math.min(lcaFragments.length, currentPath.length);

let j = 0;
while (j < minLength && lcaFragments[j] === currentPath[j]) {
j++;
}

lcaFragments = lcaFragments.slice(0, j);
}

let lca = lcaFragments.length > 0 ? lcaFragments.join(path.sep) : '/';

const stats = await fsP.stat(lca);
if (stats?.isFile()) {
lca = path.dirname(lca);
}

return lca;
}
Loading