From 88b1f7f11649068e70c6e77f1f2269e72d9affc3 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 11 Dec 2024 19:48:18 +0100 Subject: [PATCH 1/2] feat: presets meta --- src/env.ts | 3 ++- src/presets/cloudflare.ts | 5 +++++ src/presets/node.ts | 6 ++++++ src/presets/nodeless.ts | 6 ++++++ src/types.ts | 17 +++++++++++++++++ tsconfig.json | 1 + 6 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/env.ts b/src/env.ts index 58dbe4d3..2a3d7b01 100644 --- a/src/env.ts +++ b/src/env.ts @@ -15,6 +15,7 @@ import nodeCompatPreset from "./presets/nodeless"; */ export function defineEnv(opts: CreateEnvOptions = {}): { env: Environment; + presets: Preset[]; } { const presets: Preset[] = []; @@ -32,7 +33,7 @@ export function defineEnv(opts: CreateEnvOptions = {}): { const resolvedEnv = env(...presets); - return { env: resolvedEnv }; + return { env: resolvedEnv, presets }; } /** diff --git a/src/presets/cloudflare.ts b/src/presets/cloudflare.ts index 710559fa..4d5e28f8 100644 --- a/src/presets/cloudflare.ts +++ b/src/presets/cloudflare.ts @@ -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/ @@ -42,6 +43,10 @@ const hybridNodeCompatModules = [ ]; const cloudflarePreset: Preset = { + meta: { + name: "unenv:cloudflare", + version, + }, alias: { ...Object.fromEntries( cloudflareNodeCompatModules.flatMap((p) => [ diff --git a/src/presets/node.ts b/src/presets/node.ts index 68d07eb2..c29da048 100644 --- a/src/presets/node.ts +++ b/src/presets/node.ts @@ -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", diff --git a/src/presets/nodeless.ts b/src/presets/nodeless.ts index 93a20b65..4e3ac1fb 100644 --- a/src/presets/nodeless.ts +++ b/src/presets/nodeless.ts @@ -1,7 +1,13 @@ import { NodeBuiltinModules, mapArrToVal } from "../utils"; import type { Preset } from "../types"; +import { version } from "../../package.json"; const nodeless: Preset & { alias: Map } = { + meta: { + name: "unenv:nodeless", + version, + }, + alias: { // Generic mock for built-ins ...mapArrToVal("unenv/runtime/mock/proxy-cjs", NodeBuiltinModules), diff --git a/src/types.ts b/src/types.ts index c0907087..e6735a00 100644 --- a/src/types.ts +++ b/src/types.ts @@ -23,6 +23,23 @@ export interface Environment { } export interface Preset { + meta?: { + /** + * Preset name. + */ + name?: string; + + /** + * Preset version. + */ + version?: string; + + /** + * Path to preset directory usable for absolute path imports + */ + path?: string; + }; + alias?: Environment["alias"]; // inject's value is nullable to support overrides/subtraction inject?: { [key: string]: string | string[] | false }; diff --git a/tsconfig.json b/tsconfig.json index 2df85352..58fc2946 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,6 +6,7 @@ "esModuleInterop": true, "strict": true, "declaration": true, + "resolveJsonModule": true, "types": [ "node" ], From 5dfe4977757dc65fe7619233a96fe2b5b52e0ac2 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Thu, 12 Dec 2024 16:05:09 +0100 Subject: [PATCH 2/2] mark meta as readonly --- src/types.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/types.ts b/src/types.ts index e6735a00..984f6806 100644 --- a/src/types.ts +++ b/src/types.ts @@ -27,17 +27,17 @@ export interface Preset { /** * Preset name. */ - name?: string; + readonly name?: string; /** * Preset version. */ - version?: string; + readonly version?: string; /** * Path to preset directory usable for absolute path imports */ - path?: string; + readonly path?: string; }; alias?: Environment["alias"];