Skip to content

Commit 595de75

Browse files
authored
Merge pull request webpack#3007 from asarode/master
Support multiple config compiler flags (webpack#2835)
2 parents b3e901d + 19c7395 commit 595de75

File tree

1 file changed

+51
-29
lines changed

1 file changed

+51
-29
lines changed

bin/convert-argv.js

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var validateWebpackOptions = require("../lib/validateWebpackOptions");
88

99
module.exports = function(optimist, argv, convertOptions) {
1010

11-
var options = {};
11+
var options = [];
1212

1313
// Help
1414
if(argv.help) {
@@ -36,11 +36,11 @@ module.exports = function(optimist, argv, convertOptions) {
3636
}
3737

3838
var configFileLoaded = false;
39-
var configPath, ext;
39+
var configFiles = [];
4040
var extensions = Object.keys(interpret.extensions).sort(function(a, b) {
4141
return a === '.js' ? -1 : b === '.js' ? 1 : a.length - b.length;
4242
});
43-
var configFiles = ["webpack.config", "webpackfile"].map(function(filename) {
43+
var defaultConfigFiles = ["webpack.config", "webpackfile"].map(function(filename) {
4444
return extensions.map(function(ext) {
4545
return {
4646
path: path.resolve(filename + ext),
@@ -53,30 +53,41 @@ module.exports = function(optimist, argv, convertOptions) {
5353

5454
var i;
5555
if(argv.config) {
56-
configPath = path.resolve(argv.config);
57-
for(i = extensions.length - 1; i >= 0; i--) {
58-
var tmpExt = extensions[i];
59-
if(configPath.indexOf(tmpExt, configPath.length - tmpExt.length) > -1) {
60-
ext = tmpExt;
61-
break;
56+
function getConfigExtension(configPath) {
57+
for(i = extensions.length - 1; i >= 0; i--) {
58+
var tmpExt = extensions[i];
59+
if(configPath.indexOf(tmpExt, configPath.length - tmpExt.length) > -1) {
60+
return tmpExt;
61+
}
6262
}
63+
return path.extname(configPath);
6364
}
64-
if(!ext) {
65-
ext = path.extname(configPath);
65+
66+
function mapConfigArg(configArg) {
67+
var resolvedPath = path.resolve(configArg);
68+
var extension = getConfigExtension(resolvedPath);
69+
return {
70+
path: resolvedPath,
71+
ext: extension
72+
};
6673
}
74+
75+
var configArgList = Array.isArray(argv.config) ? argv.config : [argv.config];
76+
configFiles = configArgList.map(mapConfigArg);
6777
} else {
68-
for(i = 0; i < configFiles.length; i++) {
69-
var webpackConfig = configFiles[i].path;
78+
for(i = 0; i < defaultConfigFiles.length; i++) {
79+
var webpackConfig = defaultConfigFiles[i].path;
7080
if(fs.existsSync(webpackConfig)) {
71-
ext = configFiles[i].ext;
72-
configPath = webpackConfig;
81+
configFiles.push({
82+
path: webpackConfig,
83+
ext: defaultConfigFiles[i].ext
84+
});
7385
break;
7486
}
7587
}
7688
}
7789

78-
if(configPath) {
79-
90+
if(configFiles.length > 0) {
8091
function registerCompiler(moduleDescriptor) {
8192
if(moduleDescriptor) {
8293
if(typeof moduleDescriptor === "string") {
@@ -96,22 +107,33 @@ module.exports = function(optimist, argv, convertOptions) {
96107
}
97108
}
98109

99-
registerCompiler(interpret.extensions[ext]);
100-
options = require(configPath);
110+
function requireConfig(configPath) {
111+
var options = require(configPath);
112+
var isES6DefaultExportedFunc = (
113+
typeof options === "object" && options !== null && typeof options.default === "function"
114+
);
115+
if(typeof options === "function" || isES6DefaultExportedFunc) {
116+
options = isES6DefaultExportedFunc ? options.default : options;
117+
options = options(argv.env, argv);
118+
}
119+
return options;
120+
}
121+
122+
configFiles.forEach(function(file) {
123+
registerCompiler(interpret.extensions[file.ext]);
124+
options.push(requireConfig(file.path));
125+
});
101126
configFileLoaded = true;
102127
}
103128

104-
var isES6DefaultExportedFunc = (
105-
typeof options === "object" && options !== null && typeof options.default === "function"
106-
);
107-
108-
if(typeof options === "function" || isES6DefaultExportedFunc) {
109-
options = isES6DefaultExportedFunc ? options.default : options;
110-
options = options(argv.env, argv);
129+
if(!configFileLoaded) {
130+
return processConfiguredOptions({});
131+
} else if(options.length === 1) {
132+
return processConfiguredOptions(options[0]);
133+
} else {
134+
return processConfiguredOptions(options);
111135
}
112136

113-
return processConfiguredOptions(options);
114-
115137
function processConfiguredOptions(options) {
116138
if(options === null || typeof options !== "object") {
117139
console.error("Config did not export an object or a function returning an object.");
@@ -543,7 +565,7 @@ module.exports = function(optimist, argv, convertOptions) {
543565
}
544566

545567
if(!options.entry) {
546-
if(configPath) {
568+
if(configFileLoaded) {
547569
console.error("Configuration file found but no entry configured.");
548570
} else {
549571
console.error("No configuration file found and no entry configured via CLI option.");

0 commit comments

Comments
 (0)