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

amd-resolver

v1.2.3

Published

Resolve module names to module meta objects

Downloads

71

Readme

amd-resolver Build Status Gitter

Resolve module names to module meta objects.

A module meta object contains information such as a url that can be used for loading a file from storage. The module meta format is described here.

API

Resolver(options : object) : constructor

Creates interface to convert module names to a module meta object.

Parameters
  • options {object} - is a configuration options object with information for creating module meta objects. The options are compatible with RequireJS settings for paths, packages, baseUrl, shim, and urlArgs.
    • baseUrl {string} - path that every file is relative to.

    • paths {object} - is an object with key value pairs to map module names to files.

      For example, if you wanted to have a module called md5 and you wanted to map that to the location of the actual file, you can specify the following:

      {
        "paths": {
          "md5": "path/to/file/md5"
        }
      }

      That will tells resolver that the location for md5 is path/to/file/md5.js.

    • extensions {array} - is an array of strings that define a list of known extensions. Files with extensions in this whitelist will not get the js extension appened to it.

    • packages {array} - is an array of directory aliases to files. Think npm packages that load index.js by default.

      A package can be a string, in which case resolver will generate urls in the form of packagename/main.js. That is to say that if you have a package called machines, then resolving machines will generate a url to machinge/main.js.

      Alternatively, a package can be an object which provides more granual control of the resolution process. The following properties are supported:

      • location {string} - which is the location on disk.
      • main {string} - file name. Provide one if the module file is other than main.js.
      • name {string} - package name.
    • shim {object} - maps code in the global object to modules. An example of this is Backbone, which is loaded into the global object. So, in order for Module Loaders to load Backbone, they need to know how to find Backbone in the global object and also know its dependencies (underscore) in case Backbone needs to be loaded.

      Shims provides two properties:

      • exports|name {string} - The name of the code in the global object.
      • imports|deps {array} - List of dependencies. This is important when the shim has not yet been loaded and it requires other modules to be loaded first.
Example:
var resolver = new Resolver({
  "urlArgs": 'bust=' + (new Date()).getTime(),
  "baseUrl": "../",
  "extensions": ["json"],
  "paths": {
    "mocha": "../node_modules/mocha/mocha",
    "chai": "../node_modules/chai/chai"
  },
  "shim": {
    "mocha": {
      "exports": "mocha",
      "imports": ["sinon", "chai"]
    }
  },
  "packages": [
    "pacakge1", {
      "main": "index.js"
    }, {
      "location": "good/tests",
      "main": "index",
      "name": "js"
    }, {
      "location": "good/tests",
      "name": "lib"
    }
  ]
});

resolve(name : string, baseUrl : string)

Creates a module meta object. If name starts with ./, ../, or a protocol, then the resolution process will build a module meta with a URL using the input baseUrl (if available). The URL is built using this routine. If name starts with anything else, then the resolution process will use the baseUrl configured in the resolver ignoring the one passed it.

Parameters
  • name {string} - Name of the module to create a module meta object for. The name can be formatted with plugins such as css!filename.css.
  • baseUrl {string} - URL to be used as a base only when the name of the module starts with ./, ../, or a protocol. Otherwise, it is ignored.
Returns {object} - module meta
  • name {string} - Name of the module being resolved. Plugin definitions are not included.
  • url {Url} - Url object that's compliant with URL Api.
  • plugins {array} - Array of strings created from the input module name. Anything at the beginning of the module name that is delimited by a ! will be processed as a plugin.
  • shim {object} - Object containing information about modules that exist in the global object. shim can specify a couple of things.
    • name {string} - Name module has in the global space.
    • deps {array} - Array of string of dependencies that need to be loaded before the shim.
Examples:

Create module meta objects

var mochaModuleMeta    = resolver.resolve("mocha"),
    package1ModuleMeta = resolver.resolve("package1"),
    cssModuleMeta      = resolver.resolve("css!less!path/to/file.less");

Urls

var mochaUrl    = mochaModuleMeta.url.href,    // url === "../node_modules/mocha/mocha.js"
    package1Url = package1ModuleMeta.url.href, // url === "package1/index.js"
    cssUrl      = cssModuleMeta.url.href;      // url === "path/to/file.less"

Plugins

var cssPlugins = cssModuleMeta.plugins; // plugins === ["css", "less"]

Shim

var mochaShim = mochaModuleMeta.shim; // shim === {name: "mocha", deps: ["sinon", "chai"]}

Install

From npm

npm install amd-resolver