From 32b2dba810de64ba9c4ac41f0b3eaef40c6a4608 Mon Sep 17 00:00:00 2001 From: bestlyg Date: Mon, 20 Nov 2023 17:35:58 +0800 Subject: [PATCH] feat: config normalize after build --- packages/solutions/module-tools/src/build.ts | 26 ++++++++++--- .../module-tools/src/config/normalize.ts | 38 +++++++++++-------- .../solutions/module-tools/src/hooks/build.ts | 4 +- 3 files changed, 44 insertions(+), 24 deletions(-) diff --git a/packages/solutions/module-tools/src/build.ts b/packages/solutions/module-tools/src/build.ts index 05419b09874a..7152fafba2f2 100644 --- a/packages/solutions/module-tools/src/build.ts +++ b/packages/solutions/module-tools/src/build.ts @@ -15,24 +15,38 @@ export const build = async ( const runner = api.useHookRunners(); - debug('normalize build config'); + debug('merge build config'); - const { normalizeBuildConfig } = await import('./config/normalize'); - const resolvedBuildConfig = await normalizeBuildConfig(api, context, options); + const { mergeBuildConfig, normalizeBuildConfig } = await import( + './config/normalize' + ); + const mergedConfig = await mergeBuildConfig(api); - debug('normalize build config done'); + debug('merge build config done'); - debug('normalizedBuildConfig', resolvedBuildConfig); + debug('mergedConfig', mergedConfig); debug('run beforeBuild hooks'); await runner.beforeBuild({ - config: resolvedBuildConfig, + config: mergedConfig, cliOptions: options, }); debug('run beforeBuild hooks done'); + debug('normalize build config'); + + const resolvedBuildConfig = await normalizeBuildConfig( + mergedConfig, + context, + options, + ); + + debug('normalize build config done'); + + debug('normalizedBuildConfig', resolvedBuildConfig); + const builder = await import('./builder'); await builder.run({ cmdOptions: options, resolvedBuildConfig, context }, api); }; diff --git a/packages/solutions/module-tools/src/config/normalize.ts b/packages/solutions/module-tools/src/config/normalize.ts index 62bb43eae645..3de83b029740 100644 --- a/packages/solutions/module-tools/src/config/normalize.ts +++ b/packages/solutions/module-tools/src/config/normalize.ts @@ -67,25 +67,10 @@ export const mergeConfig = ( }; export const normalizeBuildConfig = async ( - api: PluginAPI, + mergedConfig: PartialBaseBuildConfig[], context: ModuleContext, buildCmdOptions: BuildCommandOptions, ): Promise => { - let config = api.useConfigContext() as unknown as ModuleUserConfig; - - if (isLegacyUserConfig(config as { legacy?: boolean })) { - const { createUserConfigFromLegacy } = await import( - './transformLegacyConfig' - ); - config = await createUserConfigFromLegacy(config as ModuleLegacyUserConfig); - } - - const { buildConfig, buildPreset } = config; - - const configFromPreset = await presetToConfig(buildPreset); - - const mergedConfig = mergeConfig(configFromPreset, buildConfig ?? {}); - validPartialBuildConfig(mergedConfig, context.appDirectory); const normalizedConfig = await Promise.all( @@ -105,6 +90,27 @@ export const normalizeBuildConfig = async ( return normalizedConfig; }; +export const mergeBuildConfig = async ( + api: PluginAPI, +): Promise => { + let config = api.useConfigContext() as unknown as ModuleUserConfig; + + if (isLegacyUserConfig(config as { legacy?: boolean })) { + const { createUserConfigFromLegacy } = await import( + './transformLegacyConfig' + ); + config = await createUserConfigFromLegacy(config as ModuleLegacyUserConfig); + } + + const { buildConfig, buildPreset } = config; + + const configFromPreset = await presetToConfig(buildPreset); + + const mergedConfig = mergeConfig(configFromPreset, buildConfig ?? {}); + + return mergedConfig; +}; + export const transformToAbsPath = async ( baseConfig: BaseBuildConfig, options: { context: ModuleContext; buildCmdOptions: BuildCommandOptions }, diff --git a/packages/solutions/module-tools/src/hooks/build.ts b/packages/solutions/module-tools/src/hooks/build.ts index cc8a454c476d..729e37067ade 100644 --- a/packages/solutions/module-tools/src/hooks/build.ts +++ b/packages/solutions/module-tools/src/hooks/build.ts @@ -10,13 +10,13 @@ import type { BuildPlatformResult, WatchDtsHookContext, WatchJsHookContext, - BuildConfig, BaseBuildConfig, + PartialBaseBuildConfig, } from '../types'; export const buildHooks = { beforeBuild: createParallelWorkflow< - { config: BuildConfig; cliOptions: BuildCommandOptions }, + { config: PartialBaseBuildConfig[]; cliOptions: BuildCommandOptions }, void >(), beforeBuildTask: createAsyncWaterfall(),