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: normalize environment config #2633

Merged
merged 5 commits into from
Jun 19, 2024
Merged
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
6 changes: 5 additions & 1 deletion packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ const createDefaultConfig = (): RsbuildConfig => ({
tools: getDefaultToolsConfig(),
security: getDefaultSecurityConfig(),
performance: getDefaultPerformanceConfig(),
environments: {},
});

function getDefaultEntry(root: string): RsbuildEntry {
Expand Down Expand Up @@ -230,7 +231,10 @@ export const withDefaultConfig = async (
* 3. Meaningful and can be filled by constant value.
*/
export const normalizeConfig = (config: RsbuildConfig): NormalizedConfig =>
mergeRsbuildConfig(createDefaultConfig(), config) as NormalizedConfig;
mergeRsbuildConfig(
createDefaultConfig(),
config,
) as unknown as NormalizedConfig;

export type ConfigParams = {
env: string;
Expand Down
24 changes: 23 additions & 1 deletion packages/core/src/createContext.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { isAbsolute, join } from 'node:path';
import type {
BundlerType,
NormalizedEnvironmentConfig,
RsbuildContext,
RsbuildTarget,
} from '@rsbuild/shared';
Expand All @@ -22,7 +23,7 @@ function getAbsolutePath(root: string, filepath: string) {

function getAbsoluteDistPath(
cwd: string,
config: RsbuildConfig | NormalizedConfig,
config: RsbuildConfig | NormalizedConfig | NormalizedEnvironmentConfig,
) {
const dirRoot = config.output?.distPath?.root ?? ROOT_DIST_DIR;
return getAbsolutePath(cwd, dirRoot);
Expand All @@ -46,6 +47,7 @@ async function createContextByConfig(
entry: getEntryObject(config, 'web'),
targets: config.output?.targets || [],
version: RSBUILD_VERSION,
environments: {},
rootPath,
distPath,
cachePath,
Expand All @@ -56,6 +58,26 @@ async function createContextByConfig(
};
}

export function updateEnvironmentContext(
context: RsbuildContext,
configs: Record<string, NormalizedEnvironmentConfig>,
) {
context.environments = Object.fromEntries(
Object.entries(configs).map(([name, config]) => [
name,
{
// TODO: replace output.targets with output.target
target: config.output.targets[0],
distPath: getAbsoluteDistPath(context.rootPath, config),
entry: getEntryObject(config, config.output.targets[0]),
tsconfigPath: config.source.tsconfigPath
? getAbsolutePath(context.rootPath, config.source.tsconfigPath)
: undefined,
},
]),
);
}

export function updateContextByNormalizedConfig(
context: RsbuildContext,
config: NormalizedConfig,
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,9 @@ export function pick<T, U extends keyof T>(obj: T, keys: ReadonlyArray<U>) {
);
}

export const camelCase = (input: string): string =>
input.replace(/[-_](\w)/g, (_, c) => c.toUpperCase());

export const prettyTime = (seconds: number) => {
const format = (time: string) => color.bold(time);

Expand Down
20 changes: 18 additions & 2 deletions packages/core/src/initPlugins.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { join } from 'node:path';
import type {
GetRsbuildConfig,
NormalizedEnvironmentConfig,
PluginManager,
RsbuildPluginAPI,
RspackChain,
Expand Down Expand Up @@ -60,14 +61,29 @@ export function getPluginAPI({
const { hooks } = context;
const publicContext = createPublicContext(context);

const getNormalizedConfig = () => {
function getNormalizedConfig(): NormalizedConfig;
function getNormalizedConfig(options: {
environment: string;
}): NormalizedEnvironmentConfig;
function getNormalizedConfig(options?: { environment: string }) {
if (context.normalizedConfig) {
if (options?.environment) {
const config =
context.normalizedConfig.environments[options.environment];

if (!config) {
throw new Error(
`Cannot find normalized config by environment: ${options.environment}.`,
);
}
return config;
}
return context.normalizedConfig;
}
throw new Error(
'Cannot access normalized config until modifyRsbuildConfig is called.',
);
};
}

const getRsbuildConfig = ((type = 'current') => {
switch (type) {
Expand Down
10 changes: 4 additions & 6 deletions packages/core/src/mergeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,17 @@ const merge = (x: unknown, y: unknown, path = '') => {
return merged;
};

export const mergeRsbuildConfig = (
...configs: RsbuildConfig[]
): RsbuildConfig => {
export const mergeRsbuildConfig = <T = RsbuildConfig>(...configs: T[]): T => {
if (configs.length === 2) {
return merge(configs[0], configs[1]) as RsbuildConfig;
return merge(configs[0], configs[1]) as T;
}

if (configs.length < 2) {
return configs[0];
}

return configs.reduce(
(result, config) => merge(result, config) as RsbuildConfig,
{},
(result, config) => merge(result, config) as T,
{} as T,
);
};
3 changes: 2 additions & 1 deletion packages/core/src/plugins/entry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
type NormalizedEnvironmentConfig,
type RsbuildEntry,
type RsbuildTarget,
castArray,
Expand All @@ -10,7 +11,7 @@ import { reduceConfigsMergeContext } from '../reduceConfigs';
import type { NormalizedConfig, RsbuildConfig, RsbuildPlugin } from '../types';

export function getEntryObject(
config: RsbuildConfig | NormalizedConfig,
config: RsbuildConfig | NormalizedConfig | NormalizedEnvironmentConfig,
target: RsbuildTarget,
): RsbuildEntry {
if (!config.source?.entry) {
Expand Down
50 changes: 48 additions & 2 deletions packages/core/src/provider/initConfigs.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import type {
InspectConfigOptions,
NormalizedEnvironmentConfig,
PluginManager,
RspackConfig,
} from '@rsbuild/shared';
import { normalizeConfig } from '../config';
import { updateContextByNormalizedConfig } from '../createContext';
import {
updateContextByNormalizedConfig,
updateEnvironmentContext,
} from '../createContext';
import { camelCase } from '../helpers';
import { isDebug, logger } from '../logger';
import { mergeRsbuildConfig } from '../mergeConfig';
import { initPlugins } from '../pluginManager';
Expand Down Expand Up @@ -33,6 +38,37 @@ export type InitConfigsOptions = {
rsbuildOptions: Required<CreateRsbuildOptions>;
};

const normalizeEnvironmentsConfigs = (
normalizedConfig: NormalizedConfig,
): Record<string, NormalizedEnvironmentConfig> => {
const { environments, dev, server, provider, ...rsbuildSharedConfig } =
normalizedConfig;
if (environments) {
return Object.fromEntries(
Object.entries(environments).map(([name, config]) => [
name,
{
...mergeRsbuildConfig(
rsbuildSharedConfig,
config as unknown as NormalizedEnvironmentConfig,
),
dev,
server,
},
]),
);
}

return {
// TODO: replace output.targets with output.target
[camelCase(rsbuildSharedConfig.output.targets[0])]: {
...rsbuildSharedConfig,
dev,
server,
},
};
};

export async function initRsbuildConfig({
context,
pluginManager,
Expand All @@ -51,9 +87,19 @@ export async function initRsbuildConfig({
});

await modifyRsbuildConfig(context);
context.normalizedConfig = normalizeConfig(context.config);
const normalizeBaseConfig = normalizeConfig(context.config);

const environments = normalizeEnvironmentsConfigs(normalizeBaseConfig);

context.normalizedConfig = {
...normalizeBaseConfig,
environments,
};

updateContextByNormalizedConfig(context, context.normalizedConfig);

updateEnvironmentContext(context, environments);

return context.normalizedConfig;
}

Expand Down
Loading
Loading