From 6122b1e1245f29172cd8f01b77f1db233f74df15 Mon Sep 17 00:00:00 2001 From: Timeless0911 <1604889533@qq.com> Date: Wed, 20 Nov 2024 17:44:31 +0800 Subject: [PATCH] chore: update --- packages/core/src/cli/build.ts | 8 ++------ packages/core/src/cli/commands.ts | 14 +++++--------- packages/core/src/cli/init.ts | 6 +++--- packages/core/src/cli/inspect.ts | 11 ++--------- packages/core/src/cli/mf.ts | 13 ++----------- packages/core/src/config.ts | 24 +++++++++++++----------- 6 files changed, 27 insertions(+), 49 deletions(-) diff --git a/packages/core/src/cli/build.ts b/packages/core/src/cli/build.ts index 502c6e6a6..13386b8ba 100644 --- a/packages/core/src/cli/build.ts +++ b/packages/core/src/cli/build.ts @@ -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 = {}, + options: Pick = {}, ): Promise { - 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), diff --git a/packages/core/src/cli/commands.ts b/packages/core/src/cli/commands.ts index 3c4e15bb1..11714be33 100644 --- a/packages/core/src/cli/commands.ts +++ b/packages/core/src/cli/commands.ts @@ -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'; @@ -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, }); @@ -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, @@ -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); diff --git a/packages/core/src/cli/init.ts b/packages/core/src/cli/init.ts index 5a7ae5320..e77035b2d 100644 --- a/packages/core/src/cli/init.ts +++ b/packages/core/src/cli/init.ts @@ -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 { const cwd = process.cwd(); const root = options.root ? getAbsolutePath(cwd, options.root) : cwd; @@ -15,5 +15,5 @@ export async function getRslibConfig( envMode: options.envMode, }); - return { root, rslibConfig }; + return rslibConfig; } diff --git a/packages/core/src/cli/inspect.ts b/packages/core/src/cli/inspect.ts index 850caddb9..cfe67a6c7 100644 --- a/packages/core/src/cli/inspect.ts +++ b/packages/core/src/cli/inspect.ts @@ -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 = {}, ): Promise { - 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), diff --git a/packages/core/src/cli/mf.ts b/packages/core/src/cli/mf.ts index 3298372f4..352f89f01 100644 --- a/packages/core/src/cli/mf.ts +++ b/packages/core/src/cli/mf.ts @@ -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 = {}, ): Promise { - 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 { - const rsbuildConfigObject = await composeCreateRsbuildConfig( - rslibConfig, - root, - ); + const rsbuildConfigObject = await composeCreateRsbuildConfig(rslibConfig); const mfRsbuildConfig = rsbuildConfigObject.find( (config) => config.format === 'mf', ); diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index e36229494..eaaf14efa 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -53,6 +53,7 @@ import { calcLongestCommonPath, checkMFPlugin, color, + getAbsolutePath, isEmptyObject, isObject, nodeBuiltInModules, @@ -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; @@ -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); @@ -1191,7 +1197,6 @@ async function composeLibRsbuildConfig(config: LibConfig, root: string) { export async function composeCreateRsbuildConfig( rslibConfig: RslibConfig, - root: string, ): Promise { const constantRsbuildConfig = await createConstantRsbuildConfig(); const { lib: libConfigsArray, ...sharedRsbuildConfig } = rslibConfig; @@ -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. @@ -1266,12 +1271,9 @@ export async function composeCreateRsbuildConfig( export async function composeRsbuildEnvironments( rslibConfig: RslibConfig, - root: string, ): Promise> { - const rsbuildConfigWithLibInfo = await composeCreateRsbuildConfig( - rslibConfig, - root, - ); + const rsbuildConfigWithLibInfo = + await composeCreateRsbuildConfig(rslibConfig); // User provided ids should take precedence over generated ids. const usedIds = rsbuildConfigWithLibInfo