Support returning Promise and ES6 default export from configuration

This commit is contained in:
Tobias Koppers 2015-11-14 15:34:45 +01:00
parent 32004fb149
commit bb2c3558c0
3 changed files with 248 additions and 218 deletions

View File

@ -11,7 +11,7 @@ module.exports = function(optimist, argv, convertOptions) {
// Help // Help
if(argv.help) { if(argv.help) {
optimist.showHelp(); optimist.showHelp();
process.exit(-1); process.exit(-1); // eslint-disable-line
} }
// Shortcuts // Shortcuts
@ -80,9 +80,22 @@ module.exports = function(optimist, argv, convertOptions) {
options = require(configPath); options = require(configPath);
} }
return processConfiguredOptions(options);
function processConfiguredOptions(options) {
if(typeof options !== "object" || options === null) { if(typeof options !== "object" || options === null) {
console.log("Config did not export an object."); console.log("Config did not export an object.");
process.exit(-1); process.exit(-1); // eslint-disable-line
}
// process Promise
if(typeof options.then === "function") {
return options.then(processConfiguredOptions);
}
// process ES6 default
if(typeof options === "object" && typeof options["default"] === "object") {
return processConfiguredOptions(options["default"]);
} }
if(Array.isArray(options)) { if(Array.isArray(options)) {
@ -138,6 +151,9 @@ module.exports = function(optimist, argv, convertOptions) {
options.watch = true; options.watch = true;
} }
return options;
}
function processOptions(options) { function processOptions(options) {
function ifArg(name, fn, init, finalize) { function ifArg(name, fn, init, finalize) {
if(Array.isArray(argv[name])) { if(Array.isArray(argv[name])) {
@ -209,7 +225,7 @@ module.exports = function(optimist, argv, convertOptions) {
} }
} catch(e) { } catch(e) {
console.log("Invalid plugin arguments " + name + " (" + e + ")."); console.log("Invalid plugin arguments " + name + " (" + e + ").");
process.exit(-1); process.exit(-1); // eslint-disable-line
} }
var path; var path;
@ -217,7 +233,7 @@ module.exports = function(optimist, argv, convertOptions) {
path = resolve.sync(process.cwd(), name); path = resolve.sync(process.cwd(), name);
} catch(e) { } catch(e) {
console.log("Cannot resolve plugin " + name + "."); console.log("Cannot resolve plugin " + name + ".");
process.exit(-1); process.exit(-1); // eslint-disable-line
} }
var Plugin; var Plugin;
try { try {
@ -516,7 +532,7 @@ module.exports = function(optimist, argv, convertOptions) {
} else { } else {
optimist.showHelp(); optimist.showHelp();
console.error("Output filename not configured."); console.error("Output filename not configured.");
process.exit(-1); process.exit(-1); // eslint-disable-line
} }
} }
@ -553,8 +569,5 @@ module.exports = function(optimist, argv, convertOptions) {
} }
}); });
} }
} }
return options;
}; };

View File

