Skip to content

Commit

Permalink
feat: presets meta (#374)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Dec 12, 2024
1 parent 4cd756d commit ec0747a
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import nodeCompatPreset from "./presets/nodeless";
*/
export function defineEnv(opts: CreateEnvOptions = {}): {
env: Environment;
presets: Preset[];
} {
const presets: Preset[] = [];

Expand All @@ -32,7 +33,7 @@ export function defineEnv(opts: CreateEnvOptions = {}): {

const resolvedEnv = env(...presets);

return { env: resolvedEnv };
return { env: resolvedEnv, presets };
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/presets/cloudflare.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Preset } from "../types";
import { version } from "../../package.json";

// Built-in APIs provided by workerd.
// https://developers.cloudflare.com/workers/runtime-apis/nodejs/
Expand Down Expand Up @@ -42,6 +43,10 @@ const hybridNodeCompatModules = [
];

const cloudflarePreset: Preset = {
meta: {
name: "unenv:cloudflare",
version,
},
alias: {
...Object.fromEntries(
cloudflareNodeCompatModules.flatMap((p) => [
Expand Down
6 changes: 6 additions & 0 deletions src/presets/node.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { NodeBuiltinModules } from "../utils";
import type { Preset } from "../types";
import { version } from "../../package.json";

export default {
meta: {
name: "unenv:node",
version,
},

alias: {
"node-fetch": "unenv/runtime/npm/node-fetch",
"cross-fetch": "unenv/runtime/npm/cross-fetch",
Expand Down
6 changes: 6 additions & 0 deletions src/presets/nodeless.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { NodeBuiltinModules, mapArrToVal } from "../utils";
import type { Preset } from "../types";
import { version } from "../../package.json";

const nodeless: Preset & { alias: Map<string, string> } = {
meta: {
name: "unenv:nodeless",
version,
},

alias: {
// Generic mock for built-ins
...mapArrToVal("unenv/runtime/mock/proxy-cjs", NodeBuiltinModules),
Expand Down
17 changes: 17 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ export interface Environment {
}

export interface Preset {
meta?: {
/**
* Preset name.
*/
readonly name?: string;

/**
* Preset version.
*/
readonly version?: string;

/**
* Path to preset directory usable for absolute path imports
*/
readonly path?: string;
};

alias?: Environment["alias"];
// inject's value is nullable to support overrides/subtraction
inject?: { [key: string]: string | string[] | false };
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"esModuleInterop": true,
"strict": true,
"declaration": true,
"resolveJsonModule": true,
"types": [
"node"
],
Expand Down

0 comments on commit ec0747a

Please sign in to comment.