From 1a6bc956b1406fbfd95c53dbba8ce82e492485be Mon Sep 17 00:00:00 2001 From: fi3ework Date: Tue, 11 Nov 2025 20:14:39 +0800 Subject: [PATCH] feat: log a flag to mark first compile done --- packages/core/src/cli/commands.ts | 17 +++++++++++++++-- .../integration/cli/build-watch/build.test.ts | 7 ++++++- tests/integration/cli/mf/mf.test.ts | 4 ++-- tests/integration/plugins/index.test.ts | 2 +- tests/integration/server/index.test.ts | 2 +- tests/scripts/shared.ts | 19 ++++++++++++++++++- 6 files changed, 43 insertions(+), 8 deletions(-) diff --git a/packages/core/src/cli/commands.ts b/packages/core/src/cli/commands.ts index b3ff138c3..613bf1746 100644 --- a/packages/core/src/cli/commands.ts +++ b/packages/core/src/cli/commands.ts @@ -1,4 +1,4 @@ -import type { LogLevel, RsbuildMode } from '@rsbuild/core'; +import type { LogLevel, RsbuildMode, RsbuildPlugin } from '@rsbuild/core'; import cac, { type CAC } from 'cac'; import type { ConfigLoader } from '../config'; import type { Format, Syntax } from '../types/config'; @@ -139,12 +139,25 @@ export function runCli(): void { try { const cliBuild = async () => { const { config, watchFiles } = await initConfig(options); - await build(config, options); + if (options.watch) { + config.plugins = config.plugins || []; + config.plugins.push({ + name: 'rslib:on-after-build', + setup(api) { + api.onAfterBuild(({ isFirstCompile }) => { + if (isFirstCompile) { + logger.success('build complete, watching for changes...'); + } + }); + }, + } satisfies RsbuildPlugin); + watchFilesForRestart(watchFiles, async () => { await cliBuild(); }); } + await build(config, options); }; await cliBuild(); diff --git a/tests/integration/cli/build-watch/build.test.ts b/tests/integration/cli/build-watch/build.test.ts index 22b85870d..e6ac954ae 100644 --- a/tests/integration/cli/build-watch/build.test.ts +++ b/tests/integration/cli/build-watch/build.test.ts @@ -1,5 +1,6 @@ import { spawn } from 'node:child_process'; import path from 'node:path'; +import { stripVTControlCharacters as stripAnsi } from 'node:util'; import { describe, expect, onTestFinished, test } from '@rstest/core'; import fse from 'fs-extra'; import { @@ -53,7 +54,11 @@ export default defineConfig({ await expectFile(dist1EsmIndexFile); - process.kill(); + expect(stripAnsi(process.stdout())).toContain( + 'build complete, watching for changes', + ); + + process.child.kill(); }); }); diff --git a/tests/integration/cli/mf/mf.test.ts b/tests/integration/cli/mf/mf.test.ts index 91563a1f7..cd2e8d376 100644 --- a/tests/integration/cli/mf/mf.test.ts +++ b/tests/integration/cli/mf/mf.test.ts @@ -17,7 +17,7 @@ describe('mf-dev', () => { fse.removeSync(distFolder); const distPath = join(distFolder, 'index.js'); - const childProcess = runCli('mf-dev --lib mf0', { + const { child: childProcess } = runCli('mf-dev --lib mf0', { cwd: fixturePath, env: { ...process.env, @@ -47,7 +47,7 @@ describe('mf-dev', () => { const distPath1 = join(distFolder1, 'index.js'); const distPath2 = join(distFolder2, 'index.js'); - const childProcess = runCli('mf-dev --lib mf1 --lib mf2', { + const { child: childProcess } = runCli('mf-dev --lib mf1 --lib mf2', { cwd: fixturePath, }); diff --git a/tests/integration/plugins/index.test.ts b/tests/integration/plugins/index.test.ts index 6538742c1..e43975888 100644 --- a/tests/integration/plugins/index.test.ts +++ b/tests/integration/plugins/index.test.ts @@ -16,7 +16,7 @@ test('should run shared plugins only once', async () => { test('should merge plugins correctly', async () => { const fixturePath = join(__dirname, 'mf-dev'); - const childProcess = runCli('mf-dev', { + const { child: childProcess } = runCli('mf-dev', { cwd: fixturePath, }); diff --git a/tests/integration/server/index.test.ts b/tests/integration/server/index.test.ts index f4220c5d8..2a57a3f2c 100644 --- a/tests/integration/server/index.test.ts +++ b/tests/integration/server/index.test.ts @@ -23,7 +23,7 @@ describe('server config', async () => { fse.removeSync(distPath); - const childProcess = runCli('mf-dev', { + const { child: childProcess } = runCli('mf-dev', { cwd: fixturePath, env: { ...process.env, diff --git a/tests/scripts/shared.ts b/tests/scripts/shared.ts index 7c350fa4e..2e0b3c047 100644 --- a/tests/scripts/shared.ts +++ b/tests/scripts/shared.ts @@ -42,7 +42,24 @@ export function runCliSync(command: string, options?: ExecSyncOptions) { } export function runCli(command: string, options?: ExecOptions) { - return exec(`node ${rslibBinPath} ${command}`, options); + const child = exec(`node ${rslibBinPath} ${command}`, options); + + let stdout = ''; + let stderr = ''; + + child.stdout?.on('data', (data) => { + stdout += data.toString(); + }); + + child.stderr?.on('data', (data) => { + stderr += data.toString(); + }); + + return { + child, + stdout: () => stdout, + stderr: () => stderr, + }; } export function getCwdByExample(exampleName: string) {