@ -48,37 +48,47 @@ function ifArg(name, fn, init) {
} }
} }
var firstOptions = Array.isArray(options) ? options[0] : options; function processOptions(options) {
// process Promise
if(typeof options.then === "function") {
options.then(processOptions).catch(function(err) {
console.error(err.stack || err);
process.exit(); // eslint-disable-line
});
return;
}
var outputOptions = Object.create(options.stats || firstOptions.stats || {}); var firstOptions = Array.isArray(options) ? options[0] : options;
if(typeof outputOptions.context === "undefined")
var outputOptions = Object.create(options.stats || firstOptions.stats || {});
if(typeof outputOptions.context === "undefined")
outputOptions.context = firstOptions.context; outputOptions.context = firstOptions.context;
ifArg("json", function(bool) { ifArg("json", function(bool) {
if(bool) if(bool)
outputOptions.json = bool; outputOptions.json = bool;
}); });
if(typeof outputOptions.colors === "undefined") if(typeof outputOptions.colors === "undefined")
outputOptions.colors = require("supports-color"); outputOptions.colors = require("supports-color");
ifArg("sort-modules-by", function(value) { ifArg("sort-modules-by", function(value) {
outputOptions.modulesSort = value; outputOptions.modulesSort = value;
}); });
ifArg("sort-chunks-by", function(value) { ifArg("sort-chunks-by", function(value) {
outputOptions.chunksSort = value; outputOptions.chunksSort = value;
}); });
ifArg("sort-assets-by", function(value) { ifArg("sort-assets-by", function(value) {
outputOptions.assetsSort = value; outputOptions.assetsSort = value;
}); });
ifArg("display-exclude", function(value) { ifArg("display-exclude", function(value) {
outputOptions.exclude = value; outputOptions.exclude = value;
}); });
if(!outputOptions.json) { if(!outputOptions.json) {
if(typeof outputOptions.cached === "undefined") if(typeof outputOptions.cached === "undefined")
outputOptions.cached = false; outputOptions.cached = false;
if(typeof outputOptions.cachedAssets === "undefined") if(typeof outputOptions.cachedAssets === "undefined")
@ -113,7 +123,7 @@ if(!outputOptions.json) {
if(!outputOptions.exclude && !argv["display-modules"]) if(!outputOptions.exclude && !argv["display-modules"])
outputOptions.exclude = ["node_modules", "bower_components", "jam", "components"]; outputOptions.exclude = ["node_modules", "bower_components", "jam", "components"];
} else { } else {
if(typeof outputOptions.chunks === "undefined") if(typeof outputOptions.chunks === "undefined")
outputOptions.chunks = true; outputOptions.chunks = true;
if(typeof outputOptions.modules === "undefined") if(typeof outputOptions.modules === "undefined")
@ -126,22 +136,22 @@ if(!outputOptions.json) {
outputOptions.cached = true; outputOptions.cached = true;
if(typeof outputOptions.cachedAssets === "undefined") if(typeof outputOptions.cachedAssets === "undefined")
outputOptions.cachedAssets = true; outputOptions.cachedAssets = true;
} }
ifArg("hide-modules", function(bool) { ifArg("hide-modules", function(bool) {
if(bool) { if(bool) {
outputOptions.modules = false; outputOptions.modules = false;
outputOptions.chunkModules = false; outputOptions.chunkModules = false;
} }
}); });
var webpack = require("../lib/webpack.js"); var webpack = require("../lib/webpack.js");
Error.stackTraceLimit = 30; Error.stackTraceLimit = 30;
var lastHash = null; var lastHash = null;
var compiler = webpack(options); var compiler = webpack(options);
function compilerCallback(err, stats) { function compilerCallback(err, stats) {
if(!options.watch) { if(!options.watch) {
// Do not keep cache anymore // Do not keep cache anymore
compiler.purgeInputFileSystem(); compiler.purgeInputFileSystem();
@ -152,7 +162,7 @@ function compilerCallback(err, stats) {
if(err.details) console.error(err.details); if(err.details) console.error(err.details);
if(!options.watch) { if(!options.watch) {
process.on("exit", function() { process.on("exit", function() {
process.exit(1); process.exit(1); // eslint-disable-line
}); });
} }
return; return;
@ -163,16 +173,20 @@ function compilerCallback(err, stats) {
lastHash = stats.hash; lastHash = stats.hash;
process.stdout.write(stats.toString(outputOptions) + "\n"); process.stdout.write(stats.toString(outputOptions) + "\n");
} }
} }
if(options.watch) { if(options.watch) {
var primaryOptions = !Array.isArray(options) ? options : options[0]; var primaryOptions = !Array.isArray(options) ? options : options[0];
var watchOptions = primaryOptions.watchOptions || primaryOptions.watch || {}; var watchOptions = primaryOptions.watchOptions || primaryOptions.watch || {};
if(watchOptions.stdin) { if(watchOptions.stdin) {
process.stdin.on('end', function() { process.stdin.on('end', function() {
process.exit(0) process.exit(0); // eslint-disable-line
}); });
process.stdin.resume(); process.stdin.resume();
} }
compiler.watch(watchOptions, compilerCallback); compiler.watch(watchOptions, compilerCallback);
} else } else
compiler.run(compilerCallback); compiler.run(compilerCallback);
}
processOptions(options);

View File

@ -1,5 +1,7 @@
webpack = require("../../"); webpack = require("../../");
module.exports = exports.default = new Promise (resolve, reject) ->
resolveIt = ->
resolve
output: output:
hashDigestLength: 5 hashDigestLength: 5
module: module:
@ -19,7 +21,7 @@ module.exports =
new webpack.DefinePlugin new webpack.DefinePlugin
"typeof CONST_TYPEOF": JSON.stringify("typeof"), "typeof CONST_TYPEOF": JSON.stringify("typeof"),
CONST_UNDEFINED: undefined, CONST_UNDEFINED: undefined,
CONST_NULL: null, CONST_NULL: "null",
CONST_TRUE: true, CONST_TRUE: true,
CONST_FALSE: false, CONST_FALSE: false,
CONST_FUNCTION: -> return "ok"; CONST_FUNCTION: -> return "ok";
@ -38,3 +40,4 @@ module.exports =
data.resource = data.resource.replace /extra\.js/, "extra2.js"; data.resource = data.resource.replace /extra\.js/, "extra2.js";
callback null, data; callback null, data;
] ]
setTimeout resolveIt, 300