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

feat: config normalize after build #4972

Closed
wants to merge 1 commit into from
Closed
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
26 changes: 20 additions & 6 deletions packages/solutions/module-tools/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
38 changes: 22 additions & 16 deletions packages/solutions/module-tools/src/config/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,25 +67,10 @@ export const mergeConfig = (
};

export const normalizeBuildConfig = async (
api: PluginAPI<ModuleTools>,
mergedConfig: PartialBaseBuildConfig[],
context: ModuleContext,
buildCmdOptions: BuildCommandOptions,
): Promise<BaseBuildConfig[]> => {
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(
Expand All @@ -105,6 +90,27 @@ export const normalizeBuildConfig = async (
return normalizedConfig;
};

export const mergeBuildConfig = async (
api: PluginAPI<ModuleTools>,
): Promise<PartialBaseBuildConfig[]> => {
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 },
Expand Down
4 changes: 2 additions & 2 deletions packages/solutions/module-tools/src/hooks/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<BaseBuildConfig>(),
Expand Down