Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Redesign resolvedConfig #1926

Open
wants to merge 5 commits into
base: v2-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/refactor-react/farm.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import react from "@farmfe/plugin-react";
import path from "path";
import { config } from "process";

console.log(__dirname);
// console.log(__dirname);

export default defineConfig((config) => {
console.log(config, "config");
Expand Down
39 changes: 26 additions & 13 deletions packages/core/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,17 @@ 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,
configEnv,
defaultNodeEnv
);

// TODO issues4 这个方法是否有必要需要讨论 我们只考虑在最后拿到的 config 不存在 undefined 可以让rust 识别就ok
let rawConfig: UserConfig = mergeFarmCliConfig(
inlineOptions,
{},
Expand All @@ -141,27 +145,30 @@ export async function resolveConfig(

const inlineConfig = rawConfig;

let configFilePath = initialConfigPath;

if (loadedUserConfig) {
configFilePath = loadedUserConfig.configFilePath;
rawConfig = mergeConfig(rawConfig, loadedUserConfig.config);
}

const { jsPlugins, vitePlugins, rustPlugins, vitePluginAdapters } =
await resolvePlugins(rawConfig, compileMode);
const { jsPlugins, rustPlugins, vitePluginAdapters } = await resolvePlugins(
rawConfig,
compileMode
);

// TODO issues2 如果我这时候push 了一个 插件在 resolveConfigHook 中
const sortFarmJsPlugins = getSortedPlugins([
...jsPlugins,
...vitePluginAdapters,
externalAdapter()
]);

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(
Expand All @@ -170,19 +177,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,
Expand All @@ -192,6 +203,7 @@ export async function resolveConfig(

await resolveConfigResolvedHook(resolvedUserConfig, sortFarmJsPlugins); // Fix: Await the Promise<void> and pass the resolved value to the function.

// TODO issues5 这个也要整合进去 没必要放在 resolveConfigResolvedHook 之后
await handleLazyCompilation(
resolvedUserConfig,
command as keyof typeof COMMANDS
Expand All @@ -205,17 +217,18 @@ async function handleLazyCompilation(
command: keyof typeof COMMANDS
) {
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);
// TODO 这个watch 方法需要在讨论 现在设计里没有 watch 这个方法了 build 的话也可以做 判断 config 里的 watch
[COMMANDS.WATCH]: async (config: ResolvedUserConfig) => {
if (config.compilation?.lazyCompilation) {
await setLazyCompilationDefine(config);
}
}
};
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export interface UserConfig {
watch?: boolean | WatchOptions;
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 */
Expand Down
Loading