amd-resolve
Advanced tools
Comparing version
113
lib/index.js
@@ -17,5 +17,9 @@ /** | ||
*/ | ||
var special = exports.special = ['require', 'exports', 'module']; | ||
var special = exports.special = [ | ||
'require', 'exports', 'module' | ||
].reduce(function(acc, x) { acc[x] = true; return acc }, {}); | ||
exports.isSpecial = function (x) { return special[x] }; | ||
/** | ||
@@ -42,2 +46,3 @@ * Resolves module `id` to an absolute path. | ||
* https://github.com/amdjs/amdjs-api/wiki/Common-Config | ||
* http://wiki.commonjs.org/wiki/Modules/AsynchronousDefinition | ||
* http://requirejs.org/docs/api.html#jsfiles | ||
@@ -49,2 +54,4 @@ * http://requirejs.org/docs/api.html#config-baseUrl | ||
* as well as these implementations: | ||
* https://github.com/jrburke/requirejs/blob/2.1.0/require.js | ||
* https://github.com/jrburke/almond/blob/0.2.0/almond.js | ||
* https://github.com/jrburke/r.js/blob/2.1.0/build/jslib/requirePatch.js#L146 | ||
@@ -58,7 +65,7 @@ * | ||
exports.sync = function(id, opts) { | ||
if (special.indexOf(id) !== -1) return id; | ||
if (special[id]) return id; | ||
var baseDir = opts.baseDir || opts.baseUrl || process.cwd(); | ||
var relDir = opts.relDir; | ||
var parentID = opts.parentID; | ||
var relDir = opts.relDir; | ||
var baseDir = opts.baseDir || opts.baseUrl || process.cwd(); | ||
var paths = opts.paths || {}; | ||
@@ -73,6 +80,2 @@ var map = opts.map || {}; | ||
// TODO: Are "paths", "map", and "packages" options mutually explusive? | ||
// If yes, which takes priority; if no, how should the output from one | ||
// tranform apply as input to the other? | ||
if (id.charAt(0) === '.') { | ||
@@ -83,42 +86,47 @@ // relative module | ||
} else { | ||
// top-level module | ||
var ids; | ||
// Refer to the following functions in require.js for corresponding logic: | ||
// nameToUrl() | ||
// apply mappings specified in "map" for the parent module to the requested | ||
// module | ||
if (parentID) { | ||
var psegs = parentID.split('/') | ||
, dmap; | ||
// apply mappings specified in "paths" to the requested module | ||
for (var prefix in paths) { | ||
// TODO: Do more specific prefixes take priority (as is the case with the | ||
// "map" option)? This is not stated in the specification. | ||
for (var i = psegs.length; i > 0; i--) { | ||
var pmp = psegs.slice(0, i).join('/'); | ||
dmap = map[pmp]; | ||
if (dmap) { break; } | ||
} | ||
if (!dmap) { | ||
dmap = map['*']; | ||
} | ||
if (id.indexOf(prefix) === 0) { | ||
var p = paths[prefix] | ||
, r = id.slice(prefix.length); | ||
if (typeof p === 'string') { | ||
ids = [ p + r ]; | ||
} else if (Array.isArray(p)) { | ||
ids = p.map(function(pi) { return pi + r }); | ||
if (dmap) { | ||
var mid = modmap(id, dmap); | ||
if (mid) { | ||
id = mid; | ||
} | ||
break; | ||
} | ||
} | ||
ids = ids || [ id ]; | ||
// apply mappings specified in "map" for the parent module to the requested | ||
// module | ||
for (var pprefix in map) { | ||
var pprio = -1; | ||
if ((parentID && parentID.indexOf(pprefix) === 0) || | ||
(pprefix === '*' && 0 > pprio)) { | ||
var dmap = map[pprefix] | ||
, prio = -1; | ||
for (var prefix in dmap) { | ||
if (id.indexOf(prefix) === 0 && prefix.length > prio) { | ||
var p = dmap[prefix] | ||
, r = id.slice(prefix.length); | ||
ids = [ p + r ]; | ||
prio = prefix.length; | ||
function modmap(id, paths) { | ||
var segs = id.split('/'); | ||
for (var i = segs.length; i > 0; i--) { | ||
var mp = segs.slice(0, i).join('/') | ||
, p = paths[mp]; | ||
if (p) { | ||
var rem = id.slice(mp.length); | ||
return p + rem; | ||
} | ||
} | ||
pprio = pprefix !== '*' ? pprefix.length : 0; | ||
return null; | ||
} | ||
} | ||
var ids = [ id ] | ||
, segs = id.split('/') | ||
, fseg = segs[0]; | ||
@@ -134,8 +142,8 @@ // apply configuration specified in "packages" to the requested module | ||
if (id.indexOf(pkg.name) === 0) { | ||
var r = id.slice(pkg.name.length); | ||
if (r.length === 0) { | ||
if (fseg === pkg.name) { | ||
var rem = id.slice(pkg.name.length); | ||
if (rem.length === 0) { | ||
ids = [ pkg.location + '/' + pkg.main ] | ||
} else { | ||
ids = [ pkg.location + '/' + r ] | ||
ids = [ pkg.location + '/' + rem ] | ||
} | ||
@@ -145,2 +153,19 @@ } | ||
// apply mappings specified in "paths" to the requested module, preferring | ||
// the most specific paths | ||
for (var i = segs.length; i > 0; i--) { | ||
var mp = segs.slice(0, i).join('/') | ||
, p = paths[mp]; | ||
if (p) { | ||
var rem = id.slice(mp.length); | ||
if (typeof p === 'string') { | ||
ids = [ p + rem ]; | ||
} else if (Array.isArray(p)) { | ||
ids = p.map(function(pi) { return pi + rem }); | ||
} | ||
break; | ||
} | ||
} | ||
for (var i = 0, len = ids.length; i < len; i++) { | ||
@@ -147,0 +172,0 @@ var mid = ids[i]; |
{ | ||
"name": "amd-resolve", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "A hookable AMD module resolution implementation.", | ||
@@ -13,3 +13,7 @@ "keywords": ["modules", "require", "resolve", "amd", "requirejs"], | ||
}, | ||
"author": { "name": "Jared Hanson", "email": "jaredhanson@gmail.com", "url": "http://www.jaredhanson.net/" }, | ||
"author": { | ||
"name": "Jared Hanson", | ||
"email": "jaredhanson@gmail.com", | ||
"url": "http://www.jaredhanson.net/" | ||
}, | ||
"licenses": [ { | ||
@@ -26,6 +30,6 @@ "type": "MIT", | ||
}, | ||
"engines": { "node": ">= 0.4.0" }, | ||
"scripts": { | ||
"test": "NODE_PATH=lib node_modules/.bin/mocha" | ||
}, | ||
"engines": { "node": ">= 0.4.0" } | ||
"test": "node_modules/.bin/mocha --reporter spec --require should" | ||
} | ||
} |
@@ -13,3 +13,3 @@ # amd-resolve | ||
## Installation | ||
## Install | ||
@@ -27,3 +27,3 @@ $ npm install amd-resolve | ||
$ npm install --dev | ||
$ npm install | ||
$ make test | ||
@@ -39,21 +39,4 @@ | ||
(The MIT License) | ||
[The MIT License](http://opensource.org/licenses/MIT) | ||
Copyright (c) 2012 Jared Hanson | ||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
Copyright (c) 2012-2013 Jared Hanson <[http://jaredhanson.net/](http://jaredhanson.net/)> |
Sorry, the diff of this file is not supported yet
164
12.33%8719
-5.86%40
-29.82%