Skip to content
This repository has been archived by the owner on Jun 18, 2019. It is now read-only.

Commit

Permalink
feat(ConfigPathResolver): adds support of webpack-config-<name> nam…
Browse files Browse the repository at this point in the history
…ing convention
  • Loading branch information
mdreizin committed Jun 29, 2016
1 parent 154cfad commit 6adc77b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
8 changes: 7 additions & 1 deletion src/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,13 @@ class Config {
* @example
* import Config from 'webpack-config';
*
* export default new Config().extend('npm-module-name/webpack.config.js');
* // Loads from `node_modules/react-redux/webpack.config.js`
* export default new Config().extend('react-redux/webpack.config.js');
* @example
* import Config from 'webpack-config';
*
* // Loads from `node_modules/webpack-config-my/webpack.config.js`
* export default new Config().extend('my/webpack.config.js');
* @example
* import Config from 'webpack-config';
*
Expand Down
47 changes: 39 additions & 8 deletions src/ConfigPathResolver.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import {
isError
} from 'lodash';
import {
resolve
} from 'path';
Expand All @@ -10,18 +13,43 @@ import ConfigRegistry from './ConfigRegistry';
*/
const NAME_RESOLVER = new WeakMap();

/**
* @private
* @type {String}
*/
const MODULE_PREFIX = 'webpack-config';

/* eslint-disable valid-jsdoc */
/**
* @private
* @type {Function[]}
*/
const DEFAULT_RESOLVERS = [
/**
* `require('<npm-module-name>')`
* `require('<module-name>')`
* @param {String} filename
* @returns {String}
* @returns {String|Error}
*/
filename => require.resolve(filename),
filename => {
try {
return require.resolve(filename);
} catch (err) {
return err;
}
},

/**
* `require('webpack-config-<name>')`
* @param {String} filename
* @returns {String|Error}
*/
filename => {
try {
return require.resolve(`${MODULE_PREFIX}-${filename}`);
} catch (err) {
return err;
}
},

/**
* `path.resolve('<file-name>')`
Expand Down Expand Up @@ -75,11 +103,14 @@ class ConfigPathResolver {
resolvePath(filename) {
filename = this.nameResolver.resolveName(filename);

this.resolvers.forEach(resolver => {
try {
filename = resolver(filename);
} catch (e) {} // eslint-disable-line no-empty
});
for (const resolver of this.resolvers) {
const value = resolver(filename);

if (!isError(value)) {
filename = value;
break;
}
}

return filename;
}
Expand Down

0 comments on commit 6adc77b

Please sign in to comment.