Package Exports
- unenv
- unenv/runtime/_internal/types
- unenv/runtime/_internal/utils
- unenv/runtime/fetch/call
- unenv/runtime/fetch/index
- unenv/runtime/mock/empty
- unenv/runtime/mock/noop
- unenv/runtime/mock/proxy
- unenv/runtime/mock/proxy-cjs
- unenv/runtime/node/buffer/_base64
- unenv/runtime/node/buffer/_buffer
- unenv/runtime/node/buffer/_ieee754
- unenv/runtime/node/buffer/index
- unenv/runtime/node/events/_events
- unenv/runtime/node/events/index
- unenv/runtime/node/fs/_classes
- unenv/runtime/node/fs/_constants
- unenv/runtime/node/fs/_fs
- unenv/runtime/node/fs/index
- unenv/runtime/node/fs/promises/index
- unenv/runtime/node/http/_consts
- unenv/runtime/node/http/_request
- unenv/runtime/node/http/_response
- unenv/runtime/node/http/index
- unenv/runtime/node/net/index
- unenv/runtime/node/net/socket
- unenv/runtime/node/path/index
- unenv/runtime/node/process/_process
- unenv/runtime/node/process/index
- unenv/runtime/node/stream/consumers/index
- unenv/runtime/node/stream/duplex
- unenv/runtime/node/stream/index
- unenv/runtime/node/stream/promises/index
- unenv/runtime/node/stream/readable
- unenv/runtime/node/stream/transform
- unenv/runtime/node/stream/web/index
- unenv/runtime/node/stream/writable
- unenv/runtime/node/url/index
- unenv/runtime/node/util/_legacy-types
- unenv/runtime/node/util/_log
- unenv/runtime/node/util/_mime
- unenv/runtime/node/util/_promisify
- unenv/runtime/node/util/index
- unenv/runtime/node/util/types/_types
- unenv/runtime/node/util/types/index
- unenv/runtime/npm/consola
- unenv/runtime/npm/cross-fetch
- unenv/runtime/npm/debug
- unenv/runtime/npm/fsevents
- unenv/runtime/npm/inherits
- unenv/runtime/npm/mime
- unenv/runtime/npm/mime-db
- unenv/runtime/npm/node-fetch
- unenv/runtime/npm/whatwg-url
- unenv/runtime/polyfill/fetch.node
- unenv/runtime/polyfill/global
- unenv/runtime/polyfill/global-this
- unenv/runtime/polyfill/process
Readme
unenv
Unenv is a framework agnostic system that allows converting JavaScript code to be platform agnostic and working in any environment including Browsers, Workers, Node.js or pure JavaScript runtime.
Install
# Using npm
npm i -D unenv
# Using yarn
yarn add --dev unenv
Usage
Using env
utility and builtin presets ( and nodeless), unenv will provide an abstract config that can be used in building pipelines (rollup.js, webpack, etc)
import { env, node, nodeless } from 'unenv'
const { alias, inject, polyfill, external } = env(...presets)
Presets
node
Suitable to convert universal libraries working in Node.js. (preset)
- Add support for global
fetch
API - Set Node.js built-ins as externals
nodeless
Using this preset, we can convert a code that is depending on Node.js to work anywhere else.
Built-in Node.js modules
Unenv provides a replacement for all Node.js built-ins for cross-platform compatiblity.
npm packages
Unenv provides a replacement for common npm packages for cross platform compatibility.
Package | Status | Source |
---|---|---|
npm/consola | Use native console |
unenv/runtime/npm/consola |
npm/cross-fetch | Use native fetch |
unenv/runtime/npm/cross-fetch |
npm/debug | Mocked with console.debug |
unenv/runtime/npm/debug |
npm/fsevents | Mocked | unenv/runtime/npm/fsevents |
npm/inherits | Inlined | unenv/runtime/npm/inherits |
npm/mime-db | Minimized | unenv/runtime/npm/mime-db |
npm/mime | Minimized | unenv/runtime/npm/mime |
npm/node-fetch | Use native fetch |
unenv/runtime/npm/node-fetch |
npm/whatwg-url | Use native URL |
unenv/runtime/npm/whatwg-url |
Auto-mocking proxy
import MockProxy from 'unenv/runtime/mock/proxy'
console.log(MockProxy().foo.bar()[0])
Above package doesn't works 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 packages 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
, code will be compatible in other platforms.
Other polyfilles
Please check ./src/runtime to discover other polyfills.
License
MIT