Support multiple config compiler flags

This commit is contained in:
arjun 2016-09-17 22:42:56 -07:00
parent 17ca14ccd9
commit 77ec1fdb71
1 changed files with 51 additions and 28 deletions

View File

@ -35,11 +35,11 @@ module.exports = function(optimist, argv, convertOptions) {
} }
var configFileLoaded = false; var configFileLoaded = false;
var configPath, ext; var configFile = {};
var extensions = Object.keys(interpret.extensions).sort(function(a, b) { var extensions = Object.keys(interpret.extensions).sort(function(a, b) {
return a === '.js' ? -1 : b === '.js' ? 1 : a.length - b.length; return a === '.js' ? -1 : b === '.js' ? 1 : a.length - b.length;
}); });
var configFiles = ["webpack.config", "webpackfile"].map(function(filename) { var defaultConfigFiles = ["webpack.config", "webpackfile"].map(function(filename) {
return extensions.map(function(ext) { return extensions.map(function(ext) {
return { return {
path: path.resolve(filename + ext), path: path.resolve(filename + ext),
@ -52,30 +52,41 @@ module.exports = function(optimist, argv, convertOptions) {
var i; var i;
if(argv.config) { if(argv.config) {
configPath = path.resolve(argv.config); function getConfigExtension(configPath) {
for(i = extensions.length - 1; i >= 0; i--) { for(i = extensions.length - 1; i >= 0; i--) {
var tmpExt = extensions[i]; var tmpExt = extensions[i];
if(configPath.indexOf(tmpExt, configPath.length - tmpExt.length) > -1) { if(configPath.indexOf(tmpExt, configPath.length - tmpExt.length) > -1) {
ext = tmpExt; return tmpExt;
break; break;
} }
} }
if(!ext) { return path.extname(configPath);
ext = path.extname(configPath); }
if(Array.isArray(argv.config)) {
configFile = argv.config.map(function(configArg) {
var resolvedPath = path.resolve(configArg);
var extension = getConfigExtension(resolvedPath);
return {
path: resolvedPath,
ext: extension
};
});
} else {
configFile.path = path.resolve(argv.config);
configFile.ext = getConfigExtension(configFile.path);
} }
} else { } else {
for(i = 0; i < configFiles.length; i++) { for(i = 0; i < defaultConfigFiles.length; i++) {
var webpackConfig = configFiles[i].path; var webpackConfig = defaultConfigFiles[i].path;
if(fs.existsSync(webpackConfig)) { if(fs.existsSync(webpackConfig)) {
ext = configFiles[i].ext; configFile.ext = defaultConfigFiles[i].ext;
configPath = webpackConfig; configFile.path = webpackConfig;
break; break;
} }
} }
} }
if(configPath) { if(configFile) {
function registerCompiler(moduleDescriptor) { function registerCompiler(moduleDescriptor) {
if(moduleDescriptor) { if(moduleDescriptor) {
if(typeof moduleDescriptor === "string") { if(typeof moduleDescriptor === "string") {
@ -95,19 +106,31 @@ module.exports = function(optimist, argv, convertOptions) {
} }
} }
registerCompiler(interpret.extensions[ext]); function requireConfig(configPath) {
options = require(configPath); var options = require(configPath);
configFileLoaded = true;
}
var isES6DefaultExportedFunc = ( var isES6DefaultExportedFunc = (
typeof options === "object" && options !== null && typeof options.default === "function" typeof options === "object" && options !== null && typeof options.default === "function"
); );
if(typeof options === "function" || isES6DefaultExportedFunc) { if(typeof options === "function" || isES6DefaultExportedFunc) {
options = isES6DefaultExportedFunc ? options.default : options; options = isES6DefaultExportedFunc ? options.default : options;
options = options(argv.env, argv); options = options(argv.env, argv);
} }
return options;
}
if(Array.isArray(configFile)) {
options = [];
configFile.forEach(function(file) {
registerCompiler(interpret.extensions[file.ext]);
options.push(requireConfig(file.path));
});
configFileLoaded = true;
} else {
registerCompiler(interpret.extensions[configFile.ext]);
options = requireConfig(configFile.path);
configFileLoaded = true;
}
}
return processConfiguredOptions(options); return processConfiguredOptions(options);
@ -543,7 +566,7 @@ module.exports = function(optimist, argv, convertOptions) {
} }
if(!options.entry) { if(!options.entry) {
if(configPath) { if(configFile.path) {
console.error("Configuration file found but no entry configured."); console.error("Configuration file found but no entry configured.");
} else { } else {
console.error("No configuration file found and no entry configured via CLI option."); console.error("No configuration file found and no entry configured via CLI option.");