Skip to content

Commit

Permalink
feat: defineEnv util
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Dec 11, 2024
1 parent 4e76a66 commit 8d40d88
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
41 changes: 40 additions & 1 deletion src/env.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,44 @@
import type { Preset, Environment } from "./types";
import type { Preset, Environment, CreateEnvOptions } from "./types";

import nodeCompatPreset from "./presets/nodeless";

/**
* Configure a target environment.
*
* @example
* ```ts
* const { env } = defineEnv({
* nodeCompat: true,
* presets: [myPreset],
* overrides: {}
* });
*/
export function defineEnv(opts: CreateEnvOptions = {}): {
env: Environment;
} {
const presets: Preset[] = [];

if (opts.nodeCompat) {
presets.push(nodeCompatPreset);
}

if (opts.presets) {
presets.push(...opts.presets);
}

if (opts.overrides) {
presets.push(opts.overrides);
}

const resolvedEnv = env(...presets);

return { env: resolvedEnv };
}

/**
* Merge presets into a final environment.
* Later presets take precedence over earlier ones.
*/
export function env(...presets: Preset[]): Environment {
const _env: Environment = {
alias: {},
Expand Down
17 changes: 17 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
export interface CreateEnvOptions {
/**
* Enable Node.js compatibility (nodeless) preset.
*/
nodeCompat?: boolean;

/**
* Additional presets.
*/
presets?: Preset[];

/**
* Additional overrides.
*/
overrides?: Environment;
}

export interface Environment {
alias: { [key: string]: string };
inject: { [key: string]: string | string[] };
Expand Down

0 comments on commit 8d40d88

Please sign in to comment.