Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/rspack-contrib/reduce-configs

Merge an initial configuration object with one or more configuration objects, functions, or arrays of configuration objects/functions.
https://github.com/rspack-contrib/reduce-configs

Last synced: 16 days ago
JSON representation

Merge an initial configuration object with one or more configuration objects, functions, or arrays of configuration objects/functions.

Awesome Lists containing this project

README

        

# reduce-configs

Merge an initial configuration object with one or more configuration objects, functions, or arrays of configuration objects/functions.



npm version

license

## Install

```bash
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:**

```ts
type OneOrMany = T | T[];
type ConfigChain = OneOrMany T | void)>;

function reduceConfigs(options: {
/**
* Initial configuration object.
*/
initial: T;
/**
* The configuration object, function, or array of configuration objects/functions
* to be merged into the initial configuration
*/
config?: ConfigChain | undefined;
/**
* The function used to merge configuration objects.
* @default Object.assign
*/
mergeFn?: typeof Object.assign;
}): T;
```

- **Example:**

```ts
import { reduceConfigs } from "@rsbuild/core";

const initial = { a: 1, b: 2 };

// Merging an object
const finalConfig1 = reduceConfigs({
initial: initial,
config: { b: 3, c: 4 },
});
// -> { a: 1, b: 3, c: 4 }

// Using a function to modify the config
const finalConfig2 = reduceConfigs({
initial: initial,
config: (config) => ({ ...config, b: 5, d: 6 }),
});
// -> { a: 1, b: 5, d: 6 }

// Merging an array of objects/functions
const finalConfig3 = reduceConfigs({
initial: initial,
config: [
{ b: 7 },
(config) => ({ ...config, c: 8 }),
(config) => ({ ...config, d: 9 }),
],
});
// -> { a: 1, b: 7, c: 8, d: 9 }
```

## reduceConfigsWithContext

The `reduceConfigsWithContext` function is similar to `reduceConfigs`, which allows you to pass an additional `context` object to the configuration function.

- **Type:**

```ts
type OneOrMany = T | T[];
type ConfigChainWithContext = OneOrMany<
T | ((config: T, ctx: Ctx) => T | void)
>;

function reduceConfigsWithContext(options: {
/**
* Initial configuration object.
*/
initial: T;
/**
* The configuration object, function, or array of configuration objects/functions
* to be merged into the initial configuration
*/
config?: ConfigChain | undefined;
/**
* Context object that can be used within the configuration functions.
*/
ctx?: Ctx;
/**
* The function used to merge configuration objects.
* @default Object.assign
*/
mergeFn?: typeof Object.assign;
}): T;
```

- **Example:**

```ts
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,
});
// -> { a: 1, b: 3, c: 99 }
```

## License

[MIT](./LICENSE).