Skip to content

Commit

Permalink
chore: update error handle
Browse files Browse the repository at this point in the history
  • Loading branch information
Timeless0911 committed Aug 1, 2024
1 parent 7879b47 commit ef7766f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}),
Expand Down
18 changes: 13 additions & 5 deletions packages/plugin-dts/src/dts.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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
Expand Down Expand Up @@ -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();
}
Expand Down
5 changes: 3 additions & 2 deletions packages/plugin-dts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>[] = [];

Expand All @@ -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'));
}
});
}),
Expand Down
20 changes: 8 additions & 12 deletions packages/plugin-dts/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit ef7766f

Please sign in to comment.