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.
# Using npm
npm i -D unenv
# Using yarn
yarn add --dev unenv
# Using pnpm
pnpm add -D unenv
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.
Suitable to convert universal libraries working in Node.js.
- Add supports for global
fetch
API - Set Node.js built-ins as externals
import { env, node } from "unenv";
const envConfig = env(node, {});
Suitable to transform libraries made for Node.js to run in other JavaScript runtimes.
import { env, nodeless } from "unenv";
const envConfig = env(nodeless, {});
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, {});
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, {});
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, {});
unenv
provides a replacement for all Node.js built-ins for cross-platform compatibility.
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 |
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("node: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.
To discover other polyfills, please check ./src/runtime.
MIT
unenv's People
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.