Code Monkey home page Code Monkey logo

unenv's Introduction

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.

Module Status Source
node:assert Mocked -
node:async_hooks Mocked -
node:buffer Polyfilled unenv/node/buffer
node:child_process Mocked -
node:cluster Mocked -
node:console Mocked -
node:constants Mocked -
node:crypto Mocked -
node:dgram Mocked -
node:diagnostics_channel Mocked -
node:dns Mocked -
node:domain Mocked -
node:events Polyfilled unenv/node/events
node:fs Polyfilled unenv/node/fs
node:fs/promises Polyfilled unenv/node/fs/promises
node:http2 Mocked -
node:http Polyfilled unenv/node/http
node:https Mocked -
node:inspector Mocked -
node:module Mocked -
node:net Polyfilled unenv/node/net
node:os Mocked -
node:path Polyfilled unenv/node/path
node:perf_hooks Mocked -
node:process Polyfilled unenv/node/process
node:punycode Mocked -
node:querystring Mocked -
node:readline Mocked -
node:repl Mocked -
node:stream Polyfilled unenv/node/stream
node:stream/consumers Mocked unenv/node/stream/consumers
node:stream/promises Mocked unenv/node/stream/promises
node:stream/web Native unenv/node/stream/web
node:string_decoder Mocked -
node:sys Mocked -
node:timers Mocked -
node:timers/promises Mocked -
node:tls Mocked -
node:trace_events Mocked -
node:tty Mocked -
node:url Polyfilled unenv/node/url
node:util Polyfilled unenv/node/util
node:util/types Polyfilled unenv/node/util/types
node:v8 Mocked -
node:vm Mocked -
node:wasi Mocked -
node:worker_threads Mocked -
node:zlib Mocked -

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

unenv's People

Contributors

pi0 avatar renovate[bot] avatar danielroe avatar atinux avatar farnabaz avatar mannil avatar ms-fadaei avatar officialpiyush avatar tahul avatar tarantoj-intrepid avatar

Stargazers

Roman avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.