reduce-configs
Merge an initial configuration object with one or more configuration objects, functions, or arrays of configuration objects/functions.
Install
npm add reduce-configs -D
reduceConfigs
The reduceConfigs
function merges one or more configuration objects into a final configuration. It also allows modification of the configuration object via functions.
type OneOrMany<T> = T | T[];
type ConfigChain<T> = OneOrMany<T | ((config: T) => T | void)>;
function reduceConfigs<T>(options: {
initial: T;
config?: ConfigChain<T> | undefined;
mergeFn?: typeof Object.assign;
}): T;
import { reduceConfigs } from "@rsbuild/core";
const initial = { a: 1, b: 2 };
const finalConfig1 = reduceConfigs({
initial: initial,
config: { b: 3, c: 4 },
});
const finalConfig2 = reduceConfigs({
initial: initial,
config: (config) => ({ ...config, b: 5, d: 6 }),
});
const finalConfig3 = reduceConfigs({
initial: initial,
config: [
{ b: 7 },
(config) => ({ ...config, c: 8 }),
(config) => ({ ...config, d: 9 }),
],
});
reduceConfigsWithContext
The reduceConfigsWithContext
function is similar to reduceConfigs
, which allows you to pass an additional context
object to the configuration function.
type OneOrMany<T> = T | T[];
type ConfigChainWithContext<T, Ctx> = OneOrMany<
T | ((config: T, ctx: Ctx) => T | void)
>;
function reduceConfigsWithContext<T, Ctx>(options: {
initial: T;
config?: ConfigChain<T> | undefined;
ctx?: Ctx;
mergeFn?: typeof Object.assign;
}): T;
import { reduceConfigsWithContext } from "@rsbuild/core";
const initial = { a: 1, b: 2 };
const context = { user: "admin" };
const finalConfig = reduceConfigsWithContext({
initial,
config: [
{ b: 3 },
(config, ctx) => ({ ...config, c: ctx.user === "admin" ? 99 : 4 }),
],
ctx: context,
});
License
MIT.