Skip to content

Commit 6b605d2

Browse files
committed
feat: add --root cli option and make basic js api work
1 parent 90339f9 commit 6b605d2

File tree

17 files changed

+225
-75
lines changed

17 files changed

+225
-75
lines changed

packages/core/src/build.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

packages/core/src/cli/build.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { type RsbuildInstance, createRsbuild } from '@rsbuild/core';
2+
import { composeRsbuildEnvironments, pruneEnvironments } from '../config';
3+
import type { RslibConfig } from '../types/config';
4+
import { getAbsolutePath } from '../utils/helper';
5+
import type { BuildOptions } from './commands';
6+
7+
export async function build(
8+
config: RslibConfig,
9+
options: Pick<BuildOptions, 'root' | 'lib' | 'watch'> = {},
10+
): Promise<RsbuildInstance> {
11+
const cwd = process.cwd();
12+
options.root = options.root ? getAbsolutePath(cwd, options.root) : cwd;
13+
14+
const environments = await composeRsbuildEnvironments(config, options.root);
15+
const rsbuildInstance = await createRsbuild({
16+
rsbuildConfig: {
17+
environments: pruneEnvironments(environments, options.lib),
18+
},
19+
});
20+
21+
await rsbuildInstance.build({
22+
watch: options.watch,
23+
});
24+
25+
return rsbuildInstance;
26+
}

packages/core/src/cli/commands.ts

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
import { type RsbuildMode, createRsbuild } from '@rsbuild/core';
1+
import type { RsbuildMode } from '@rsbuild/core';
22
import { type Command, program } from 'commander';
3-
import { build } from '../build';
4-
import {
5-
composeRsbuildEnvironments,
6-
loadConfig,
7-
pruneEnvironments,
8-
} from '../config';
9-
import { startMFDevServer } from '../mf';
103
import { logger } from '../utils/logger';
4+
import { build } from './build';
5+
import { getRslibConfig } from './init';
6+
import { inspect } from './inspect';
7+
import { startMFDevServer } from './mf';
118

