module-lookup-amd
Advanced tools
Comparing version 2.0.5 to 3.0.0
23
index.js
@@ -10,21 +10,22 @@ var ConfigFile = require('requirejs-config-file').ConfigFile; | ||
* | ||
* @param {String|Object} config - Pass a loaded config object if you'd like to avoid rereading the config | ||
* @param {String} depPath - the dependency name | ||
* @param {String} filepath - the file containing the dependency | ||
* @param {String} [directory] - location of all files | ||
* @param {Object} options - Pass a loaded config object if you'd like to avoid rereading the config | ||
* @param {String|Object} [options.config] - Pass a loaded config object if you'd like to avoid rereading the config | ||
* @param {String} options.partial - the dependency name | ||
* @param {String} options.filename - the file containing the dependency | ||
* @param {String} [options.directory] - location of all files | ||
* | ||
* @return {String} | ||
*/ | ||
module.exports = function(config, depPath, filepath, directory) { | ||
module.exports = function(options) { | ||
var configPath; | ||
var config = options.config || {}; | ||
var depPath = options.partial; | ||
var filepath = options.filename; | ||
var directory = options.directory; | ||
debug('given config: ', config); | ||
debug('given depPath: ', depPath); | ||
debug('given filepath: ', filepath); | ||
debug('given partial: ', depPath); | ||
debug('given filename: ', filepath); | ||
debug('given directory: ', directory); | ||
if (typeof config === 'undefined') { | ||
throw new Error('requirejs config missing'); | ||
} | ||
if (typeof config === 'string') { | ||
@@ -31,0 +32,0 @@ configPath = path.dirname(config); |
@@ -194,3 +194,3 @@ var debug = require('debug')('lookup'); | ||
//and work up from it. | ||
for (i = syms.length; i > 0; i -= 1) { | ||
for (var i = syms.length; i > 0; i -= 1) { | ||
var parentModule = syms.slice(0, i).join('/'); | ||
@@ -197,0 +197,0 @@ var parentPath = getOwn(paths, parentModule); |
{ | ||
"name": "module-lookup-amd", | ||
"version": "2.0.5", | ||
"version": "3.0.0", | ||
"description": "Resolve aliased dependency paths using a RequireJS config", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "jscs -p airbnb index.js test bin && mocha" | ||
"test": "jscs index.js test bin && ./node_modules/.bin/mocha --compilers js:babel/register test/test.js" | ||
}, | ||
@@ -34,4 +34,6 @@ "bin": { | ||
"devDependencies": { | ||
"jscs": "~1.13.1", | ||
"mocha": "^1.21.4", | ||
"babel": "~5.8.38", | ||
"jscs": "~2.11.0", | ||
"jscs-preset-mrjoelkemp": "~1.0.0", | ||
"mocha": "~2.4.5", | ||
"rewire": "~2.3.4", | ||
@@ -38,0 +40,0 @@ "sinon": "~1.15.4" |
@@ -16,10 +16,13 @@ ### 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) | ||
var realPath = lookup('path/to/my/config.js', 'dependency/path', 'path/to/file/containing/dependency'); | ||
var realPath = lookup({ | ||
config: 'path/to/my/requirejs/config', // optional | ||
partial: 'someModule', | ||
filename: 'file/containing/partial', | ||
directory: 'directory/containing/all/js/files' | ||
}); | ||
``` | ||
### `lookup(configPath, dependencyPath, filepath, directory)` | ||
* `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) | ||
* `config`: the path to your RequireJS configuration file | ||
* `partial`: the (potentially aliased) dependency that you want to lookup | ||
* `filename`: the path of the file that contains the dependency (i.e., parent module) | ||
* `directory`: (Optional) the path to all files | ||
@@ -26,0 +29,0 @@ * Used as a last resort for resolving paths from files that are not within the config's base url (like test files that import a module) |
137
test/test.js
@@ -9,24 +9,36 @@ var assert = require('assert'); | ||
var dir; | ||
var directory; | ||
var filename; | ||
var configPath; | ||
var config; | ||
describe('lookup', function() { | ||
beforeEach(function() { | ||
dir = '/path/from/my/machine/js'; | ||
filename = dir + '/poet/Remote.js'; | ||
configPath = __dirname + '/example/config.json'; | ||
directory = '/path/from/my/machine/js'; | ||
filename = directory + '/poet/Remote.js'; | ||
config = __dirname + '/example/config.json'; | ||
}); | ||
it('returns the real path of an aliased module given a path to a requirejs config file', function() { | ||
assert.equal(lookup(configPath, 'a', filename), path.join(dir, 'foo/a')); | ||
assert.equal(lookup({ | ||
config, | ||
partial: 'a', | ||
filename | ||
}), path.join(directory, 'foo/a')); | ||
}); | ||
it('returns the looked up path given a loaded requirejs config object', function() { | ||
var configObject = new ConfigFile(configPath).read(); | ||
assert.equal(lookup(configObject, 'foobar', filename), path.join(dir, 'foo/bar/b')); | ||
var configObject = new ConfigFile(config).read(); | ||
assert.equal(lookup({ | ||
config: configObject, | ||
partial: 'foobar', | ||
filename | ||
}), path.join(directory, 'foo/bar/b')); | ||
}); | ||
it('supports paths that use plugin loaders', function() { | ||
assert.equal(lookup(configPath, 'hgn!templates/a', filename), path.join(dir, '../templates/a')); | ||
assert.equal(lookup({ | ||
config, | ||
partial: 'hgn!templates/a', | ||
filename | ||
}), path.join(directory, '../templates/a')); | ||
}); | ||
@@ -36,36 +48,58 @@ | ||
// 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')); | ||
assert.equal(lookup({ | ||
config, | ||
partial: 'hgn!./templates/a', | ||
filename | ||
}), path.join(directory, '../templates/poet/a')); | ||
assert.equal(lookup({ | ||
config, | ||
partial: 'text!./templates/a.mustache', | ||
filename | ||
}), path.join(directory, '../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')); | ||
assert.equal(lookup({ | ||
config, | ||
partial: 'hgn!./templates/_icons/_embed', | ||
filename | ||
}), path.join(directory, '../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')); | ||
assert.equal(lookup({ | ||
config, | ||
partial: 'hgn!./templates/_icons/_embed', | ||
filename | ||
}), path.join(directory, '../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')); | ||
assert.equal(lookup({ | ||
config, | ||
partial: 'my/sweet/path', | ||
filename | ||
}), path.join(directory, 'my/sweet/path')); | ||
}); | ||
it('throws if the config is missing', function() { | ||
assert.throws(function() { | ||
lookup(undefined, 'foobar', filename); | ||
it('does not throw if the config is missing', function() { | ||
assert.doesNotThrow(function() { | ||
lookup({ | ||
partial: 'foobar', | ||
filename | ||
}); | ||
}); | ||
}); | ||
it('throws if the config is not a string or object', function() { | ||
assert.throws(function() { | ||
lookup(null, 'foobar', filename); | ||
}); | ||
}); | ||
it('does not throw if the baseUrl is missing', function() { | ||
var configObject = new ConfigFile(configPath).read(); | ||
var configObject = new ConfigFile(config).read(); | ||
delete configObject.baseUrl; | ||
assert.doesNotThrow(function() { | ||
lookup(configObject, 'foobar', filename); | ||
lookup({ | ||
config: configObject, | ||
partial: 'foobar', | ||
filename | ||
}); | ||
}); | ||
@@ -75,7 +109,11 @@ }); | ||
it('does not throw if config.map is missing', function() { | ||
var configObject = new ConfigFile(configPath).read(); | ||
var configObject = new ConfigFile(config).read(); | ||
delete configObject.map; | ||
assert.doesNotThrow(function() { | ||
lookup(configObject, 'foobar', filename); | ||
lookup({ | ||
config: configObject, | ||
partial: 'foobar', | ||
filename | ||
}); | ||
}); | ||
@@ -85,7 +123,11 @@ }); | ||
it('does not throw if config.paths is missing', function() { | ||
var configObject = new ConfigFile(configPath).read(); | ||
var configObject = new ConfigFile(config).read(); | ||
delete configObject.paths; | ||
assert.doesNotThrow(function() { | ||
lookup(configObject, 'foobar', filename); | ||
lookup({ | ||
config: configObject, | ||
partial: 'foobar', | ||
filename | ||
}); | ||
}); | ||
@@ -101,6 +143,10 @@ }); | ||
lookup(configPath, 'foobar', filename); | ||
lookup({ | ||
config, | ||
partial: 'foobar', | ||
filename | ||
}); | ||
// Add the slash since we normalize the slash during the lookup | ||
assert.equal(stub.args[0][2].baseUrl, path.dirname(configPath) + '/'); | ||
assert.equal(stub.args[0][2].baseUrl, path.dirname(config) + '/'); | ||
revert(); | ||
@@ -110,3 +156,3 @@ }); | ||
it('defaults to ./ if the config was a reused object', function() { | ||
var configObject = new ConfigFile(configPath).read(); | ||
var configObject = new ConfigFile(config).read(); | ||
delete configObject.baseUrl; | ||
@@ -118,3 +164,7 @@ | ||
lookup(configObject, 'foobar', filename); | ||
lookup({ | ||
config: configObject, | ||
partial: 'foobar', | ||
filename | ||
}); | ||
@@ -129,14 +179,27 @@ assert.equal(stub.args[0][2].baseUrl, './'); | ||
assert.doesNotThrow(function() { | ||
lookup(configPath, 'my/sweet/path', '/some/random/folder/foo.js'); | ||
}) | ||
lookup({ | ||
config, | ||
partial: 'my/sweet/path', | ||
filename: '/some/random/folder/foo.js' | ||
}); | ||
}); | ||
}); | ||
it('returns an empty string if a directory is not given', function() { | ||
assert.equal(lookup(configPath, 'my/sweet/path', '/some/random/folder/foo.js'), ''); | ||
assert.equal(lookup({ | ||
config, | ||
partial: 'my/sweet/path', | ||
filename: '/some/random/folder/foo.js' | ||
}), ''); | ||
}); | ||
it('returns the normalized path about the given directory and the base url', function() { | ||
assert.equal(lookup(configPath, 'my/sweet/path', '/some/random/folder/foo.js', '/some/random/folder/'), '/some/random/folder/my/sweet/path'); | ||
assert.equal(lookup({ | ||
config, | ||
partial: 'my/sweet/path', | ||
filename: '/some/random/folder/foo.js', | ||
directory: '/some/random/folder/' | ||
}), '/some/random/folder/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
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
18988
14
466
35
6