module-lookup-amd
Advanced tools
Comparing version 1.0.0 to 2.0.0
42
index.js
@@ -1,3 +0,4 @@ | ||
var ConfigFile = require('requirejs-config-file').ConfigFile, | ||
path = require('path'); | ||
var ConfigFile = require('requirejs-config-file').ConfigFile; | ||
var path = require('path'); | ||
var normalize = require('./lib/normalize'); | ||
@@ -10,5 +11,7 @@ /** | ||
* @param {String} depPath | ||
* @param {String} filepath - the file containing the dependency | ||
* | ||
* @return {String} | ||
*/ | ||
module.exports = function(config, depPath) { | ||
module.exports = function(config, depPath, filepath) { | ||
var configPath; | ||
@@ -21,33 +24,18 @@ | ||
var baseUrl = configPath || config.baseUrl, | ||
pathTokens = depPath.split('/'), | ||
topLevelDir = pathTokens[0], | ||
exclamationLocation, alias; | ||
if (config.baseUrl[config.baseUrl.length - 1] !== '/') { | ||
config.baseUrl = config.baseUrl + '/'; | ||
} | ||
// Uses a plugin loader | ||
if ((exclamationLocation = topLevelDir.indexOf('!')) !== -1) { | ||
topLevelDir = topLevelDir.slice(exclamationLocation + 1); | ||
if ((exclamationLocation = depPath.indexOf('!')) !== -1) { | ||
depPath = depPath.slice(exclamationLocation + 1); | ||
} | ||
// Check if the top-most dir of path is an alias | ||
alias = config.paths[topLevelDir]; | ||
var normalized = normalize(depPath, filepath || '', config); | ||
if (alias) { | ||
// Handle alias values that are relative paths (about the baseUrl) | ||
if (alias.indexOf('..') === 0 && baseUrl) { | ||
// Get the resolved path of the baseURL from the depPath | ||
configPath = configPath || depPath.slice(0, depPath.indexOf(baseUrl) + baseUrl.length); | ||
var filepathWithoutBase = filepath.split(config.baseUrl)[0]; | ||
alias = path.resolve(configPath, alias); | ||
} | ||
normalized = path.join(filepathWithoutBase, normalized); | ||
alias = alias[alias.length - 1] === '/' ? alias : alias + '/'; | ||
depPath = alias + pathTokens.slice(1).join('/'); | ||
} | ||
// Normalize trailing slash | ||
depPath = depPath[depPath.length - 1] === '/' ? depPath.slice(0, -1) : depPath; | ||
return depPath; | ||
return normalized; | ||
}; |
{ | ||
"name": "module-lookup-amd", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "Resolve aliased dependency paths using a RequireJS config", | ||
@@ -9,2 +9,5 @@ "main": "index.js", | ||
}, | ||
"bin": { | ||
"lookup-amd": "bin/cli.js" | ||
}, | ||
"repository": { | ||
@@ -27,2 +30,3 @@ "type": "git", | ||
"dependencies": { | ||
"commander": "~2.8.1", | ||
"requirejs-config-file": "~2.0.0" | ||
@@ -29,0 +33,0 @@ }, |
@@ -1,5 +0,9 @@ | ||
### module-lookup-amd | ||
### module-lookup-amd [![npm](http://img.shields.io/npm/v/module-lookup-amd.svg)](https://npmjs.org/package/module-lookup-amd) [![npm](http://img.shields.io/npm/dm/module-lookup-amd.svg)](https://npmjs.org/package/module-lookup-amd) | ||
Gives you the real path of (possibly) aliased modules. Otherwise, gives you back the same dependency name if it's not aliased. | ||
This module basically exposes the requirejs config map and path resolution logic | ||
and gives you back the real, absolute, path of (possibly) aliased modules names. | ||
I built this for [Dependents'](https://sublime.wbond.net/packages/Dependents) [jump to dependency](https://github.com/mrjoelkemp/Dependents#jump-to-a-dependency) feature that lets you click on a module name | ||
and open the file that name resolves to. | ||
`npm install module-lookup-amd` | ||
@@ -13,8 +17,15 @@ | ||
var realPath = lookup('path/to/my/config.js', 'dependency/path'); | ||
var realPath = lookup('path/to/my/config.js', 'dependency/path', 'path/to/file/containing/dependency'); | ||
``` | ||
### `lookup(configPath, dependencyPath)` | ||
### `lookup(configPath, dependencyPath, filepath)` | ||
* `configPath`: the path to your RequireJS configuration file | ||
* `dependencyPath`: the (potentially aliased) dependency that you want to lookup | ||
* `filepath`: the filepath of the file that contains the dependency (i.e., parent module) | ||
### Shell usage | ||
*Assumes a global `-g` installation* | ||
`lookup-amd -c path/to/my/config.js -f path/to/file/containing/dependency my/dependency/name` |
require.config({ | ||
"baseUrl": "/js", | ||
"paths": { | ||
"a": "./a", | ||
"foobar": "./b", | ||
"templates": "./templates" | ||
"a": "foo/a", | ||
"foobar": "foo/bar/b", | ||
"templates": "../templates" | ||
}, | ||
"map": { | ||
"poet": { | ||
"poet/templates": "templates/poet" | ||
} | ||
} | ||
}); |
@@ -1,11 +0,13 @@ | ||
var assert = require('assert'), | ||
path = require('path'), | ||
ConfigFile = require('requirejs-config-file').ConfigFile, | ||
lookup = require('../'); | ||
var assert = require('assert'); | ||
var path = require('path'); | ||
var ConfigFile = require('requirejs-config-file').ConfigFile; | ||
var lookup = require('../'); | ||
var dir = '/path/from/my/machine/js'; | ||
var filename = dir + '/poet/Remote.js'; | ||
var configPath = __dirname + '/example/config.json'; | ||
describe('lookup', function() { | ||
var configPath = __dirname + '/example/config.json'; | ||
it('returns the real path of an aliased module given a path to a requirejs config file', function() { | ||
assert(lookup(configPath, 'a') === './a'); | ||
assert.equal(lookup(configPath, 'a', filename), path.join(dir, 'foo/a')); | ||
}); | ||
@@ -15,8 +17,26 @@ | ||
var configObject = new ConfigFile(configPath).read(); | ||
assert(lookup(configObject, 'foobar') === './b'); | ||
assert.equal(lookup(configObject, 'foobar', filename), path.join(dir, 'foo/bar/b')); | ||
}); | ||
it('supports paths that use plugin loaders', function() { | ||
assert(lookup(configPath, 'hgn!templates/a') === './templates/a'); | ||
assert.equal(lookup(configPath, 'hgn!templates/a', filename), path.join(dir, '../templates/a')); | ||
}); | ||
it('supports relative plugin loader paths', function() { | ||
// templates should path lookup to ../templates | ||
assert.equal(lookup(configPath, 'hgn!./templates/a', filename), path.join(dir, '../templates/poet/a')); | ||
assert.equal(lookup(configPath, 'text!./templates/a.mustache', filename), path.join(dir, '../templates/poet/a.mustache')); | ||
}); | ||
it('supports map aliasing', function() { | ||
assert.equal(lookup(configPath, 'hgn!./templates/_icons/_embed', filename), path.join(dir, '../templates/poet/_icons/_embed')); | ||
}); | ||
it('supports relative pathing', function() { | ||
assert.equal(lookup(configPath, 'hgn!./templates/_icons/_embed', filename), path.join(dir, '../templates/poet/_icons/_embed')); | ||
}); | ||
it('returns the same dependency if not aliased', function() { | ||
assert.equal(lookup(configPath, 'my/sweet/path', filename), path.join(dir, 'my/sweet/path')); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13013
13
280
31
2
1
+ Addedcommander@~2.8.1
+ Addedcommander@2.8.1(transitive)
+ Addedgraceful-readlink@1.0.1(transitive)