Skip to content

Commit

Permalink
Webpack support
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjoelkemp committed May 10, 2016
1 parent 620ceec commit a60f7d8
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .jscsrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"preset": "google"
"preset": "mrjoelkemp"
}
61 changes: 47 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,9 @@ var sassLookup = require('sass-lookup');
var resolveDependencyPath = require('resolve-dependency-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];
}
}

return obj1;
};
var webpackResolve = require('enhanced-resolve');

var defaultLookups = {};

Expand All @@ -37,6 +30,7 @@ module.exports = function(options) {
var filename = options.filename;
var directory = options.directory;
var config = options.config;
var webpackConfig = options.webpackConfig;

var ext = path.extname(filename);

Expand All @@ -49,7 +43,7 @@ module.exports = function(options) {

debug('found a resolver for ' + ext);

var result = resolver(partial, filename, directory, config);
var result = resolver(partial, filename, directory, config, webpackConfig);
debug('resolved path for ' + partial + ': ' + result);
return result;
};
Expand All @@ -72,21 +66,34 @@ module.exports.register = function(extension, lookupStrategy) {
* @param {String} config
* @return {String}
*/
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) {
case 'amd':
debug('using amd resolver');
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':
default:
debug('using generic resolver for es6');
Expand All @@ -95,6 +102,8 @@ function jsLookup(partial, filename, directory, config) {
}

/**
* TODO: Export to a separate module
*
* @private
* @param {String} partial
* @param {String} filename
Expand Down Expand Up @@ -126,3 +135,27 @@ function commonJSLookup(partial, filename, directory) {

return result;
}

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 '';
}
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@
"bin": {
"filing-cabinet": "bin/cli.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"
},
"repository": {
"type": "git",
Expand All @@ -34,7 +31,9 @@
},
"homepage": "https://github.com/mrjoelkemp/node-filing-cabinet",
"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.7.0",
"rewire": "~2.3.4",
Expand All @@ -44,9 +43,11 @@
"app-module-path": "~1.0.4",
"commander": "~2.8.1",
"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",
"resolve-dependency-path": "~1.0.2",
"sass-lookup": "~1.0.2",
Expand Down
8 changes: 5 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ var result = cabinet({
partial: 'somePartialPath',
directory: 'path/to/all/files',
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
```

* `partial`: the dependency path
* 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

Expand Down
41 changes: 34 additions & 7 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ describe('filing-cabinet', function() {
});
});

afterEach(function() {
mock.restore();
});

describe('es6', function() {
it('uses a generic resolver', function() {
var stub = sinon.stub();
Expand Down Expand Up @@ -155,13 +159,13 @@ describe('filing-cabinet', function() {
});

assert.equal(
result,
path.join(
path.resolve(directory),
'node_modules',
'lodash.assign',
'index.js'
)
result,
path.join(
path.resolve(directory),
'node_modules',
'lodash.assign',
'index.js'
)
);
});
});
Expand Down Expand Up @@ -282,4 +286,27 @@ describe('filing-cabinet', function() {
assert.ok(stub2.called);
});
});

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');
});
});
});
8 changes: 8 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
entry: "./index.js",
resolve: {
alias: {
R: './node_modules/resolve'
}
}
};

0 comments on commit a60f7d8

Please sign in to comment.