Skip to content

Commit 08ddd5f

Browse files
committed
feat: support dev command to dev mf format
1 parent ed48cf4 commit 08ddd5f

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

examples/module-federation/mf-react-component/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ Dev MF module
1212
nx run @examples/mf-remote:dev
1313
```
1414

15-
2. Use Storybook to development component.
15+
2. Start MF dev mode.
16+
17+
```
18+
nx run @examples/mf-react-component:dev
19+
```
20+
21+
3. Use Storybook to development component.
1622

1723
```
1824
nx run @examples/mf-react-component:storybook

examples/module-federation/mf-react-component/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"types": "./dist/cjs/index.d.ts",
1414
"scripts": {
1515
"build": "rslib build",
16+
"dev": "rslib mf dev",
1617
"serve": "pnpm build & http-server -p 3001 ./dist/ --cors",
1718
"storybook": "storybook dev -p 6006"
1819
},

packages/core/src/cli/commands.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { RsbuildMode } from '@rsbuild/core';
22
import { type Command, program } from 'commander';
33
import { build } from '../build';
44
import { initRsbuild, loadConfig } from '../config';
5+
import { startMFDevServer } from '../mf';
56
import { logger } from '../utils/logger';
67

78
export type CommonOptions = {
@@ -36,8 +37,9 @@ export function runCli(): void {
3637

3738
const buildCommand = program.command('build');
3839
const inspectCommand = program.command('inspect');
40+
const mfDevCommand = program.command('mf dev');
3941

40-
[buildCommand, inspectCommand].forEach(applyCommonOptions);
42+
[buildCommand, inspectCommand, mfDevCommand].forEach(applyCommonOptions);
4143

4244
buildCommand
4345
.option('-w --watch', 'turn on watch mode, watch for changes and rebuild')
@@ -85,5 +87,20 @@ export function runCli(): void {
8587
}
8688
});
8789

90+
mfDevCommand
91+
.description(`dev format: 'mf' library`)
92+
.action(async (options: CommonOptions) => {
93+
try {
94+
const rslibConfig = await loadConfig({
95+
path: options.config,
96+
envMode: options.envMode,
97+
});
98+
await startMFDevServer(rslibConfig);
99+
} catch (err) {
100+
logger.error('Failed to start mf dev.');
101+
logger.error(err);
102+
process.exit(1);
103+
}
104+
});
88105
program.parse();
89106
}

packages/core/src/mf.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { createRsbuild, mergeRsbuildConfig } from '@rsbuild/core';
2+
import { composeCreateRsbuildConfig } from './config';
3+
4+
import type { RsbuildConfig, RsbuildInstance } from '@rsbuild/core';
5+
import type { RslibConfig } from './types';
6+
7+
export async function startMFDevServer(
8+
config: RslibConfig,
9+
): Promise<RsbuildInstance | undefined> {
10+
const rsbuildInstance = await initMFRsbuild(config);
11+
return rsbuildInstance;
12+
}
13+
14+
async function initMFRsbuild(
15+
rslibConfig: RslibConfig,
16+
): Promise<RsbuildInstance | undefined> {
17+
const rsbuildConfigObject = await composeCreateRsbuildConfig(rslibConfig);
18+
const mfRsbuildConfig = rsbuildConfigObject.find(
19+
(config) => config.format === 'mf',
20+
);
21+
22+
if (!mfRsbuildConfig) {
23+
// no mf format, return.
24+
return;
25+
}
26+
27+
mfRsbuildConfig.config = changeEnvToDev(mfRsbuildConfig.config);
28+
const rsbuildInstance = await createRsbuild({
29+
rsbuildConfig: mfRsbuildConfig.config,
30+
});
31+
await rsbuildInstance.startDevServer();
32+
return rsbuildInstance;
33+
}
34+
35+
function changeEnvToDev(rsbuildConfig: RsbuildConfig) {
36+
return mergeRsbuildConfig(rsbuildConfig, {
37+
mode: 'development',
38+
dev: {
39+
writeToDisk: true,
40+
},
41+
tools: {
42+
rspack: {
43+
optimization: {
44+
nodeEnv: 'development',
45+
},
46+
},
47+
},
48+
});
49+
}

0 commit comments

Comments
 (0)