Skip to content

Commit 06563fd

Browse files
authored
feat: log a flag to mark first compile done in watch mode (#1330)
1 parent 95ae7d7 commit 06563fd

File tree

6 files changed

+43
-8
lines changed

6 files changed

+43
-8
lines changed

packages/core/src/cli/commands.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { LogLevel, RsbuildMode } from '@rsbuild/core';
1+
import type { LogLevel, RsbuildMode, RsbuildPlugin } from '@rsbuild/core';
22
import cac, { type CAC } from 'cac';
33
import type { ConfigLoader } from '../config';
44
import type { Format, Syntax } from '../types/config';
@@ -139,12 +139,25 @@ export function runCli(): void {
139139
try {
140140
const cliBuild = async () => {
141141
const { config, watchFiles } = await initConfig(options);
142-
await build(config, options);
142+
143143
if (options.watch) {
144+
config.plugins = config.plugins || [];
145+
config.plugins.push({
146+
name: 'rslib:on-after-build',
147+
setup(api) {
148+
api.onAfterBuild(({ isFirstCompile }) => {
149+
if (isFirstCompile) {
150+
logger.success('build complete, watching for changes...');
151+
}
152+
});
153+
},
154+
} satisfies RsbuildPlugin);
155+
144156
watchFilesForRestart(watchFiles, async () => {
145157
await cliBuild();
146158
});
147159
}
160+
await build(config, options);
148161
};
149162

150163
await cliBuild();

tests/integration/cli/build-watch/build.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { spawn } from 'node:child_process';
22
import path from 'node:path';
3+
import { stripVTControlCharacters as stripAnsi } from 'node:util';
34
import { describe, expect, onTestFinished, test } from '@rstest/core';
45
import fse from 'fs-extra';
56
import {
@@ -53,7 +54,11 @@ export default defineConfig({
5354

5455
await expectFile(dist1EsmIndexFile);
5556

56-
process.kill();
57+
expect(stripAnsi(process.stdout())).toContain(
58+
'build complete, watching for changes',
59+
);
60+
61+
process.child.kill();
5762
});
5863
});
5964

tests/integration/cli/mf/mf.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('mf-dev', () => {
1717
fse.removeSync(distFolder);
1818
const distPath = join(distFolder, 'index.js');
1919

20-
const childProcess = runCli('mf-dev --lib mf0', {
20+
const { child: childProcess } = runCli('mf-dev --lib mf0', {
2121
cwd: fixturePath,
2222
env: {
2323
...process.env,
@@ -47,7 +47,7 @@ describe('mf-dev', () => {
4747
const distPath1 = join(distFolder1, 'index.js');
4848
const distPath2 = join(distFolder2, 'index.js');
4949

50-
const childProcess = runCli('mf-dev --lib mf1 --lib mf2', {
50+
const { child: childProcess } = runCli('mf-dev --lib mf1 --lib mf2', {
5151
cwd: fixturePath,
5252
});
5353

tests/integration/plugins/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ test('should run shared plugins only once', async () => {
1616

1717
test('should merge plugins correctly', async () => {
1818
const fixturePath = join(__dirname, 'mf-dev');
19-
const childProcess = runCli('mf-dev', {
19+
const { child: childProcess } = runCli('mf-dev', {
2020
cwd: fixturePath,
2121
});
2222

tests/integration/server/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('server config', async () => {
2323

2424
fse.removeSync(distPath);
2525

26-
const childProcess = runCli('mf-dev', {
26+
const { child: childProcess } = runCli('mf-dev', {
2727
cwd: fixturePath,
2828
env: {
2929
...process.env,

tests/scripts/shared.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,24 @@ export function runCliSync(command: string, options?: ExecSyncOptions) {
4242
}
4343

4444
export function runCli(command: string, options?: ExecOptions) {
45-
return exec(`node ${rslibBinPath} ${command}`, options);
45+
const child = exec(`node ${rslibBinPath} ${command}`, options);
46+
47+
let stdout = '';
48+
let stderr = '';
49+
50+
child.stdout?.on('data', (data) => {
51+
stdout += data.toString();
52+
});
53+
54+
child.stderr?.on('data', (data) => {
55+
stderr += data.toString();
56+
});
57+
58+
return {
59+
child,
60+
stdout: () => stdout,
61+
stderr: () => stderr,
62+
};
4663
}
4764

4865
export function getCwdByExample(exampleName: string) {

0 commit comments

Comments
 (0)