Skip to content

Commit 77ec1fd

Browse files
committed
Support multiple config compiler flags
1 parent 17ca14c commit 77ec1fd

1 file changed

Lines changed: 51 additions & 28 deletions

File tree

bin/convert-argv.js

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ module.exports = function(optimist, argv, convertOptions) {
3535
}
3636

3737
var configFileLoaded = false;
38-
var configPath, ext;
38+
var configFile = {};
3939
var extensions = Object.keys(interpret.extensions).sort(function(a, b) {
4040
return a === '.js' ? -1 : b === '.js' ? 1 : a.length - b.length;
4141
});
42-
var configFiles = ["webpack.config", "webpackfile"].map(function(filename) {
42+
var defaultConfigFiles = ["webpack.config", "webpackfile"].map(function(filename) {
4343
return extensions.map(function(ext) {
4444
return {
4545
path: path.resolve(filename + ext),
@@ -52,30 +52,41 @@ module.exports = function(optimist, argv, convertOptions) {
5252

5353
var i;
5454
if(argv.config) {
55-
configPath = path.resolve(argv.config);
56-
for(i = extensions.length - 1; i >= 0; i--) {
57-
var tmpExt = extensions[i];
58-
if(configPath.indexOf(tmpExt, configPath.length - tmpExt.length) > -1) {
59-
ext = tmpExt;
60-
break;
55+
function getConfigExtension(configPath) {
56+
for(i = extensions.length - 1; i >= 0; i--) {
57+
var tmpExt = extensions[i];
58+
if(configPath.indexOf(tmpExt, configPath.length - tmpExt.length) > -1) {
59+
return tmpExt;
60+
break;
61+
}
6162
}
63+
return path.extname(configPath);
6264
}
63-
if(!ext) {
64-
ext = path.extname(configPath);
65+
if(Array.isArray(argv.config)) {
66+
configFile = argv.config.map(function(configArg) {
67+
var resolvedPath = path.resolve(configArg);
68+
var extension = getConfigExtension(resolvedPath);
69+
return {
70+
path: resolvedPath,
71+
ext: extension
72+
};
73+
});
74+
} else {
75+
configFile.path = path.resolve(argv.config);
76+
configFile.ext = getConfigExtension(configFile.path);
6577
}
6678
} else {
67-
for(i = 0; i < configFiles.length; i++) {
68-
var webpackConfig = configFiles[i].path;
79+
for(i = 0; i < defaultConfigFiles.length; i++) {
80+
var webpackConfig = defaultConfigFiles[i].path;
6981
if(fs.existsSync(webpackConfig)) {
70-
ext = configFiles[i].ext;
71-
configPath = webpackConfig;
82+
configFile.ext = defaultConfigFiles[i].ext;
83+
configFile.path = webpackConfig;
7284
break;
7385
}
7486
}
7587
}
7688

77-
if(configPath) {
78-
89+
if(configFile) {
7990
function registerCompiler(moduleDescriptor) {
8091
if(moduleDescriptor) {
8192
if(typeof moduleDescriptor === "string") {
@@ -95,18 +106,30 @@ module.exports = function(optimist, argv, convertOptions) {
95106
}
96107
}
97108

98-
registerCompiler(interpret.extensions[ext]);
99-
options = require(configPath);
100-
configFileLoaded = true;
101-
}
102-
103-
var isES6DefaultExportedFunc = (
104-
typeof options === "object" && options !== null && typeof options.default === "function"
105-
);
109+
function requireConfig(configPath) {
110+
var options = require(configPath);
111+
var isES6DefaultExportedFunc = (
112+
typeof options === "object" && options !== null && typeof options.default === "function"
113+
);
114+
if(typeof options === "function" || isES6DefaultExportedFunc) {
115+
options = isES6DefaultExportedFunc ? options.default : options;
116+
options = options(argv.env, argv);
117+
}
118+
return options;
119+
}
106120

107-
if(typeof options === "function" || isES6DefaultExportedFunc) {
108-
options = isES6DefaultExportedFunc ? options.default : options;
109-
options = options(argv.env, argv);
121+
if(Array.isArray(configFile)) {
122+
options = [];
123+
configFile.forEach(function(file) {
124+
registerCompiler(interpret.extensions[file.ext]);
125+
options.push(requireConfig(file.path));
126+
});
127+
configFileLoaded = true;
128+
} else {
129+
registerCompiler(interpret.extensions[configFile.ext]);
130+
options = requireConfig(configFile.path);
131+
configFileLoaded = true;
132+
}
110133
}
111134

112135
return processConfiguredOptions(options);
@@ -543,7 +566,7 @@ module.exports = function(optimist, argv, convertOptions) {
543566
}
544567

545568
if(!options.entry) {
546-
if(configPath) {
569+
if(configFile.path) {
547570
console.error("Configuration file found but no entry configured.");
548571
} else {
549572
console.error("No configuration file found and no entry configured via CLI option.");

0 commit comments

Comments
 (0)