Skip to content

Commit

Permalink
standalone preset
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Dec 13, 2023
1 parent c6084b2 commit 5c2120e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ pnpm add -D unenv
Using `env` utility and built-in presets (and [nodeless](./src/presets/nodeless.ts)), `unenv` will provide an abstract configuration that can be used in building pipelines ([rollup.js](https://rollupjs.org), [webpack](https://webpack.js.org), etc.).

```js
import { env, node, nodeless } from "unenv";
import { env, node, deno, nodeless } from "unenv";

const { alias, inject, polyfill, external } = env(...presets);
const { alias, inject, polyfill, external } = env(nodeless, {});
```

## Presets
Expand All @@ -34,13 +34,31 @@ Suitable to convert universal libraries working in Node.js. ([preset](./src/pres
- Add supports for global [`fetch` API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)
- Set Node.js built-ins as externals

```js
import { env, nodeless } from "unenv";

const envConfig = env(node, {});
```

### `nodeless`

Using this preset, we can convert a code that is depending on Node.js to work anywhere else.

```js
import { env, nodeless } from "unenv";

const envConfig = env(nodeless, {});
```

### `deno`

This preset extends `nodeless` but allows using Deno's Node API Compatibility ([docs](https://docs.deno.com/runtime/manual/node/compatibility) and [docs](https://docs.deno.com/deploy/api/runtime-node)) ([preset](./src/presets/deno.ts)).
This preset can be used to extend `nodeless` to using Deno's Node API Compatibility ([docs](https://docs.deno.com/runtime/manual/node/compatibility) and [docs](https://docs.deno.com/deploy/api/runtime-node)) ([preset](./src/presets/deno.ts)).

```js
import { env, nodeless, deno } from "unenv";

const envConfig = env(nodeless, deno, {});
```

### Built-in Node.js modules

Expand Down
10 changes: 2 additions & 8 deletions src/presets/deno.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type { Preset } from "../types";
import nodeless from "./nodeless";

// https://docs.deno.com/runtime/manual/node/compatibility
// https://docs.deno.com/deploy/api/runtime-node
export const denoNodeCompatModules = [
const denoNodeCompatModules = [
"assert",
"assert/strict",
"async_hooks",
Expand Down Expand Up @@ -56,16 +55,11 @@ export const denoNodeCompatModules = [

const denoPreset: Preset = {
alias: {
...nodeless.alias,
...Object.fromEntries(denoNodeCompatModules.map((p) => [p, `node:${p}`])),
...Object.fromEntries(
denoNodeCompatModules.map((p) => [`node:${p}`, `node:${p}`]),
),
},
inject: {
...nodeless.inject,
},
polyfill: [...(nodeless.polyfill as string[])],
}
};

export default denoPreset;
1 change: 1 addition & 0 deletions src/presets/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default as node } from "./node";
export { default as nodeless } from "./nodeless";
export { default as deno } from "./deno";

0 comments on commit 5c2120e

Please sign in to comment.