From a60f7d80238c1bbc4876a722f9e93c69c915bdb1 Mon Sep 17 00:00:00 2001 From: Joel Kemp Date: Tue, 10 May 2016 14:40:51 -0400 Subject: [PATCH] Webpack support Ref https://github.com/mrjoelkemp/node-dependency-tree/issues/27 --- .jscsrc | 2 +- index.js | 61 ++++++++++++++++++++++++++++++++++++----------- package.json | 11 +++++---- readme.md | 8 ++++--- test/test.js | 41 +++++++++++++++++++++++++------ webpack.config.js | 8 +++++++ 6 files changed, 101 insertions(+), 30 deletions(-) create mode 100644 webpack.config.js diff --git a/.jscsrc b/.jscsrc index a663e1f..f892ba4 100644 --- a/.jscsrc +++ b/.jscsrc @@ -1,3 +1,3 @@ { -"preset": "google" + "preset": "mrjoelkemp" } \ No newline at end of file diff --git a/index.js b/index.js index d9e2f76..c02b6c7 100644 --- a/index.js +++ b/index.js @@ -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 = {}; @@ -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); @@ -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; }; @@ -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'); @@ -95,6 +102,8 @@ function jsLookup(partial, filename, directory, config) { } /** + * TODO: Export to a separate module + * * @private * @param {String} partial * @param {String} filename @@ -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 ''; +} diff --git a/package.json b/package.json index 465b852..2bb2352 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", @@ -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", diff --git a/readme.md b/readme.md index f7773e9..d92ab5c 100644 --- a/readme.md +++ b/readme.md @@ -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 diff --git a/test/test.js b/test/test.js index ff9b0eb..130d0fe 100644 --- a/test/test.js +++ b/test/test.js @@ -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(); @@ -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' + ) ); }); }); @@ -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'); + }); + }); }); diff --git a/webpack.config.js b/webpack.config.js new file mode 100644 index 0000000..c15be29 --- /dev/null +++ b/webpack.config.js @@ -0,0 +1,8 @@ +module.exports = { + entry: "./index.js", + resolve: { + alias: { + R: './node_modules/resolve' + } + } +}; \ No newline at end of file