MiguelCastillo / amd-resolver

Resolve module names to File objects
MIT License
0 stars 1 forks source link

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
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
Returns {object} - module meta
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