Skip to content

Commit

Permalink
chore: update
Browse files Browse the repository at this point in the history
  • Loading branch information
Timeless0911 committed Nov 20, 2024
1 parent a8747f1 commit 6122b1e
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 49 deletions.
8 changes: 2 additions & 6 deletions packages/core/src/cli/build.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import { type RsbuildInstance, createRsbuild } from '@rsbuild/core';
import { composeRsbuildEnvironments, pruneEnvironments } from '../config';
import type { RslibConfig } from '../types/config';
import { getAbsolutePath } from '../utils/helper';
import type { BuildOptions } from './commands';

export async function build(
config: RslibConfig,
options: Pick<BuildOptions, 'root' | 'lib' | 'watch'> = {},
options: Pick<BuildOptions, 'lib' | 'watch'> = {},
): Promise<RsbuildInstance> {
const cwd = process.cwd();
const root = options.root ? getAbsolutePath(cwd, options.root) : cwd;

const environments = await composeRsbuildEnvironments(config, root);
const environments = await composeRsbuildEnvironments(config);
const rsbuildInstance = await createRsbuild({
rsbuildConfig: {
environments: pruneEnvironments(environments, options.lib),
Expand Down
14 changes: 5 additions & 9 deletions packages/core/src/cli/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { RsbuildMode } from '@rsbuild/core';
import { type Command, program } from 'commander';
import { logger } from '../utils/logger';
import { build } from './build';
import { getRslibConfig } from './init';
import { loadRslibConfig } from './init';
import { inspect } from './inspect';
import { startMFDevServer } from './mf';

Expand Down Expand Up @@ -62,9 +62,8 @@ export function runCli(): void {
.description('build the library for production')
.action(async (options: BuildOptions) => {
try {
const { root, rslibConfig } = await getRslibConfig(options);
const rslibConfig = await loadRslibConfig(options);
await build(rslibConfig, {
root,
lib: options.lib,
watch: options.watch,
});
Expand All @@ -91,9 +90,8 @@ export function runCli(): void {
.action(async (options: InspectOptions) => {
try {
// TODO: inspect should output Rslib's config
const { root, rslibConfig } = await getRslibConfig(options);
const rslibConfig = await loadRslibConfig(options);
await inspect(rslibConfig, {
root,
lib: options.lib,
mode: options.mode,
output: options.output,
Expand All @@ -110,11 +108,9 @@ export function runCli(): void {
.description('start Rsbuild dev server of Module Federation format')
.action(async (options: CommonOptions) => {
try {
const { root, rslibConfig } = await getRslibConfig(options);
const rslibConfig = await loadRslibConfig(options);
// TODO: support lib option in mf dev server
await startMFDevServer(rslibConfig, {
root,
});
await startMFDevServer(rslibConfig);
} catch (err) {
logger.error('Failed to start mf dev.');
logger.error(err);
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/cli/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import type { RslibConfig } from '../types';
import { getAbsolutePath } from '../utils/helper';
import type { CommonOptions } from './commands';

export async function getRslibConfig(
export async function loadRslibConfig(
options: CommonOptions,
): Promise<{ root: string; rslibConfig: RslibConfig }> {
): Promise<RslibConfig> {
const cwd = process.cwd();
const root = options.root ? getAbsolutePath(cwd, options.root) : cwd;

Expand All @@ -15,5 +15,5 @@ export async function getRslibConfig(
envMode: options.envMode,
});

return { root, rslibConfig };
return rslibConfig;
}
11 changes: 2 additions & 9 deletions packages/core/src/cli/inspect.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import { type RsbuildInstance, createRsbuild } from '@rsbuild/core';
import { composeRsbuildEnvironments, pruneEnvironments } from '../config';
import type { RslibConfig } from '../types/config';
import { getAbsolutePath } from '../utils/helper';
import type { InspectOptions } from './commands';

export async function inspect(
config: RslibConfig,
options: Pick<
InspectOptions,
'root' | 'lib' | 'mode' | 'output' | 'verbose'
> = {},
options: Pick<InspectOptions, 'lib' | 'mode' | 'output' | 'verbose'> = {},
): Promise<RsbuildInstance> {
const cwd = process.cwd();
const root = options.root ? getAbsolutePath(cwd, options.root) : cwd;

const environments = await composeRsbuildEnvironments(config, root);
const environments = await composeRsbuildEnvironments(config);
const rsbuildInstance = await createRsbuild({
rsbuildConfig: {
environments: pruneEnvironments(environments, options.lib),
Expand Down
13 changes: 2 additions & 11 deletions packages/core/src/cli/mf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,18 @@ import { createRsbuild, mergeRsbuildConfig } from '@rsbuild/core';
import type { RsbuildConfig, RsbuildInstance } from '@rsbuild/core';
import { composeCreateRsbuildConfig } from '../config';
import type { RslibConfig } from '../types';
import { getAbsolutePath } from '../utils/helper';
import type { CommonOptions } from './commands';

export async function startMFDevServer(
config: RslibConfig,
options: Pick<CommonOptions, 'root'> = {},
): Promise<RsbuildInstance | undefined> {
const cwd = process.cwd();
const root = options.root ? getAbsolutePath(cwd, options.root) : cwd;
const rsbuildInstance = await initMFRsbuild(config, root);
const rsbuildInstance = await initMFRsbuild(config);
return rsbuildInstance;
}

async function initMFRsbuild(
rslibConfig: RslibConfig,
root: string,
): Promise<RsbuildInstance | undefined> {
const rsbuildConfigObject = await composeCreateRsbuildConfig(
rslibConfig,
root,
);
const rsbuildConfigObject = await composeCreateRsbuildConfig(rslibConfig);
const mfRsbuildConfig = rsbuildConfigObject.find(
(config) => config.format === 'mf',
);
Expand Down
24 changes: 13 additions & 11 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import {
calcLongestCommonPath,
checkMFPlugin,
color,
getAbsolutePath,
isEmptyObject,
isObject,
nodeBuiltInModules,
Expand Down Expand Up @@ -1084,11 +1085,16 @@ const composeExternalHelpersConfig = (
return defaultConfig;
};

async function composeLibRsbuildConfig(config: LibConfig, root: string) {
async function composeLibRsbuildConfig(config: LibConfig) {
checkMFPlugin(config);
const pkgJson = readPackageJson(root);

// Get the absolute path of the root directory to align with Rsbuild's default behavior
const rootPath = config.root
? getAbsolutePath(process.cwd(), config.root)
: process.cwd();
const pkgJson = readPackageJson(rootPath);
const { compilerOptions } = await loadTsconfig(
root,
rootPath,
config.source?.tsconfigPath,
);
const cssModulesAuto = config.output?.cssModules?.auto ?? true;
Expand Down Expand Up @@ -1147,7 +1153,7 @@ async function composeLibRsbuildConfig(config: LibConfig, root: string) {
const { entryConfig, lcp } = await composeEntryConfig(
config.source?.entry,
config.bundle,
root,
rootPath,
cssModulesAuto,
);
const cssConfig = composeCssConfig(lcp, config.bundle);
Expand Down Expand Up @@ -1191,7 +1197,6 @@ async function composeLibRsbuildConfig(config: LibConfig, root: string) {

export async function composeCreateRsbuildConfig(
rslibConfig: RslibConfig,
root: string,
): Promise<RsbuildConfigWithLibInfo[]> {
const constantRsbuildConfig = await createConstantRsbuildConfig();
const { lib: libConfigsArray, ...sharedRsbuildConfig } = rslibConfig;
Expand All @@ -1210,7 +1215,7 @@ export async function composeCreateRsbuildConfig(

// Merge the configuration of each environment based on the shared Rsbuild
// configuration and Lib configuration in the settings.
const libRsbuildConfig = await composeLibRsbuildConfig(userConfig, root);
const libRsbuildConfig = await composeLibRsbuildConfig(userConfig);

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

export async function composeRsbuildEnvironments(
rslibConfig: RslibConfig,
root: string,
): Promise<Record<string, EnvironmentConfig>> {
const rsbuildConfigWithLibInfo = await composeCreateRsbuildConfig(
rslibConfig,
root,
);
const rsbuildConfigWithLibInfo =
await composeCreateRsbuildConfig(rslibConfig);

// User provided ids should take precedence over generated ids.
const usedIds = rsbuildConfigWithLibInfo
Expand Down

0 comments on commit 6122b1e

Please sign in to comment.