Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: presets meta #374

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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[];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: update l10 above with presets

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure to undrestand, L10 is part of example code:

image

} {
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 };
pi0 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
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> } = {
pi0 marked this conversation as resolved.
Show resolved Hide resolved
meta: {
name: "unenv:nodeless",
pi0 marked this conversation as resolved.
Show resolved Hide resolved
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?: {
pi0 marked this conversation as resolved.
Show resolved Hide resolved
/**
* 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
Loading