Skip to content
Merged
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
17 changes: 15 additions & 2 deletions packages/core/src/cli/commands.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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();
Expand Down
7 changes: 6 additions & 1 deletion tests/integration/cli/build-watch/build.test.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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();
});
});

Expand Down
4 changes: 2 additions & 2 deletions tests/integration/cli/mf/mf.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
});

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/plugins/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/server/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 18 additions & 1 deletion tests/scripts/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading