From 8bb64ab07e80d7e2fe669147324fed38d82f1baf Mon Sep 17 00:00:00 2001 From: erkelost <1256029807@qq.com> Date: Wed, 6 Nov 2024 17:14:28 +0800 Subject: [PATCH 1/3] refactor: redesign resolved config methods --- packages/core/src/config/index.ts | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/packages/core/src/config/index.ts b/packages/core/src/config/index.ts index ce1ae2850..1c50add74 100644 --- a/packages/core/src/config/index.ts +++ b/packages/core/src/config/index.ts @@ -147,8 +147,10 @@ export async function resolveConfig( rawConfig = mergeConfig(rawConfig, loadedUserConfig.config); } - const { jsPlugins, vitePlugins, rustPlugins, vitePluginAdapters } = - await resolvePlugins(rawConfig, compileMode); + const { jsPlugins, rustPlugins, vitePluginAdapters } = await resolvePlugins( + rawConfig, + compileMode + ); const sortFarmJsPlugins = getSortedPlugins([ ...jsPlugins, @@ -203,18 +205,20 @@ async function handleLazyCompilation( config: ResolvedUserConfig, command: keyof typeof COMMANDS ) { + console.log(command); + const commandHandlers = { - [COMMANDS.START]: async (cfg: ResolvedUserConfig) => { + [COMMANDS.START]: async (config: ResolvedUserConfig) => { if ( - cfg.compilation.lazyCompilation && - typeof cfg.server?.host === 'string' + config.compilation.lazyCompilation && + typeof config.server?.host === 'string' ) { - await setLazyCompilationDefine(cfg); + await setLazyCompilationDefine(config); } }, - [COMMANDS.WATCH]: async (cfg: ResolvedUserConfig) => { - if (cfg.compilation?.lazyCompilation) { - await setLazyCompilationDefine(cfg); + [COMMANDS.WATCH]: async (config: ResolvedUserConfig) => { + if (config.compilation?.lazyCompilation) { + await setLazyCompilationDefine(config); } } }; From 266d8b92954066823f4b4fc17600aef0a3f96ead Mon Sep 17 00:00:00 2001 From: erkelost <1256029807@qq.com> Date: Wed, 6 Nov 2024 18:01:18 +0800 Subject: [PATCH 2/3] refactor: redesign resolved config methods --- examples/refactor-react/farm.config.ts | 2 +- packages/core/src/config/index.ts | 20 ++++++++++++++------ packages/core/src/config/types.ts | 1 + 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/examples/refactor-react/farm.config.ts b/examples/refactor-react/farm.config.ts index ead3b0f19..5df878f11 100644 --- a/examples/refactor-react/farm.config.ts +++ b/examples/refactor-react/farm.config.ts @@ -3,7 +3,7 @@ import { defineConfig } from "@farmfe/core"; import react from "@farmfe/plugin-react"; import path from "path"; -console.log(__dirname); +// console.log(__dirname); export default defineConfig({ plugins: [ diff --git a/packages/core/src/config/index.ts b/packages/core/src/config/index.ts index 1c50add74..7a4856688 100644 --- a/packages/core/src/config/index.ts +++ b/packages/core/src/config/index.ts @@ -125,6 +125,9 @@ export async function resolveConfig( // configPath may be file or directory const { configFile, configPath: initialConfigPath } = inlineOptions; + let configFilePath = initialConfigPath; + + // TODO issues1 这里拿到的 config.root 不应该是undefined 我们可以给默认值 const loadedUserConfig = await loadConfigFile( configFile, inlineOptions, @@ -132,6 +135,7 @@ export async function resolveConfig( defaultNodeEnv ); + // TODO issues4 这个方法是否有必要需要讨论 我们只考虑在最后拿到的 config 不存在 undefined 可以让rust 识别就ok let rawConfig: UserConfig = mergeFarmCliConfig( inlineOptions, {}, @@ -140,8 +144,6 @@ export async function resolveConfig( const inlineConfig = rawConfig; - let configFilePath = initialConfigPath; - if (loadedUserConfig) { configFilePath = loadedUserConfig.configFilePath; rawConfig = mergeConfig(rawConfig, loadedUserConfig.config); @@ -152,6 +154,7 @@ export async function resolveConfig( compileMode ); + // TODO issues2 如果我这时候push 了一个 插件在 resolveConfigHook 中 const sortFarmJsPlugins = getSortedPlugins([ ...jsPlugins, ...vitePluginAdapters, @@ -159,10 +162,12 @@ export async function resolveConfig( ]); const config = await resolveConfigHook(rawConfig, sortFarmJsPlugins); + + // TODO issues3 logger 放到方法内部 后续针对 compilation server logger 整合成一个方法 // define logger when resolvedConfigHook const logger = new Logger({ - customLogger: loadedUserConfig.config?.customLogger, - allowClearScreen: loadedUserConfig.config?.clearScreen + customLogger: config?.customLogger, + allowClearScreen: config?.clearScreen }); const resolvedUserConfig = await resolveUserConfig( @@ -171,19 +176,23 @@ export async function resolveConfig( compileMode ); + // TODO 同3 resolvedUserConfig.logger = logger; + // TODO 同3 // normalize server config first cause it may be used in normalizeUserCompilationFnConfig resolvedUserConfig.server = normalizeDevServerConfig( resolvedUserConfig.server, compileMode ); + // TODO 同3 resolvedUserConfig.compilation = await normalizeUserCompilationConfig( resolvedUserConfig, mode as CompilationMode ); + // TODO issues6 这个方法需要考虑是否需要整合到 resolveUserConfig 中 root 属性是否有必要在这里重新定义 Object.assign(resolvedUserConfig, { root: resolvedUserConfig.compilation.root, jsPlugins: sortFarmJsPlugins, @@ -193,6 +202,7 @@ export async function resolveConfig( await resolveConfigResolvedHook(resolvedUserConfig, sortFarmJsPlugins); // Fix: Await the Promise and pass the resolved value to the function. + // TODO issues5 这个也要整合进去 没必要放在 resolveConfigResolvedHook 之后 await handleLazyCompilation( resolvedUserConfig, command as keyof typeof COMMANDS @@ -205,8 +215,6 @@ async function handleLazyCompilation( config: ResolvedUserConfig, command: keyof typeof COMMANDS ) { - console.log(command); - const commandHandlers = { [COMMANDS.START]: async (config: ResolvedUserConfig) => { if ( diff --git a/packages/core/src/config/types.ts b/packages/core/src/config/types.ts index f81f2cf0e..dfde6480a 100644 --- a/packages/core/src/config/types.ts +++ b/packages/core/src/config/types.ts @@ -111,6 +111,7 @@ export interface UserConfig { watch?: boolean; envPrefix?: string | string[]; publicDir?: string; + customLogger?: Logger; /** js plugin(which is a javascript object) and rust plugin(which is string refer to a .farm file or a package) */ plugins?: (RustPlugin | JsPlugin | JsPlugin[] | undefined | null | false)[]; /** vite plugins */ From 1bcf658f664731b204c8a8eba3e16bb7f19b4693 Mon Sep 17 00:00:00 2001 From: erkelost <1256029807@qq.com> Date: Wed, 6 Nov 2024 18:02:40 +0800 Subject: [PATCH 3/3] refactor: redesign resolved config methods --- packages/core/src/config/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/core/src/config/index.ts b/packages/core/src/config/index.ts index 7a4856688..4001a0bc2 100644 --- a/packages/core/src/config/index.ts +++ b/packages/core/src/config/index.ts @@ -224,6 +224,7 @@ async function handleLazyCompilation( await setLazyCompilationDefine(config); } }, + // TODO 这个watch 方法需要在讨论 现在设计里没有 watch 这个方法了 build 的话也可以做 判断 config 里的 watch [COMMANDS.WATCH]: async (config: ResolvedUserConfig) => { if (config.compilation?.lazyCompilation) { await setLazyCompilationDefine(config);