diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index 02188fa9e..6b29e5691 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -307,7 +307,7 @@ const getDefaultDtsConfig = ( pluginDts({ bundle: dts?.bundle ?? bundle, distPath: dts?.distPath ?? output?.distPath?.root ?? './dist', - tsconfigPath: dts?.tsconfigPath ?? 'tsconfig.json', + tsconfigPath: dts?.tsconfigPath, // TODO: temporarily use main as dts entry entryPath: entryConfig.source?.entry?.main as string, }), diff --git a/packages/plugin-dts/src/dts.ts b/packages/plugin-dts/src/dts.ts index 4fcfbe8bd..013591e7c 100644 --- a/packages/plugin-dts/src/dts.ts +++ b/packages/plugin-dts/src/dts.ts @@ -1,6 +1,7 @@ import { basename, dirname, join, relative } from 'node:path'; import { logger } from '@rsbuild/core'; import type { DtsGenOptions } from 'src'; +import * as ts from 'typescript'; import { emitDts } from './tsc'; import { ensureTempDeclarationDir, loadTsconfig } from './utils'; @@ -9,7 +10,11 @@ export async function generateDts(data: DtsGenOptions) { const { options: pluginOptions, isWatch } = data; const { tsconfigPath, distPath, bundle, entryPath } = pluginOptions; const cwd = process.cwd(); - const configPath = join(cwd, tsconfigPath!); + const configPath = ts.findConfigFile(cwd, ts.sys.fileExists, tsconfigPath); + if (!configPath) { + logger.error(`tsconfig.json not found in ${cwd}`); + throw new Error(); + } const { options: rawCompilerOptions } = loadTsconfig(configPath); const rootDir = rawCompilerOptions.rootDir ?? 'src'; const outDir = distPath @@ -75,12 +80,15 @@ process.on('message', async (data: DtsGenOptions) => { return; } - await generateDts(data); - - if (process.send) { - process.send('success'); + try { + await generateDts(data); + } catch (e) { + process.send!('error'); + process.exit(1); } + process.send!('success'); + if (!data.isWatch) { process.exit(); } diff --git a/packages/plugin-dts/src/index.ts b/packages/plugin-dts/src/index.ts index 2bbf35df5..db6fcdee6 100644 --- a/packages/plugin-dts/src/index.ts +++ b/packages/plugin-dts/src/index.ts @@ -31,7 +31,6 @@ export const pluginDts = ( options.distPath = options.distPath ?? config.output?.distPath?.root ?? 'dist'; - options.tsconfigPath = options.tsconfigPath ?? 'tsconfig.json'; const dtsPromises: Promise[] = []; @@ -49,10 +48,12 @@ export const pluginDts = ( childProcess.send(sendData); dtsPromises.push( - new Promise((resolve) => { + new Promise((resolve, reject) => { childProcess.on('message', (message) => { if (message === 'success') { resolve(); + } else if (message === 'error') { + reject(new Error('Error occurred in dts generation')); } }); }), diff --git a/packages/plugin-dts/src/utils.ts b/packages/plugin-dts/src/utils.ts index 9a1fb6193..547fcca25 100644 --- a/packages/plugin-dts/src/utils.ts +++ b/packages/plugin-dts/src/utils.ts @@ -3,18 +3,14 @@ import path from 'node:path'; import * as ts from 'typescript'; export function loadTsconfig(tsconfigPath: string): ts.ParsedCommandLine { - if (fs.existsSync(tsconfigPath)) { - const configFile = ts.readConfigFile(tsconfigPath, ts.sys.readFile); - const configFileContent = ts.parseJsonConfigFileContent( - configFile.config, - ts.sys, - path.dirname(tsconfigPath), - ); - - return configFileContent; - } - - throw new Error(`tsconfig.json not found in the ${tsconfigPath}`); + const configFile = ts.readConfigFile(tsconfigPath, ts.sys.readFile); + const configFileContent = ts.parseJsonConfigFileContent( + configFile.config, + ts.sys, + path.dirname(tsconfigPath), + ); + + return configFileContent; } export const TEMP_FOLDER = '.rslib';