filing-cabinet
Advanced tools
Comparing version 1.0.6 to 1.1.0
61
index.js
@@ -14,13 +14,6 @@ var path = require('path'); | ||
var appModulePath = require('app-module-path'); | ||
var assign = require('object-assign'); | ||
var assign = function(obj1, obj2) { | ||
for (var prop in obj2) { | ||
if (obj2.hasOwnProperty(prop)) { | ||
obj1[prop] = obj2[prop]; | ||
} | ||
} | ||
var webpackResolve = require('enhanced-resolve'); | ||
return obj1; | ||
}; | ||
var defaultLookups = {}; | ||
@@ -41,2 +34,3 @@ | ||
var config = options.config; | ||
var webpackConfig = options.webpackConfig; | ||
@@ -54,3 +48,3 @@ var ext = path.extname(filename); | ||
var result = resolver(partial, filename, directory, config); | ||
var result = resolver(partial, filename, directory, config, webpackConfig); | ||
debug('resolved path for ' + partial + ': ' + result); | ||
@@ -78,10 +72,17 @@ return result; | ||
*/ | ||
function jsLookup(partial, filename, directory, config) { | ||
var type = getModuleType.sync(filename); | ||
function jsLookup(partial, filename, directory, config, webpackConfig) { | ||
var type; | ||
// Handle es6 exported to amd via babel | ||
if (type === 'es6' && config) { | ||
if (config) { | ||
type = 'amd'; | ||
} | ||
if (webpackConfig) { | ||
type = 'webpack'; | ||
} | ||
if (!type) { | ||
type = getModuleType.sync(filename); | ||
} | ||
switch (type) { | ||
@@ -91,5 +92,11 @@ case 'amd': | ||
return amdLookup(config, partial, filename, directory); | ||
case 'commonjs': | ||
debug('using commonjs resolver'); | ||
return commonJSLookup(partial, filename, directory); | ||
case 'webpack': | ||
debug('using webpack resolver for es6'); | ||
return resolveWebpackPath(partial, filename, directory, webpackConfig); | ||
case 'es6': | ||
@@ -103,2 +110,4 @@ default: | ||
/** | ||
* TODO: Export to a separate module | ||
* | ||
* @private | ||
@@ -135,1 +144,25 @@ * @param {String} partial | ||
} | ||
function resolveWebpackPath(partial, filename, directory, webpackConfig) { | ||
webpackConfig = path.resolve(webpackConfig); | ||
try { | ||
var loadedConfig = require(webpackConfig); | ||
var aliases = loadedConfig.resolve ? loadedConfig.resolve.alias : []; | ||
var resolver = webpackResolve.create.sync({ | ||
alias: aliases | ||
}); | ||
var resolvedPath = resolver(directory, partial); | ||
return resolvedPath; | ||
} catch (e) { | ||
debug('error loading the webpack config at ' + webpackConfig); | ||
debug(e.message); | ||
debug(e.stack); | ||
} | ||
return ''; | ||
} |
{ | ||
"name": "filing-cabinet", | ||
"version": "1.0.6", | ||
"version": "1.1.0", | ||
"description": "Find files based on partial paths", | ||
@@ -9,7 +9,4 @@ "main": "index.js", | ||
}, | ||
"directories": { | ||
"test": "test" | ||
}, | ||
"scripts": { | ||
"test": "jscs index.js test/test.js && mocha" | ||
"test": "jscs index.js test/test.js && ./node_modules/.bin/mocha --compilers js:babel/register test/test.js" | ||
}, | ||
@@ -38,5 +35,7 @@ "repository": { | ||
"devDependencies": { | ||
"jscs": "~2.0.0", | ||
"babel": "~5.8.38", | ||
"jscs": "~2.11.0", | ||
"jscs-preset-mrjoelkemp": "~1.0.0", | ||
"mocha": "~2.2.5", | ||
"mock-fs": "~3.0.0", | ||
"mock-fs": "~3.7.0", | ||
"rewire": "~2.3.4", | ||
@@ -49,5 +48,7 @@ "sinon": "~1.15.4" | ||
"debug": "~2.2.0", | ||
"enhanced-resolve": "~2.2.2", | ||
"is-relative-path": "~1.0.0", | ||
"module-definition": "~2.2.2", | ||
"module-lookup-amd": "~2.0.4", | ||
"object-assign": "~4.0.1", | ||
"resolve": "~1.1.7", | ||
@@ -54,0 +55,0 @@ "resolve-dependency-path": "~1.0.2", |
@@ -17,6 +17,7 @@ ### filing-cabinet [![npm](http://img.shields.io/npm/v/filing-cabinet.svg)](https://npmjs.org/package/filing-cabinet) [![npm](http://img.shields.io/npm/dm/filing-cabinet.svg)](https://npmjs.org/package/filing-cabinet) | ||
filename: 'path/to/parent/file', | ||
config: 'path/to/requirejs/config' | ||
config: 'path/to/requirejs/config', | ||
webpackConfig: 'path/to/webpack/config' | ||
}); | ||
console.log(result); | ||
console.log(result); // absolute/path/to/somePartialPath | ||
``` | ||
@@ -26,4 +27,5 @@ | ||
* This could be in any of the registered languages | ||
* `config`: (optional) requirejs config for resolving aliased modules | ||
* `webpackConfig`: (optional) webpack config for resolving aliased modules | ||
### Registered languages | ||
@@ -30,0 +32,0 @@ |
@@ -25,2 +25,15 @@ var assert = require('assert'); | ||
'bar.js': 'module.exports = function() {};' | ||
}, | ||
'node_modules': { | ||
'lodash.assign': { | ||
'index.js': 'module.exports = function() {};' | ||
}, | ||
'nested': { | ||
'index.js': 'require("lodash.assign")', | ||
'node_modules': { | ||
'lodash.assign': { | ||
'index.js': 'module.exports = function() {};' | ||
} | ||
} | ||
} | ||
} | ||
@@ -31,2 +44,6 @@ } | ||
afterEach(function() { | ||
mock.restore(); | ||
}); | ||
describe('es6', function() { | ||
@@ -135,2 +152,23 @@ it('uses a generic resolver', function() { | ||
}); | ||
it('resolves a nested module', function() { | ||
var directory = 'js/node_modules/nested/'; | ||
var filename = directory + 'index.js'; | ||
var result = cabinet({ | ||
partial: 'lodash.assign', | ||
filename: filename, | ||
directory: directory | ||
}); | ||
assert.equal( | ||
result, | ||
path.join( | ||
path.resolve(directory), | ||
'node_modules', | ||
'lodash.assign', | ||
'index.js' | ||
) | ||
); | ||
}); | ||
}); | ||
@@ -254,2 +292,25 @@ }); | ||
}); | ||
describe('webpack', function() { | ||
function testResolution(partial) { | ||
const directory = path.resolve(__dirname, '../'); | ||
const resolved = cabinet({ | ||
partial, | ||
filename: `${directory}/index.js`, | ||
directory, | ||
webpackConfig: `${directory}/webpack.config.js` | ||
}); | ||
assert.equal(resolved, `${directory}/node_modules/resolve/index.js`); | ||
} | ||
it('resolves an aliased path', function() { | ||
testResolution('R'); | ||
}); | ||
it('resolves a non-aliased path', function() { | ||
testResolution('resolve'); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
16118
11
405
61
12
7
3
+ Addedenhanced-resolve@~2.2.2
+ Addedobject-assign@~4.0.1
+ Addedcore-util-is@1.0.3(transitive)
+ Addedenhanced-resolve@2.2.2(transitive)
+ Addederrno@0.1.8(transitive)
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedisarray@1.0.0(transitive)
+ Addedmemory-fs@0.3.0(transitive)
+ Addedobject-assign@4.0.1(transitive)
+ Addedprocess-nextick-args@2.0.1(transitive)
+ Addedprr@1.0.1(transitive)
+ Addedreadable-stream@2.3.8(transitive)
+ Addedsafe-buffer@5.1.2(transitive)
+ Addedstring_decoder@1.1.1(transitive)
+ Addedtapable@0.2.9(transitive)
+ Addedutil-deprecate@1.0.2(transitive)