129
export type CommonOptions = {
10+
root?: string;
1311
config?: string;
1412
envMode?: string;
1513
lib?: string[];
@@ -20,8 +18,8 @@ export type BuildOptions = CommonOptions & {
2018
};
2119

2220
export type InspectOptions = CommonOptions & {
23-
mode: RsbuildMode;
24-
output: string;
21+
mode?: RsbuildMode;
22+
output?: string;
2523
verbose?: boolean;
2624
};
2725

@@ -31,6 +29,10 @@ const applyCommonOptions = (command: Command) => {
3129
'-c --config <config>',
3230
'specify the configuration file, can be a relative or absolute path',
3331
)
32+
.option(
33+
'-r --root <root>',
34+
'specify the project root directory, can be an absolute path or a path relative to cwd',
35+
)
3436
.option(
3537
'--env-mode <mode>',
3638
'specify the env mode to load the `.env.[mode]` file',
@@ -60,11 +62,12 @@ export function runCli(): void {
6062
.description('build the library for production')
6163
.action(async (options: BuildOptions) => {
6264
try {
63-
const rslibConfig = await loadConfig({
64-
path: options.config,
65-
envMode: options.envMode,
65+
const { root, rslibConfig } = await getRslibConfig(options);
66+
await build(rslibConfig, {
67+
root,
68+
lib: options.lib,
69+
watch: options.watch,
6670
});
67-
await build(rslibConfig, options);
6871
} catch (err) {
6972
logger.error('Failed to build.');
7073
logger.error(err);
@@ -88,21 +91,13 @@ export function runCli(): void {
8891
.action(async (options: InspectOptions) => {
8992
try {
9093
// TODO: inspect should output Rslib's config
91-
const rslibConfig = await loadConfig({
92-
path: options.config,
93-
envMode: options.envMode,
94-
});
95-
const environments = await composeRsbuildEnvironments(rslibConfig);
96-
const rsbuildInstance = await createRsbuild({
97-
rsbuildConfig: {
98-
environments: pruneEnvironments(environments, options.lib),
99-
},
100-
});
101-
await rsbuildInstance.inspectConfig({
94+
const { root, rslibConfig } = await getRslibConfig(options);
95+
await inspect(rslibConfig, {
96+
root,
97+
lib: options.lib,
10298
mode: options.mode,
99+
output: options.output,
103100
verbose: options.verbose,
104-
outputPath: options.output,
105-
writeToDisk: true,
106101
});
107102
} catch (err) {
108103
logger.error('Failed to inspect config.');
@@ -115,11 +110,11 @@ export function runCli(): void {
115110
.description('start Rsbuild dev server of Module Federation format')
116111
.action(async (options: CommonOptions) => {
117112
try {
118-
const rslibConfig = await loadConfig({
119-
path: options.config,
120-
envMode: options.envMode,
113+
const { root, rslibConfig } = await getRslibConfig(options);
114+
// TODO: support lib option in mf dev server
115+
await startMFDevServer(rslibConfig, {
116+
root,
121117
});
122-
await startMFDevServer(rslibConfig);
123118
} catch (err) {
124119
logger.error('Failed to start mf dev.');
125120
logger.error(err);

packages/core/src/cli/init.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { loadConfig } from '../config';
2+
import type { RslibConfig } from '../types';
3+
import { getAbsolutePath } from '../utils/helper';
4+
import type { CommonOptions } from './commands';
5+
6+
export async function getRslibConfig(
7+
options: CommonOptions,
8+
): Promise<{ root: string; rslibConfig: RslibConfig }> {
9+
const cwd = process.cwd();
10+
const root = options.root ? getAbsolutePath(cwd, options.root) : cwd;
11+
12+
const rslibConfig = await loadConfig({
13+
cwd: root,
14+
path: options.config,
15+
envMode: options.envMode,
16+
});
17+
18+
return { root, rslibConfig };
19+
}

packages/core/src/cli/inspect.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { type RsbuildInstance, createRsbuild } from '@rsbuild/core';
2+
import { composeRsbuildEnvironments, pruneEnvironments } from '../config';
3+
import type { RslibConfig } from '../types/config';
4+
import { getAbsolutePath } from '../utils/helper';
5+
import type { InspectOptions } from './commands';
6+
7+
export async function inspect(
8+
config: RslibConfig,
9+
options: Pick<
10+
InspectOptions,
11+
'root' | 'lib' | 'mode' | 'output' | 'verbose'
12+
> = {},
13+
): Promise<RsbuildInstance> {
14+
const cwd = process.cwd();
15+
options.root = options.root ? getAbsolutePath(cwd, options.root) : cwd;
16+
17+
const environments = await composeRsbuildEnvironments(config, options.root);
18+
const rsbuildInstance = await createRsbuild({
19+
rsbuildConfig: {
20+
environments: pruneEnvironments(environments, options.lib),
21+
},
22+
});
23+
24+
await rsbuildInstance.inspectConfig({
25+
mode: options.mode,
26+
verbose: options.verbose,
27+
outputPath: options.output,
28+
writeToDisk: true,
29+
});
30+
31+
return rsbuildInstance;
32+
}

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
import { createRsbuild, mergeRsbuildConfig } from '@rsbuild/core';
2-
import { composeCreateRsbuildConfig } from './config';
3-
42
import type { RsbuildConfig, RsbuildInstance } from '@rsbuild/core';
5-
import type { RslibConfig } from './types';
3+
import { composeCreateRsbuildConfig } from '../config';
4+
import type { RslibConfig } from '../types';
5+
import { getAbsolutePath } from '../utils/helper';
6+
import type { CommonOptions } from './commands';
67

78
export async function startMFDevServer(
89
config: RslibConfig,
10+
options: Pick<CommonOptions, 'root'> = {},
911
): Promise<RsbuildInstance | undefined> {
10-
const rsbuildInstance = await initMFRsbuild(config);
12+
const cwd = process.cwd();
13+
options.root = options.root ? getAbsolutePath(cwd, options.root) : cwd;
14+
const rsbuildInstance = await initMFRsbuild(config, options.root);
1115
return rsbuildInstance;
1216
}
1317

1418
async function initMFRsbuild(
1519
rslibConfig: RslibConfig,
20+
root: string,
1621
): Promise<RsbuildInstance | undefined> {
17-
const rsbuildConfigObject = await composeCreateRsbuildConfig(rslibConfig);
22+
const rsbuildConfigObject = await composeCreateRsbuildConfig(
23+
rslibConfig,
24+
root,
25+
);
1826
const mfRsbuildConfig = rsbuildConfigObject.find(
1927
(config) => config.format === 'mf',
2028
);

packages/core/src/config.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,12 +1084,11 @@ const composeExternalHelpersConfig = (
10841084
return defaultConfig;
10851085
};
10861086

1087-
async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
1087+
async function composeLibRsbuildConfig(config: LibConfig, root: string) {
10881088
checkMFPlugin(config);
1089-
const rootPath = dirname(configPath);
1090-
const pkgJson = readPackageJson(rootPath);
1089+
const pkgJson = readPackageJson(root);
10911090
const { compilerOptions } = await loadTsconfig(
1092-
rootPath,
1091+
root,
10931092
config.source?.tsconfigPath,
10941093
);
10951094
const cssModulesAuto = config.output?.cssModules?.auto ?? true;
@@ -1148,7 +1147,7 @@ async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
11481147
const { entryConfig, lcp } = await composeEntryConfig(
11491148
config.source?.entry,
11501149
config.bundle,
1151-
dirname(configPath),
1150+
root,
11521151
cssModulesAuto,
11531152
);
11541153
const cssConfig = composeCssConfig(lcp, config.bundle);
@@ -1192,10 +1191,9 @@ async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
11921191

11931192
export async function composeCreateRsbuildConfig(
11941193
rslibConfig: RslibConfig,
1195-
path?: string,
1194+
root: string,
11961195
): Promise<RsbuildConfigWithLibInfo[]> {
11971196
const constantRsbuildConfig = await createConstantRsbuildConfig();
1198-
const configPath = path ?? rslibConfig._privateMeta?.configFilePath!;
11991197
const { lib: libConfigsArray, ...sharedRsbuildConfig } = rslibConfig;
12001198

12011199
if (!libConfigsArray) {
@@ -1212,10 +1210,7 @@ export async function composeCreateRsbuildConfig(
12121210

12131211
// Merge the configuration of each environment based on the shared Rsbuild
12141212
// configuration and Lib configuration in the settings.
1215-
const libRsbuildConfig = await composeLibRsbuildConfig(
1216-
userConfig,
1217-
configPath,
1218-
);
1213+
const libRsbuildConfig = await composeLibRsbuildConfig(userConfig, root);
12191214

12201215
// Reset certain fields because they will be completely overridden by the upcoming merge.
12211216
// We don't want to retain them in the final configuration.
@@ -1271,11 +1266,11 @@ export async function composeCreateRsbuildConfig(
12711266

12721267
export async function composeRsbuildEnvironments(
12731268
rslibConfig: RslibConfig,
1274-
path?: string,
1269+
root: string,
12751270
): Promise<Record<string, EnvironmentConfig>> {
12761271
const rsbuildConfigWithLibInfo = await composeCreateRsbuildConfig(
12771272
rslibConfig,
1278-
path,
1273+
root,
12791274
);
12801275

12811276
// User provided ids should take precedence over generated ids.

packages/core/src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
export { prepareCli } from './cli/prepare';
22
export { runCli } from './cli/commands';
3+
export { build } from './cli/build';
4+
export { inspect } from './cli/inspect';
5+
export { startMFDevServer } from './cli/mf';
36
export {
47
defineConfig,
58
loadConfig,
69
composeCreateRsbuildConfig as unstable_composeCreateRsbuildConfig,
710
} from './config';
8-
export { build } from './build';
911
export { logger } from './utils/logger';
1012
export type * from './types';
1113

packages/core/src/utils/helper.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fs from 'node:fs';
22
import fsP from 'node:fs/promises';
3-
import path from 'node:path';
3+
import path, { isAbsolute, join } from 'node:path';
44
import type { RsbuildPlugins } from '@rsbuild/core';
55
import color from 'picocolors';
66

@@ -109,6 +109,10 @@ export async function calcLongestCommonPath(
109109
return lca;
110110
}
111111

112+
export function getAbsolutePath(base: string, filepath: string): string {
113+
return isAbsolute(filepath) ? filepath : join(base, filepath);
114+
}
115+
112116
export const readPackageJson = (rootPath: string): undefined | PkgJson => {
113117
const pkgJsonPath = path.join(rootPath, './package.json');
114118

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
7272
"pnpapi",
7373
],
7474
"filename": {
75-
"js": "[name].js",
75+
"js": "[name].mjs",
7676
},
7777
"filenameHash": false,
7878
"minify": {

0 commit comments

Comments
 (0)