Skip to content

Commit 65ce9a4

Browse files
committed
chore: move to cli & support mf restart
1 parent 8ab1121 commit 65ce9a4

File tree

5 files changed

+41
-37
lines changed

5 files changed

+41
-37
lines changed

packages/core/src/cli/build.ts

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import { type RsbuildInstance, createRsbuild } from '@rsbuild/core';
22
import { composeRsbuildEnvironments, pruneEnvironments } from '../config';
3-
import { onBeforeRestartServer, watchFilesForRestart } from '../restart';
43
import type { RslibConfig } from '../types/config';
54
import type { BuildOptions } from './commands';
6-
import { loadRslibConfig } from './init';
5+
import { onBeforeRestartServer } from './restart';
76

87
export async function build(
98
config: RslibConfig,
10-
options: Pick<BuildOptions, 'lib' | 'watch'> & {
11-
configFilePath?: string;
12-
} = {},
9+
options: Pick<BuildOptions, 'lib' | 'watch'> = {},
1310
): Promise<RsbuildInstance> {
1411
const environments = await composeRsbuildEnvironments(config);
1512
const rsbuildInstance = await createRsbuild({
@@ -22,24 +19,8 @@ export async function build(
2219
watch: options.watch,
2320
});
2421

25-
if (options?.watch) {
26-
const files: string[] = [];
27-
28-
if (options.configFilePath) {
29-
files.push(options.configFilePath);
30-
}
31-
22+
if (options.watch) {
3223
onBeforeRestartServer(buildInstance.close);
33-
34-
watchFilesForRestart(files, async () => {
35-
const { content: rslibConfig, filePath: configFilePath } =
36-
await loadRslibConfig(options);
37-
38-
await build(rslibConfig, {
39-
configFilePath,
40-
...options,
41-
});
42-
});
4324
} else {
4425
await buildInstance.close();
4526
}

packages/core/src/cli/commands.ts

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { build } from './build';
55
import { loadRslibConfig } from './init';
66
import { inspect } from './inspect';
77
import { startMFDevServer } from './mf';
8+
import { watchFilesForRestart } from './restart';
89

910
export type CommonOptions = {
1011
root?: string;
@@ -62,13 +63,20 @@ export function runCli(): void {
6263
.description('build the library for production')
6364
.action(async (options: BuildOptions) => {
6465
try {
65-
const { content: rslibConfig, filePath: configFilePath } =
66-
await loadRslibConfig(options);
67-
await build(rslibConfig, {
68-
configFilePath,
69-
lib: options.lib,
70-
watch: options.watch,
71-
});
66+
const cliBuild = async () => {
67+
const { content: rslibConfig, filePath } =
68+
await loadRslibConfig(options);
69+
70+
await build(rslibConfig, options);
71+
72+
if (options?.watch) {
73+
watchFilesForRestart([filePath], async () => {
74+
await cliBuild();
75+
});
76+
}
77+
};
78+
79+
await cliBuild();
7280
} catch (err) {
7381
logger.error('Failed to build.');
7482
logger.error(err);
@@ -110,9 +118,18 @@ export function runCli(): void {
110118
.description('start Rsbuild dev server of Module Federation format')
111119
.action(async (options: CommonOptions) => {
112120
try {
113-
const { content: rslibConfig } = await loadRslibConfig(options);
114-
// TODO: support lib option in mf dev server
115-
await startMFDevServer(rslibConfig);
121+
const mfDev = async () => {
122+
const { content: rslibConfig, filePath } =
123+
await loadRslibConfig(options);
124+
// TODO: support lib option in mf dev server
125+
await startMFDevServer(rslibConfig);
126+
127+
watchFilesForRestart([filePath], async () => {
128+
await mfDev();
129+
});
130+
};
131+
132+
await mfDev();
116133
} catch (err) {
117134
logger.error('Failed to start mf dev.');
118135
logger.error(err);

packages/core/src/cli/mf.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { createRsbuild, mergeRsbuildConfig } from '@rsbuild/core';
22
import type { RsbuildConfig, RsbuildInstance } from '@rsbuild/core';
33
import { composeCreateRsbuildConfig } from '../config';
44
import type { RslibConfig } from '../types';
5+
import { onBeforeRestartServer } from './restart';
56

67
export async function startMFDevServer(
78
config: RslibConfig,
@@ -27,7 +28,9 @@ async function initMFRsbuild(
2728
const rsbuildInstance = await createRsbuild({
2829
rsbuildConfig: mfRsbuildConfig.config,
2930
});
30-
await rsbuildInstance.startDevServer();
31+
const devServer = await rsbuildInstance.startDevServer();
32+
33+
onBeforeRestartServer(devServer.server.close);
3134
return rsbuildInstance;
3235
}
3336

packages/core/src/restart.ts renamed to packages/core/src/cli/restart.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from 'node:path';
22
import chokidar from 'chokidar';
3-
import { color, debounce, isTTY } from './utils/helper';
4-
import { logger } from './utils/logger';
3+
import { color, debounce, isTTY } from '../utils/helper';
4+
import { logger } from '../utils/logger';
55

66
export async function watchFilesForRestart(
77
files: string[],

packages/plugin-dts/src/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,16 @@ export const pluginDts = (options: PluginDtsOptions = {}): RsbuildPlugin => ({
145145
order: 'post',
146146
});
147147

148-
api.onCloseBuild(() => {
148+
const killProcesses = () => {
149149
for (const childProcess of childProcesses) {
150150
if (!childProcess.killed) {
151151
childProcess.kill();
152152
}
153153
}
154154
childProcesses = [];
155-
});
155+
};
156+
157+
api.onCloseBuild(killProcesses);
158+
api.onCloseDevServer(killProcesses);
156159
},
157160
});

0 commit comments

Comments
 (0)