1.9.0 • Published 4 months ago

unenv v1.9.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

unenv

unenv is a framework-agnostic system that allows converting JavaScript code to be platform agnostic and work in any environment including Browsers, Workers, Node.js, or JavaScript runtime.

Install

# Using npm
npm i -D unenv

# Using yarn
yarn add --dev unenv

# Using pnpm
pnpm add -D unenv

Usage

Using env utility and built-in presets, unenv will provide an abstract configuration that can be used in building pipelines (rollup.js, webpack, etc.).

import { env } from "unenv";

const { alias, inject, polyfill, external } = env({}, {}, {});

Note: You can provide as many presets as you want. unenv will merge them internally and the right-most preset has a higher priority.

Presets

node

(view source)

Suitable to convert universal libraries working in Node.js.

  • Add supports for global fetch API
  • Set Node.js built-ins as externals
import { env, nodeless } from "unenv";

const envConfig = env(node, {});

nodeless

(view source)

Suitable to transform libraries made for Node.js to run in other JavaScript runtimes.

import { env, nodeless } from "unenv";

const envConfig = env(nodeless, {});

deno

(view source)

This preset can be used to extend nodeless to use Deno's Node.js API Compatibility (docs, docs).

!WARNING This preset is experimental and behavior might change!

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

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

cloudflare

(view source)

This preset can be used to extend nodeless to use Cloudflare Worker Node.js API Compatibility (docs).

!WARNING This preset is experimental and behavior might change!

!NOTE Make sure to enable nodejs_compat compatibility flag.

import { env, nodeless, cloudflare } from "unenv";

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

vercel

This preset can be used to extend nodeless to use Vercel Edge Node.js API Compatibility (docs).

!WARNING This preset is experimental and behavior might change!

import { env, nodeless, vercel } from "unenv";

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

Built-in Node.js modules

unenv provides a replacement for all Node.js built-ins for cross-platform compatibility.

ModuleStatusSource
node:assertMocked-
node:async_hooksPolyfilledunenv/node/async_hooks
node:bufferPolyfilledunenv/node/buffer
node:child_processMocked-
node:clusterMocked-
node:consoleMocked-
node:constantsMocked-
node:cryptoPolyfilledunenv/node/crypto
node:dgramMocked-
node:diagnostics_channelMocked-
node:dnsMocked-
node:domainMocked-
node:eventsPolyfilledunenv/node/events
node:fsPolyfilledunenv/node/fs
node:fs/promisesPolyfilledunenv/node/fs/promises
node:http2Mocked-
node:httpPolyfilledunenv/node/http
node:httpsPolyfilledunenv/node/https
node:inspectorMocked-
node:modulePolyfilledunenv/node/module -
node:netPolyfilledunenv/node/net
node:osMocked-
node:pathPolyfilledunenv/node/path
node:perf_hooksMocked-
node:processPolyfilledunenv/node/process
node:punycodeMocked-
node:querystringMocked-
node:readlineMocked-
node:replMocked-
node:streamPolyfilledunenv/node/stream
node:stream/consumersMockedunenv/node/stream/consumers
node:stream/promisesMockedunenv/node/stream/promises
node:stream/webNativeunenv/node/stream/web
node:string_decoderPolyfilledunenv/node/string_decoder
node:sysMocked-
node:timersMocked-
node:timers/promisesMocked-
node:tlsMocked-
node:trace_eventsMocked-
node:ttyMocked-
node:urlPolyfilledunenv/node/url
node:utilPolyfilledunenv/node/util
node:util/typesPolyfilledunenv/node/util/types
node:v8Mocked-
node:vmMocked-
node:wasiMocked-
node:worker_threadsMocked-
node:zlibMocked-

npm packages

unenv provides a replacement for common npm packages for cross platform compatibility.

PackageStatusSource
npm/consolaUse native consoleunenv/runtime/npm/consola
npm/cross-fetchUse native fetchunenv/runtime/npm/cross-fetch
npm/debugMocked with console.debugunenv/runtime/npm/debug
npm/fseventsMockedunenv/runtime/npm/fsevents
npm/inheritsInlinedunenv/runtime/npm/inherits
npm/mime-dbMinimizedunenv/runtime/npm/mime-db
npm/mimeMinimizedunenv/runtime/npm/mime
npm/node-fetchUse native fetchunenv/runtime/npm/node-fetch
npm/whatwg-urlUse native URLunenv/runtime/npm/whatwg-url

Auto-mocking proxy

import MockProxy from "unenv/runtime/mock/proxy";

console.log(MockProxy().foo.bar()[0]);

The above package doesn't work outside of Node.js and neither we need any platform-specific logic! When aliasing os to mock/proxy-cjs, it will be auto-mocked using a Proxy Object which can be recursively traversed like an Object, called like a Function, Iterated like an Array, or instantiated like a Class.

We use this proxy for auto-mocking unimplemented internals. Imagine a package does this:

const os = require("os");
if (os.platform() === "windows") {
  /* do some fix */
}
module.exports = () => "Hello world";

By aliasing os to unenv/runtime/mock/proxy-cjs, the code will be compatible with other platforms.

Other polyfills

To discover other polyfills, please check ./src/runtime.

License

MIT

1.9.0

4 months ago

1.8.0

6 months ago

1.6.2

9 months ago

1.6.1

9 months ago

1.6.0

9 months ago

1.7.4

9 months ago

1.7.3

9 months ago

1.7.2

9 months ago

1.7.1

9 months ago

1.7.0

9 months ago

1.5.2

10 months ago

1.5.1

12 months ago

1.5.0

12 months ago

1.2.0

1 year ago

1.1.1

1 year ago

1.0.2

1 year ago

1.1.0

1 year ago

1.4.1

1 year ago

1.4.0

1 year ago

1.3.1

1 year ago

1.2.2

1 year ago

1.3.0

1 year ago

1.2.1

1 year ago

1.0.3

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago

0.6.2

2 years ago

0.6.1

2 years ago

0.6.0

2 years ago

0.5.4

2 years ago

0.5.3

2 years ago

0.5.0

2 years ago

0.5.2

2 years ago

0.5.1

2 years ago

0.4.5

2 years ago

0.4.4

2 years ago

0.4.6

2 years ago

0.4.3

2 years ago

0.4.2

2 years ago

0.4.1

3 years ago

0.4.0

3 years ago

0.3.9

3 years ago

0.3.10

3 years ago

0.3.6

3 years ago

0.3.8

3 years ago

0.3.7

3 years ago

0.3.5

3 years ago

0.3.4

3 years ago

0.3.3

3 years ago

0.3.2

3 years ago

0.3.1

3 years ago

0.3.0

3 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.1

3 years ago