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

require-precompute

v0.2.0

Published

Make it easier to create a `require` implementation.

Downloads

8

Readme

Overview Build Status

Creating a (reasonable subset of a) Node.js-style require(name) function is pretty simple. If name starts with a period, it is a path relative to the file that called require. Just make the require used by each file keep track of the current path and you’re done.

If name does not start with a period, it is a node module. Usually it is located at currentModuleRoot + "/node_modules/" + name + "/index.js". So make require also keep track of the current module root. Simple.

However, there are two things that can complicate the above. The entry point of a module might not necessarily be called “index.js”. The name can be defined by the main property of the module’s package.json (if any). Moreover, the module might be located in a parent module’s node_modules directory, if they both share a dependency.

This module precomputes the two complications above, to make a require implementation simpler. You probably already had a build step; just add this precomputation to it.

The idea is to first look for info in the precomputed data; if it’s not there use the above steps.

Example output, from a hypothetical “x-lang” module:

{
  "./node_modules/helpers": {
    "": "lib/main"
  },
  "./node_modules/parser": {
    "source-map": "."
  },
  "./node_modules/parser/node_modules/helpers": {
    "": "main"
  },
  "./node_modules/parser/node_modules/tokenizer": {
    "helpers": "./node_modules/parser"
  },
  "./node_modules/compiler/node_modules/stringifier": {
    "": "stringify",
    "source-map": ".",
    "token-names": "./node_modules/compiler"
  },
  "./node_modules/source-map": {
    "": "lib/source-map"
  }
}

In the above example, x-lang depends on “helpers”, “source-map”, “parser” and “compiler” (and possibly other modules as well, but if so they didn’t need any data in the output). The parser also depends on helpers, but it cannot use the top-level helpers because it uses a different version. (Those two versions happen to use different files as the main entry-point.) Both the parser and the compiler’s stringifier depend on source-map, but unlike the helpers case they reuse the top-level source-map.

Installation

npm install require-precompute

var precompute = require("require-precompute")

Usage

var data = precompute(dir)

dir is the path to a directory to precompute.

data is a map between module root paths relative to dir and an object with info about that module:

  • The value of the key "" (the empty string) is the path to the module’s entrypoint (if it has a package.json with the main property), relative to the module itself. The value has been path.normalized, and the .js suffix (if any) has been removed.
  • All other keys are module names that the module depends on (according to its package.json’s dependencies property (if any)), that aren’t in the module’s own node_modules directory, but somewhere higher up. The values are the paths to those modules (relative to dir). The algorithm does not look outside dir for modules, and throws an error if it cannot be found there.

CLI

If you install this package globally, you’ll be able to run require-precompute [dir], which precomputes dir (defaults to the current directory) and prints the result to stdout.

License

The X11 (“MIT”) License.