Skip to content

Commit

Permalink
Handle resolve.modules supported by webpack2
Browse files Browse the repository at this point in the history
  • Loading branch information
pahen committed Jan 7, 2017
1 parent 47c91f1 commit 93f6e26
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
14 changes: 4 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,9 @@ function resolveWebpackPath(partial, filename, directory, webpackConfig) {
return '';
}

try {
var resolveConfig = objectAssign({}, loadedConfig.resolve);
var resolveConfig = objectAssign({}, loadedConfig.resolve);

if (!resolveConfig.modules && (resolveConfig.root || resolveConfig.modulesDirectories)) {
resolveConfig.modules = [];

if (resolveConfig.root) {
Expand All @@ -204,15 +204,9 @@ function resolveWebpackPath(partial, filename, directory, webpackConfig) {
if (resolveConfig.modulesDirectories) {
resolveConfig.modules = resolveConfig.modules.concat(resolveConfig.modulesDirectories);
}
}

var foundNodeModulesInPaths = resolveConfig.modules.some(function(dir) {
return dir.match('node_modules');
});

if (!foundNodeModulesInPaths) {
resolveConfig.modules.push('node_modules');
}

try {
var resolver = webpackResolve.create.sync(resolveConfig);

// We don't care about what the loader resolves the partial to
Expand Down
11 changes: 11 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,17 @@ describe('filing-cabinet', function() {
assert.equal(resolved, `${directory}/node_modules/resolve/index.js`);
});

it('resolves NPM module when using resolve.modulesDirectories', function() {
const resolved = cabinet({
partial: 'resolve',
filename: `${directory}/index.js`,
directory,
webpackConfig: `${directory}/webpack-root.config.js`
});

assert.equal(resolved, `${directory}/node_modules/resolve/index.js`);
});

it('resolves a path using resolve.modulesDirectories', function() {
const resolved = cabinet({
partial: 'mod2',
Expand Down
5 changes: 3 additions & 2 deletions webpack-root.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ var path = require('path');
module.exports = {
entry: "./index.js",
resolve: {
modulesDirectories: ['test/root1'],
modulesDirectories: ['test/root1', 'node_modules'],
root: [
path.resolve(__dirname, './test/root2')
path.resolve(__dirname, './test/root2'),
path.resolve(__dirname, './node_modules')
]
}
};

0 comments on commit 93f6e26

Please sign in to comment.