Skip to content

Commit

Permalink
refactor!: add all npm shims with opt-in npmShims (#444)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Feb 12, 2025
1 parent db54843 commit 98f7104
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 27 deletions.
31 changes: 17 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ The `defineEnv` utility can generate a target environment configuration.
import { defineEnv } from "unenv";

const { env } = defineEnv({
/* options */
nodeCompat: true,
npmShims: true,
resolve: true,
overrides: {},
presets: [],
});

const { alias, inject, external, polyfill } = env;
Expand All @@ -51,26 +55,25 @@ You can then integrate the env object with your build tool:
### Options

- `nodeCompat`: (default: `true`)
- Add `alias` entries for Node.js builtins as `<id>` and `node:<id>`.
- Add `inject` entries for Node.js globals `global`, `Buffer`, and `process`.
- `npmShims`: (default: `true`)
- Add `alias` entries to replace npm packages like `node-fetch` with lighter shims.
- Add `alias` entries for Node.js builtins as `<id>` and `node:<id>`.
- Add `inject` entries for Node.js globals `global`, `Buffer`, and `process`.
- `npmShims`: (default: `false`)
- Add `alias` entries to replace npm packages like `node-fetch` with lighter shims.
- `resolve`: (default: `false`) Resolve config values to absolute paths.
- `overrides`: Additional overrides for env config.
- `presets`: Additional presets (for example [`@cloudflare/unenv-preset`](https://npmjs.com/@cloudflare/unenv-preset/)).


### `unenv/` polyfills

You can also directly import `unenv/` polyfills:

| Polyfills | Description | Source |
| ---------------- | ------------------------------------------ | -------------------------------------------------------------------------------------- |
| `unenv/mock/*` | Mocking utils | [`src/runtime/mock/`](https://github.com/unjs/unenv/tree/main/src/runtime/mock) |
| `unenv/node/*` | APIs compatible with `Node.js` API | [`src/runtime/node/`](https://github.com/unjs/unenv/tree/main/src/runtime/node) |
| `unenv/npm/*` | NPM package shims | [`src/runtime/npm`](https://github.com/unjs/unenv/tree/main/src/runtime/npm) |
| `unenv/polyfill/*` | Global polyfills | [`src/runtime/polyfill/`](https://github.com/unjs/unenv/tree/main/src/runtime/polyfill) |
| `unenv/web/*` | Subset of Web APIs | [`src/runtime/web/`](https://github.com/unjs/unenv/tree/main/src/runtime/web) |
| Polyfills | Description | Source |
| ------------------ | ---------------------------------- | -------------------------------------------------------------------------------------- |
| `unenv/mock/*` | Mocking utils | [`src/runtime/mock`](https://github.com/unjs/unenv/tree/main/src/runtime/mock) |
| `unenv/node/*` | APIs compatible with `Node.js` API | [`src/runtime/node`](https://github.com/unjs/unenv/tree/main/src/runtime/node) |
| `unenv/npm/*` | NPM package shims | [`src/runtime/npm`](https://github.com/unjs/unenv/tree/main/src/runtime/npm) |
| `unenv/polyfill/*` | Global polyfills | [`src/runtime/polyfill`](https://github.com/unjs/unenv/tree/main/src/runtime/polyfill) |
| `unenv/web/*` | Subset of Web APIs | [`src/runtime/web`](https://github.com/unjs/unenv/tree/main/src/runtime/web) |

## Node.js compatibility

Expand Down Expand Up @@ -139,7 +142,7 @@ You can also directly import `unenv/` polyfills:

</details>

## Manual mocking
## Manual mocking

<details>

Expand Down
9 changes: 6 additions & 3 deletions lib/index.d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ export declare function defineEnv(opts?: CreateEnvOptions): {

export interface CreateEnvOptions {
/**
* Node.js compatibility aliases.
* Node.js compatibility.
*
* - Add `alias` entries for Node.js builtins as `<id>` and `node:<id>`.
* - Add `inject` entries for Node.js globals `global`, `Buffer`, and `process`.
*
* Default: `true`
*/
nodeCompat?: boolean;

/**
* NPM compatibility aliases.
* Add `alias` entries to replace npm packages like `node-fetch` with lighter shims.
*
* Default: `true`
* Default: `false`
*/
npmShims?: boolean;

Expand Down
2 changes: 1 addition & 1 deletion src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function unenvPreset(opts: CreateEnvOptions) {
});
}

if (opts.npmShims !== false) {
if (opts.npmShims) {
Object.assign(preset.alias, npmShims);
}

Expand Down
21 changes: 12 additions & 9 deletions src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,22 @@ export const nodeCompatAliases = {
zlib: "unenv/node/zlib",
} as const;

export const nodeCompatInjects = {
global: "unenv/polyfill/globalthis",
process: "unenv/node/process",
Buffer: ["unenv/node/buffer", "Buffer"],
} as const;

export const npmShims = {
"cross-fetch": "unenv/npm/cross-fetch",
debug: "unenv/npm/debug",
fsevents: "unenv/npm/fsevents",
inherits: "unenv/npm/inherits",
"node-fetch": "unenv/npm/node-fetch",
"node-fetch-native": "unenv/npm/node-fetch",
"node-fetch-native/polyfill": "unenv/mock/empty",
"cross-fetch": "unenv/npm/cross-fetch",
"whatwg-url": "unenv/npm/whatwg-url",
// polyfills
"cross-fetch/polyfill": "unenv/mock/empty",
"node-fetch-native/polyfill": "unenv/mock/empty",
"isomorphic-fetch": "unenv/mock/empty",
inherits: "unenv/npm/inherits",
} as const;

export const nodeCompatInjects = {
global: "unenv/polyfill/globalthis",
process: "unenv/node/process",
Buffer: ["unenv/node/buffer", "Buffer"],
} as const;

0 comments on commit 98f7104

Please sign in to comment.