Skip to content

Commit 64c96b8

Browse files
authored
Merge branch 'main' into react_jsx_1204
2 parents 0125388 + 8ca5c7b commit 64c96b8

File tree

23 files changed

+439
-222
lines changed

23 files changed

+439
-222
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ test-results
2323
.nx/
2424
**/@mf-types
2525
**/@mf-types/**
26+
.env.local
27+
.env.*.local

examples/module-federation/mf-host/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"react-dom": "^18.3.1"
1313
},
1414
"devDependencies": {
15-
"@module-federation/rsbuild-plugin": "^0.8.0",
15+
"@module-federation/rsbuild-plugin": "^0.8.1",
1616
"@rsbuild/core": "~1.1.8",
1717
"@rsbuild/plugin-react": "^1.0.7",
1818
"@types/react": "^18.3.12",

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
"storybook": "storybook dev -p 6006"
1919
},
2020
"devDependencies": {
21-
"@module-federation/enhanced": "^0.8.0",
22-
"@module-federation/rsbuild-plugin": "^0.8.0",
23-
"@module-federation/storybook-addon": "^3.0.11",
21+
"@module-federation/enhanced": "^0.8.1",
22+
"@module-federation/rsbuild-plugin": "^0.8.1",
23+
"@module-federation/storybook-addon": "^3.0.12",
2424
"@rsbuild/plugin-react": "^1.0.7",
2525
"@rslib/core": "workspace:*",
2626
"@types/react": "^18.3.12",

examples/module-federation/mf-remote/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"react-dom": "^18.3.1"
1313
},
1414
"devDependencies": {
15-
"@module-federation/rsbuild-plugin": "^0.8.0",
15+
"@module-federation/rsbuild-plugin": "^0.8.1",
1616
"@rsbuild/core": "~1.1.8",
1717
"@rsbuild/plugin-react": "^1.0.7",
1818
"@types/react": "^18.3.12",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"fs-extra": "^11.2.0",
5151
"nano-staged": "^0.8.0",
5252
"nx": "^20.1.4",
53-
"prettier": "^3.4.1",
53+
"prettier": "^3.4.2",
5454
"prettier-plugin-packagejson": "^2.5.6",
5555
"simple-git-hooks": "^2.11.1",
5656
"typescript": "^5.6.3",

packages/core/src/cli/commands.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import type { RsbuildMode } from '@rsbuild/core';
22
import { type Command, program } from 'commander';
33
import { logger } from '../utils/logger';
44
import { build } from './build';
5-
import { loadRslibConfig } from './init';
5+
import { init } from './init';
66
import { inspect } from './inspect';
77
import { startMFDevServer } from './mf';
88
import { watchFilesForRestart } from './restart';
99

1010
export type CommonOptions = {
1111
root?: string;
1212
config?: string;
13+
envDir?: string;
1314
envMode?: string;
1415
lib?: string[];
1516
};
@@ -37,7 +38,8 @@ const applyCommonOptions = (command: Command) => {
3738
.option(
3839
'--env-mode <mode>',
3940
'specify the env mode to load the `.env.[mode]` file',
40-
);
41+
)
42+
.option('--env-dir <dir>', 'specify the directory to load `.env` files');
4143
};
4244

4345
const repeatableOption = (value: string, previous: string[]) => {
@@ -64,13 +66,12 @@ export function runCli(): void {
6466
.action(async (options: BuildOptions) => {
6567
try {
6668
const cliBuild = async () => {
67-
const { content: rslibConfig, filePath } =
68-
await loadRslibConfig(options);
69+
const { config, watchFiles } = await init(options);
6970

70-
await build(rslibConfig, options);
71+
await build(config, options);
7172

7273
if (options.watch) {
73-
watchFilesForRestart([filePath], async () => {
74+
watchFilesForRestart(watchFiles, async () => {
7475
await cliBuild();
7576
});
7677
}
@@ -100,8 +101,8 @@ export function runCli(): void {
100101
.action(async (options: InspectOptions) => {
101102
try {
102103
// TODO: inspect should output Rslib's config
103-
const { content: rslibConfig } = await loadRslibConfig(options);
104-
await inspect(rslibConfig, {
104+
const { config } = await init(options);
105+
await inspect(config, {
105106
lib: options.lib,
106107
mode: options.mode,
107108
output: options.output,
@@ -119,12 +120,11 @@ export function runCli(): void {
119120
.action(async (options: CommonOptions) => {
120121
try {
121122
const cliMfDev = async () => {
122-
const { content: rslibConfig, filePath } =
123-
await loadRslibConfig(options);
123+
const { config, watchFiles } = await init(options);
124124
// TODO: support lib option in mf dev server
125-
await startMFDevServer(rslibConfig);
125+
await startMFDevServer(config);
126126

127-
watchFilesForRestart([filePath], async () => {
127+
watchFilesForRestart(watchFiles, async () => {
128128
await cliMfDev();
129129
});
130130
};

packages/core/src/cli/init.ts

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,51 @@
1+
import path from 'node:path';
2+
import { loadEnv } from '@rsbuild/core';
13
import { loadConfig } from '../config';
24
import type { RslibConfig } from '../types';
35
import { getAbsolutePath } from '../utils/helper';
46
import type { CommonOptions } from './commands';
7+
import { onBeforeRestart } from './restart';
58

6-
export async function loadRslibConfig(options: CommonOptions): Promise<{
7-
content: RslibConfig;
8-
filePath: string;
9+
const getEnvDir = (cwd: string, envDir?: string) => {
10+
if (envDir) {
11+
return path.isAbsolute(envDir) ? envDir : path.resolve(cwd, envDir);
12+
}
13+
return cwd;
14+
};
15+
16+
export async function init(options: CommonOptions): Promise<{
17+
config: RslibConfig;
18+
configFilePath: string;
19+
watchFiles: string[];
920
}> {
1021
const cwd = process.cwd();
1122
const root = options.root ? getAbsolutePath(cwd, options.root) : cwd;
23+
const envs = loadEnv({
24+
cwd: getEnvDir(root, options.envDir),
25+
mode: options.envMode,
26+
});
27+
28+
onBeforeRestart(envs.cleanup);
1229

13-
return loadConfig({
30+
const { content: config, filePath: configFilePath } = await loadConfig({
1431
cwd: root,
1532
path: options.config,
1633
envMode: options.envMode,
1734
});
35+
36+
config.source ||= {};
37+
config.source.define = {
38+
...envs.publicVars,
39+
...config.source.define,
40+
};
41+
42+
if (options.root) {
43+
config.root = root;
44+
}
45+
46+
return {
47+
config,
48+
configFilePath,
49+
watchFiles: [configFilePath, ...envs.filePaths],
50+
};
1851
}

packages/core/src/config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,9 @@ const composeFormatConfig = ({
583583
type: 'umd',
584584
},
585585
},
586+
optimization: {
587+
nodeEnv: process.env.NODE_ENV,
588+
},
586589
},
587590
},
588591
};

packages/core/tests/__snapshots__/config.test.ts.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,9 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
640640
},
641641
},
642642
},
643+
"optimization": {
644+
"nodeEnv": "test",
645+
},
643646
"output": {
644647
"asyncChunks": false,
645648
"library": {

0 commit comments

Comments
 (0)