Skip to content

fix: should wait async function when generating dts in watch mode #951

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions packages/plugin-dts/src/apiExtractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type BundleOptions = {
dtsEntry: DtsEntry[];
tsconfigPath?: string;
bundledPackages?: string[];
resolveWatchCallback?: () => void;
};

export async function bundleDts(options: BundleOptions): Promise<void> {
Expand All @@ -40,6 +41,7 @@ export async function bundleDts(options: BundleOptions): Promise<void> {
dtsEntry,
tsconfigPath = 'tsconfig.json',
bundledPackages = [],
resolveWatchCallback,
} = options;
try {
await Promise.all(
Expand Down Expand Up @@ -102,6 +104,8 @@ export async function bundleDts(options: BundleOptions): Promise<void> {
);
}),
);

resolveWatchCallback?.();
} catch (e) {
throw new Error(`${logPrefixApiExtractor} ${e}`);
}
Expand Down
64 changes: 39 additions & 25 deletions packages/plugin-dts/src/dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,33 +200,44 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
.filter(Boolean) as Required<DtsEntry>[];
}

const bundleDtsIfNeeded = async () => {
if (bundle === true) {
const { bundleDts } = await import('./apiExtractor');
await bundleDts({
name,
const bundleDtsFiles = async (resolveWatchCallback?: () => void) => {
const { bundleDts } = await import('./apiExtractor');
await bundleDts({
name,
cwd,
distPath: dtsEmitPath,
dtsEntry: dtsEntries,
tsconfigPath,
dtsExtension,
banner,
footer,
bundledPackages: calcBundledPackages({
autoExternal,
cwd,
distPath: dtsEmitPath,
dtsEntry: dtsEntries,
tsconfigPath,
dtsExtension,
banner,
footer,
bundledPackages: calcBundledPackages({
autoExternal,
cwd,
userExternals,
}),
});
}
userExternals,
}),
resolveWatchCallback,
});
};

const onComplete = async (isSuccess: boolean) => {
if (isSuccess) {
await bundleDtsIfNeeded();
}
let resolveWatchCallback: () => void;

const bundleDtsFunc = async () => {
await bundleDtsFiles(resolveWatchCallback);
};

const resolveWatch = () => {
resolveWatchCallback();
};

const dtsCompletion = new Promise<void>((resolve) => {
if (isWatch) {
resolveWatchCallback = resolve;
} else {
resolve();
}
});

await emitDts(
{
name,
Expand All @@ -240,15 +251,18 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
banner,
footer,
},
onComplete,
bundleDtsFunc,
resolveWatch,
bundle,
isWatch,
build,
);

if (!isWatch) {
await bundleDtsIfNeeded();
if (!isWatch && bundle) {
await bundleDtsFiles();
}

await dtsCompletion;
}

process.on('message', async (data: DtsGenOptions) => {
Expand Down
107 changes: 62 additions & 45 deletions packages/plugin-dts/src/tsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { logger } from '@rsbuild/core';
import color from 'picocolors';
import ts from 'typescript';
import type { DtsRedirect } from './index';
import { getTimeCost, processDtsFiles } from './utils';
import { getTimeCost, processBundlelessDtsFiles } from './utils';

const logPrefixTsc = color.dim('[tsc]');

Expand Down Expand Up @@ -47,16 +47,17 @@ async function handleDiagnosticsAndProcessFiles(
diagnosticMessages.push(message);
}

await processDtsFiles(
bundle,
declarationDir,
dtsExtension,
redirect,
configPath,
rootDir,
banner,
footer,
);
if (!bundle) {
await processBundlelessDtsFiles(
declarationDir,
dtsExtension,
redirect,
configPath,
rootDir,
banner,
footer,
);
}

if (diagnosticMessages.length) {
for (const message of diagnosticMessages) {
Expand All @@ -71,7 +72,8 @@ async function handleDiagnosticsAndProcessFiles(

export async function emitDts(
options: EmitDtsOptions,
onComplete: (isSuccess: boolean) => void,
bundleDtsFunc: () => Promise<void>,
resolveWatch: () => void,
bundle = false,
isWatch = false,
build = false,
Expand Down Expand Up @@ -112,7 +114,7 @@ export async function emitDts(
);
};

const reportWatchStatusChanged: ts.WatchStatusReporter = async (
const reportWatchStatusChanged: ts.WatchStatusReporter = (
diagnostic: ts.Diagnostic,
_newLine: string,
_options: ts.CompilerOptions,
Expand All @@ -129,39 +131,53 @@ export async function emitDts(
logger.info(logPrefixTsc, message);
}

const hasError = errorCount && errorCount > 0;

// 6194: 0 errors or 2+ errors!
if (diagnostic.code === 6194) {
if (errorCount === 0 || !errorCount) {
if (!hasError) {
logger.info(logPrefixTsc, message);
onComplete(true);
} else {
logger.error(logPrefixTsc, message);
}
await processDtsFiles(
bundle,
declarationDir,
dtsExtension,
redirect,
configPath,
rootDir,
banner,
footer,
);

if (bundle) {
if (hasError) {
resolveWatch();
} else {
bundleDtsFunc();
}
} else {
processBundlelessDtsFiles(
declarationDir,
dtsExtension,
redirect,
configPath,
rootDir,
banner,
footer,
resolveWatch,
);
}
}

// 6193: 1 error
if (diagnostic.code === 6193) {
logger.error(logPrefixTsc, message);
await processDtsFiles(
bundle,
declarationDir,
dtsExtension,
redirect,
configPath,
rootDir,
banner,
footer,
);
if (bundle) {
bundleDtsFunc();
} else {
processBundlelessDtsFiles(
declarationDir,
dtsExtension,
redirect,
configPath,
rootDir,
banner,
footer,
resolveWatch,
);
}
}
};

Expand Down Expand Up @@ -306,16 +322,17 @@ export async function emitDts(

solutionBuilder.build();

await processDtsFiles(
bundle,
declarationDir,
dtsExtension,
redirect,
configPath,
rootDir,
banner,
footer,
);
if (!bundle) {
await processBundlelessDtsFiles(
declarationDir,
dtsExtension,
redirect,
configPath,
rootDir,
banner,
footer,
);
}

if (errorNumber > 0) {
throw new Error(
Expand Down
10 changes: 4 additions & 6 deletions packages/plugin-dts/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,20 +371,16 @@ export async function redirectDtsImports(
}
}

export async function processDtsFiles(
bundle: boolean,
export async function processBundlelessDtsFiles(
dir: string,
dtsExtension: string,
redirect: DtsRedirect,
tsconfigPath: string,
rootDir: string,
banner?: string,
footer?: string,
resolveWatch?: () => void,
): Promise<void> {
if (bundle) {
return;
}

let matchPath: MatchPath | undefined;

if (redirect.path || redirect.extension) {
Expand Down Expand Up @@ -428,6 +424,8 @@ export async function processDtsFiles(
}
}),
);

resolveWatch?.();
}

export function processSourceEntry(
Expand Down
Loading