npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

oraclejs

v0.1.1

Published

Use node packages in the browser without compilation

Downloads

32

Readme

OracleJS

TypeScript code style: prettier version

require("packages") in the browser without the need to compile first.

OracleJS can fetch packages using either of the following patterns:

To learn more about how to do this, visit www.unpkg.com. Alternatively, you may specify the address to the JS file directly, whether hosted on your personal website, GitHub or a Content Delivery Network (CDN).


Install

npm i oraclejs

Browser

<script src="https://unpkg.com/oraclejs"></script>
<script>window.test = { }; // dummy object</script>

Oracle returns a promise, therefore it can be used with .then() and .catch() methods

<script>
  oracle({
    deepObject: "deep-object",
    setValue: "[email protected]",
  }).then(function (imports) {
    const { deepObject, setValue } = imports;
    deepObject.set(test, "a", 1)
    setValue(test, "b", 2)
  })
</script>

Also, rather than using the default CDN, you may specify another as a replacement for fetching named packages. The formula is CDN + package_name, so make sure to cross-check the URL resolution result in a browser before using.

<script>
  oracle(
    {
      lodash_set: "[email protected]/index.js",
      setPath:
        "https://raw.githubusercontent.com/skratchdot/object-path-set/master/index.js",
    },
    "https://cdn.jsdelivr.net/npm/"
  ).then((imports) => {
    const { lodash_set, setPath } = imports;
    lodash_set(test, "c", 3)
    setPath(test, "d", 4)
  })
</script>

Alternatively, it can be run using async-await

<script>
  (async () => {
    const imports = await oracle({ deepProp: "deep-property" })
    imports.deepProp.set(test, "e", 5)
  })()
</script>

Import

// For strict browser use
import oracle from "oraclejs/browser";

// ES6 Import
import oracle from "oraclejs";
import { convertExports, convertImports } from "oraclejs";

// NodeJS Require
const oracle = require("oraclejs");
const { convertAll } = require("oraclejs");

API

oracle({ typeOf: "@ibnlanre/typeof" })
  .then(() => { typeOf(null) } // "null"

.convertAll(code)

This converts all static imports and exports to NodeJS requires

import { convertAll } from "oraclejs";

.convertExports(code)

This converts all static export syntax to module.exports and it comes with three methods.

import { convertExports } from "oraclejs";

.defaultExports(code)

const { defaultExports } = convertExports;

// module.exports = function* myGenFunc() {}
defaultExports("export function* myGenFunc() {}");

.namedExports(code)

const { namedExports } = convertExports;

// module.exports.myGenFunc = function* myGenFunc() {}
namedExports("export function* myGenFunc() {}");

.reExports(code)

const { reExports } = convertExports;

// const { foo: default } = require('src/other_module');
// module.exports.default = default;
reExports("export { foo as default } from 'src/other_module'");

.convertImports(code)

This converts all static import syntax to require and it comes with the following methods.

import { convertImports } from "oraclejs";

.combinedImports(code)

const { combinedImports } = convertImports;

// const theDefault = require('src/my_lib');
// const my_lib = Object.entries(require('src/my_lib'))
//  .reduce((acc, [key, val]) => ({ ...acc, [key]: val }), {})
combinedImports("import theDefault, * as my_lib from 'src/my_lib'");

.defaultImports(code)

const { defaultImports } = convertImports;

// const localName = require('src/my_lib');
combinedImports("import localName from 'src/my_lib'");

.emptyImports(code)

const { emptyImports } = convertImports;

// new Function("require('src/my_lib')")()
emptyImports("import 'src/my_lib'");

.namedImports(code)

const { namedImports } = convertImports;

// const { name1, name2 } = require('src/my_lib');
namedImports("import { name1, name2 } from 'src/my_lib'");

.namespaceImports(code)

const { namespaceImports } = convertImports;

// const my_lib = Object.entries(require('src/my_lib'))
//   .reduce((acc, [key, val]) => ({ ...acc, [key]: val }), {})
namespaceImports("import * as my_lib from 'src/my_lib'");