Skip to content

Commit 140f955

Browse files
authored
feat(plugin-dts): dtsPromise only registered when firstCompile and print log with environment info (#63)
1 parent cba4e93 commit 140f955

File tree

7 files changed

+86
-55
lines changed

7 files changed

+86
-55
lines changed

e2e/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"devDependencies": {
1212
"@e2e/helper": "workspace:*",
1313
"@playwright/test": "1.43.1",
14-
"@rsbuild/core": "1.0.1-beta.8",
14+
"@rsbuild/core": "1.0.1-beta.10",
1515
"@rslib/core": "workspace:*",
1616
"@rslib/tsconfig": "workspace:*",
1717
"@types/fs-extra": "^11.0.4",

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"prebundle": "prebundle"
3939
},
4040
"dependencies": {
41-
"@rsbuild/core": "1.0.1-beta.8",
41+
"@rsbuild/core": "1.0.1-beta.10",
4242
"rsbuild-plugin-dts": "workspace:*"
4343
},
4444
"devDependencies": {

packages/plugin-dts/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@
2929
"build": "modern build",
3030
"dev": "modern build --watch"
3131
},
32+
"dependencies": {
33+
"picocolors": "1.0.1"
34+
},
3235
"devDependencies": {
3336
"@microsoft/api-extractor": "^7.47.4",
34-
"@rsbuild/core": "1.0.1-beta.8",
37+
"@rsbuild/core": "1.0.1-beta.10",
3538
"@rslib/tsconfig": "workspace:*",
3639
"typescript": "^5.5.3"
3740
},

packages/plugin-dts/src/dts.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { basename, dirname, join, relative } from 'node:path';
22
import { logger } from '@rsbuild/core';
3+
import color from 'picocolors';
34
import type { DtsGenOptions } from 'src';
45
import * as ts from 'typescript';
56
import { emitDts } from './tsc';
67
import { ensureTempDeclarationDir, loadTsconfig } from './utils';
78

89
export async function generateDts(data: DtsGenOptions): Promise<void> {
9-
logger.start('Generating DTS...');
10-
const { options: pluginOptions, cwd, isWatch } = data;
10+
const { options: pluginOptions, cwd, isWatch, name } = data;
11+
logger.start(`Generating DTS... ${color.gray(`(${name})`)}`);
1112
const { tsconfigPath, distPath, bundle, entryPath } = pluginOptions;
1213
const configPath = ts.findConfigFile(cwd, ts.sys.fileExists, tsconfigPath);
1314
if (!configPath) {
@@ -60,6 +61,7 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
6061

6162
emitDts(
6263
{
64+
name,
6365
cwd,
6466
configPath,
6567
rootDir,

packages/plugin-dts/src/index.ts

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export type pluginDtsOptions = {
1010
};
1111

1212
export type DtsGenOptions = {
13+
name: string;
1314
options: pluginDtsOptions;
1415
cwd: string;
1516
isWatch: boolean;
@@ -34,34 +35,49 @@ export const pluginDts = (options: pluginDtsOptions): RsbuildPlugin => ({
3435

3536
const dtsPromises: Promise<void>[] = [];
3637

37-
api.onBeforeBuild(({ isWatch }) => {
38-
const jsExtension = extname(__filename);
39-
const childProcess = fork(join(__dirname, `./dts${jsExtension}`), [], {
40-
stdio: 'inherit',
41-
});
38+
api.onBeforeEnvironmentCompile(
39+
({ isWatch, isFirstCompile, environment }) => {
40+
if (!isFirstCompile) {
41+
return;
42+
}
4243

43-
const dtsGenOptions = {
44-
options,
45-
cwd: api.context.rootPath,
46-
isWatch,
47-
};
44+
const jsExtension = extname(__filename);
45+
const childProcess = fork(join(__dirname, `./dts${jsExtension}`), [], {
46+
stdio: 'inherit',
47+
});
4848

49-
childProcess.send(dtsGenOptions);
49+
const dtsGenOptions = {
50+
name: environment.name,
51+
options,
52+
cwd: api.context.rootPath,
53+
isWatch,
54+
};
5055

51-
dtsPromises.push(
52-
new Promise((resolve, reject) => {
53-
childProcess.on('message', (message) => {
54-
if (message === 'success') {
55-
resolve();
56-
} else if (message === 'error') {
57-
reject(new Error('Error occurred in dts generation'));
58-
}
59-
});
60-
}),
61-
);
62-
});
56+
childProcess.send(dtsGenOptions);
57+
58+
dtsPromises.push(
59+
new Promise((resolve, reject) => {
60+
childProcess.on('message', (message) => {
61+
if (message === 'success') {
62+
resolve();
63+
} else if (message === 'error') {
64+
reject(
65+
new Error(
66+
`Error occurred in ${environment.name} dts generation`,
67+
),
68+
);
69+
}
70+
});
71+
}),
72+
);
73+
},
74+
);
75+
76+
api.onAfterBuild(async ({ isFirstCompile }) => {
77+
if (!isFirstCompile) {
78+
return;
79+
}
6380

64-
api.onAfterBuild(async () => {
6581
await Promise.all(dtsPromises);
6682
});
6783
},

packages/plugin-dts/src/tsc.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { logger } from '@rsbuild/core';
2+
import color from 'picocolors';
23
import * as ts from 'typescript';
34
import { getFileLoc, loadTsconfig } from './utils';
45

56
export type emitDtsOptions = {
7+
name: string;
68
cwd: string;
79
configPath: string;
810
rootDir: string;
@@ -18,7 +20,7 @@ export function emitDts(
1820
const getTimeCost = () => {
1921
return `${Math.floor(Date.now() - start)}ms`;
2022
};
21-
const { configPath, declarationDir } = options;
23+
const { configPath, declarationDir, name } = options;
2224
const { options: rawCompilerOptions, fileNames } = loadTsconfig(configPath);
2325

2426
const compilerOptions = {
@@ -56,7 +58,9 @@ export function emitDts(
5658
}
5759

5860
if (diagnosticMessages.length) {
59-
logger.error('Failed to emit declaration files.');
61+
logger.error(
62+
`Failed to emit declaration files. ${color.gray(`(${name})`)}`,
63+
);
6064

6165
for (const message of diagnosticMessages) {
6266
logger.error(message);
@@ -65,7 +69,9 @@ export function emitDts(
6569
throw new Error('DTS generation failed');
6670
}
6771

68-
logger.info(`DTS generation succeeded in ${getTimeCost()}`);
72+
logger.info(
73+
`DTS generation succeeded in ${getTimeCost()} ${color.gray(`(${name})`)}`,
74+
);
6975
} else {
7076
const createProgram = ts.createSemanticDiagnosticsBuilderProgram;
7177
const formatHost: ts.FormatDiagnosticsHost = {
@@ -92,10 +98,10 @@ export function emitDts(
9298
_options: ts.CompilerOptions,
9399
errorCount?: number,
94100
) => {
95-
const message = ts.flattenDiagnosticMessageText(
101+
const message = `${ts.flattenDiagnosticMessageText(
96102
diagnostic.messageText,
97103
formatHost.getNewLine(),
98-
);
104+
)} ${color.gray(`(${name})`)}`;
99105

100106
// 6031: File change detected. Starting incremental compilation...
101107
// 6032: Starting compilation in watch mode...

pnpm-lock.yaml

Lines changed: 25 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)