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
if(argv.help) {
optimist.showHelp();
process.exit(-1);
process.exit(-1); // eslint-disable-line
}
// Shortcuts
@ -80,62 +80,78 @@ module.exports = function(optimist, argv, convertOptions) {
options = require(configPath);
}
if(typeof options !== "object" || options === null) {
console.log("Config did not export an object.");
process.exit(-1);
}
return processConfiguredOptions(options);
if(Array.isArray(options)) {
options.forEach(processOptions);
} else {
processOptions(options);
}
if(argv.context) {
options.context = path.resolve(argv.context);
}
if(!options.context) {
options.context = process.cwd();
}
if(argv.watch) {
// TODO remove this in next major version
if(options.watch && typeof options.watch === "object") {
console.warn("options.watch is deprecated: Use 'options.watchOptions' instead");
options.watchOptions = options.watch;
function processConfiguredOptions(options) {
if(typeof options !== "object" || options === null) {
console.log("Config did not export an object.");
process.exit(-1); // eslint-disable-line
}
// TODO remove this in next major version
if(options.watchDelay) {
console.warn("options.watchDelay is deprecated: Use 'options.watchOptions.aggregateTimeout' instead");
// 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)) {
options.forEach(processOptions);
} else {
processOptions(options);
}
if(argv.context) {
options.context = path.resolve(argv.context);
}
if(!options.context) {
options.context = process.cwd();
}
if(argv.watch) {
// TODO remove this in next major version
if(options.watch && typeof options.watch === "object") {
console.warn("options.watch is deprecated: Use 'options.watchOptions' instead");
options.watchOptions = options.watch;
}
// TODO remove this in next major version
if(options.watchDelay) {
console.warn("options.watchDelay is deprecated: Use 'options.watchOptions.aggregateTimeout' instead");
options.watchOptions = options.watchOptions || {};
options.watchOptions.aggregateTimeout = options.watchDelay;
}
options.watch = true;
}
if(argv["watch-delay"]) {
console.warn("--watch-delay is deprecated: Use '--watch-aggregate-timeout' instead");
options.watchOptions = options.watchOptions || {};
options.watchOptions.aggregateTimeout = options.watchDelay;
options.watchOptions.aggregateTimeout = +argv["watch-delay"];
}
options.watch = true;
}
if(argv["watch-delay"]) {
console.warn("--watch-delay is deprecated: Use '--watch-aggregate-timeout' instead");
options.watchOptions = options.watchOptions || {};
options.watchOptions.aggregateTimeout = +argv["watch-delay"];
}
if(argv["watch-aggregate-timeout"]) {
options.watchOptions = options.watchOptions || {};
options.watchOptions.aggregateTimeout = +argv["watch-aggregate-timeout"];
}
if(argv["watch-aggregate-timeout"]) {
options.watchOptions = options.watchOptions || {};
options.watchOptions.aggregateTimeout = +argv["watch-aggregate-timeout"];
}
if(argv["watch-poll"]) {
options.watchOptions = options.watchOptions || {};
if(typeof argv["watch-poll"] !== "boolean")
options.watchOptions.poll = +argv["watch-poll"];
else
options.watchOptions.poll = true;
}
if(argv["watch-poll"]) {
options.watchOptions = options.watchOptions || {};
if(typeof argv["watch-poll"] !== "boolean")
options.watchOptions.poll = +argv["watch-poll"];
else
options.watchOptions.poll = true;
}
if(argv["watch-stdin"]) {
options.watchOptions = options.watchOptions || {};
options.watchOptions.stdin = true;
options.watch = true;
}
if(argv["watch-stdin"]) {
options.watchOptions = options.watchOptions || {};
options.watchOptions.stdin = true;
options.watch = true;
return options;
}
function processOptions(options) {
@ -209,7 +225,7 @@ module.exports = function(optimist, argv, convertOptions) {
}
} catch(e) {
console.log("Invalid plugin arguments " + name + " (" + e + ").");
process.exit(-1);
process.exit(-1); // eslint-disable-line
}
var path;
@ -217,7 +233,7 @@ module.exports = function(optimist, argv, convertOptions) {
path = resolve.sync(process.cwd(), name);
} catch(e) {
console.log("Cannot resolve plugin " + name + ".");
process.exit(-1);
process.exit(-1); // eslint-disable-line
}
var Plugin;
try {
@ -516,7 +532,7 @@ module.exports = function(optimist, argv, convertOptions) {
} else {
optimist.showHelp();
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,131 +48,145 @@ function ifArg(name, fn, init) {
}
}
var firstOptions = Array.isArray(options) ? options[0] : options;
var outputOptions = Object.create(options.stats || firstOptions.stats || {});
if(typeof outputOptions.context === "undefined")
outputOptions.context = firstOptions.context;
ifArg("json", function(bool) {
if(bool)
outputOptions.json = bool;
});
if(typeof outputOptions.colors === "undefined")
outputOptions.colors = require("supports-color");
ifArg("sort-modules-by", function(value) {
outputOptions.modulesSort = value;
});
ifArg("sort-chunks-by", function(value) {
outputOptions.chunksSort = value;
});
ifArg("sort-assets-by", function(value) {
outputOptions.assetsSort = value;
});
ifArg("display-exclude", function(value) {
outputOptions.exclude = value;
});
if(!outputOptions.json) {
if(typeof outputOptions.cached === "undefined")
outputOptions.cached = false;
if(typeof outputOptions.cachedAssets === "undefined")
outputOptions.cachedAssets = false;
ifArg("display-chunks", function(bool) {
outputOptions.modules = !bool;
outputOptions.chunks = bool;
});
ifArg("display-reasons", function(bool) {
outputOptions.reasons = bool;
});
ifArg("display-error-details", function(bool) {
outputOptions.errorDetails = bool;
});
ifArg("display-origins", function(bool) {
outputOptions.chunkOrigins = bool;
});
ifArg("display-cached", function(bool) {
if(bool)
outputOptions.cached = true;
});
ifArg("display-cached-assets", function(bool) {
if(bool)
outputOptions.cachedAssets = true;
});
if(!outputOptions.exclude && !argv["display-modules"])
outputOptions.exclude = ["node_modules", "bower_components", "jam", "components"];
} else {
if(typeof outputOptions.chunks === "undefined")
outputOptions.chunks = true;
if(typeof outputOptions.modules === "undefined")
outputOptions.modules = true;
if(typeof outputOptions.chunkModules === "undefined")
outputOptions.chunkModules = true;
if(typeof outputOptions.reasons === "undefined")
outputOptions.reasons = true;
if(typeof outputOptions.cached === "undefined")
outputOptions.cached = true;
if(typeof outputOptions.cachedAssets === "undefined")
outputOptions.cachedAssets = true;
}
ifArg("hide-modules", function(bool) {
if(bool) {
outputOptions.modules = false;
outputOptions.chunkModules = false;
}
});
var webpack = require("../lib/webpack.js");
Error.stackTraceLimit = 30;
var lastHash = null;
var compiler = webpack(options);
function compilerCallback(err, stats) {
if(!options.watch) {
// Do not keep cache anymore
compiler.purgeInputFileSystem();
}
if(err) {
lastHash = null;
console.error(err.stack || err);
if(err.details) console.error(err.details);
if(!options.watch) {
process.on("exit", function() {
process.exit(1);
});
}
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;
}
if(outputOptions.json) {
process.stdout.write(JSON.stringify(stats.toJson(outputOptions), null, 2) + "\n");
} else if(stats.hash !== lastHash) {
lastHash = stats.hash;
process.stdout.write(stats.toString(outputOptions) + "\n");
}
}
if(options.watch) {
var primaryOptions = !Array.isArray(options) ? options : options[0];
var watchOptions = primaryOptions.watchOptions || primaryOptions.watch || {};
if(watchOptions.stdin) {
process.stdin.on('end', function() {
process.exit(0)
var firstOptions = Array.isArray(options) ? options[0] : options;
var outputOptions = Object.create(options.stats || firstOptions.stats || {});
if(typeof outputOptions.context === "undefined")
outputOptions.context = firstOptions.context;
ifArg("json", function(bool) {
if(bool)
outputOptions.json = bool;
});
if(typeof outputOptions.colors === "undefined")
outputOptions.colors = require("supports-color");
ifArg("sort-modules-by", function(value) {
outputOptions.modulesSort = value;
});
ifArg("sort-chunks-by", function(value) {
outputOptions.chunksSort = value;
});
ifArg("sort-assets-by", function(value) {
outputOptions.assetsSort = value;
});
ifArg("display-exclude", function(value) {
outputOptions.exclude = value;
});
if(!outputOptions.json) {
if(typeof outputOptions.cached === "undefined")
outputOptions.cached = false;
if(typeof outputOptions.cachedAssets === "undefined")
outputOptions.cachedAssets = false;
ifArg("display-chunks", function(bool) {
outputOptions.modules = !bool;
outputOptions.chunks = bool;
});
process.stdin.resume();
ifArg("display-reasons", function(bool) {
outputOptions.reasons = bool;
});
ifArg("display-error-details", function(bool) {
outputOptions.errorDetails = bool;
});
ifArg("display-origins", function(bool) {
outputOptions.chunkOrigins = bool;
});
ifArg("display-cached", function(bool) {
if(bool)
outputOptions.cached = true;
});
ifArg("display-cached-assets", function(bool) {
if(bool)
outputOptions.cachedAssets = true;
});
if(!outputOptions.exclude && !argv["display-modules"])
outputOptions.exclude = ["node_modules", "bower_components", "jam", "components"];
} else {
if(typeof outputOptions.chunks === "undefined")
outputOptions.chunks = true;
if(typeof outputOptions.modules === "undefined")
outputOptions.modules = true;
if(typeof outputOptions.chunkModules === "undefined")
outputOptions.chunkModules = true;
if(typeof outputOptions.reasons === "undefined")
outputOptions.reasons = true;
if(typeof outputOptions.cached === "undefined")
outputOptions.cached = true;
if(typeof outputOptions.cachedAssets === "undefined")
outputOptions.cachedAssets = true;
}
compiler.watch(watchOptions, compilerCallback);
} else
compiler.run(compilerCallback);
ifArg("hide-modules", function(bool) {
if(bool) {
outputOptions.modules = false;
outputOptions.chunkModules = false;
}
});
var webpack = require("../lib/webpack.js");
Error.stackTraceLimit = 30;
var lastHash = null;
var compiler = webpack(options);
function compilerCallback(err, stats) {
if(!options.watch) {
// Do not keep cache anymore
compiler.purgeInputFileSystem();
}
if(err) {
lastHash = null;
console.error(err.stack || err);
if(err.details) console.error(err.details);
if(!options.watch) {
process.on("exit", function() {
process.exit(1); // eslint-disable-line
});
}
return;
}
if(outputOptions.json) {
process.stdout.write(JSON.stringify(stats.toJson(outputOptions), null, 2) + "\n");
} else if(stats.hash !== lastHash) {
lastHash = stats.hash;
process.stdout.write(stats.toString(outputOptions) + "\n");
}
}
if(options.watch) {
var primaryOptions = !Array.isArray(options) ? options : options[0];
var watchOptions = primaryOptions.watchOptions || primaryOptions.watch || {};
if(watchOptions.stdin) {
process.stdin.on('end', function() {
process.exit(0); // eslint-disable-line
});
process.stdin.resume();
}
compiler.watch(watchOptions, compilerCallback);
} else
compiler.run(compilerCallback);
}
processOptions(options);

View File

@ -1,40 +1,43 @@
webpack = require("../../");
module.exports =
output:
hashDigestLength: 5
module:
postLoaders: [
{ test: /extra2?\.js/, loader: "raw!extra!val?cacheable" }
]
amd:
fromOptions: true
resolve:
# cannot resolve should outside the outermost node_modules
# so it is injected here
alias:
should: require.resolve "should"
plugins: [
new webpack.optimize.LimitChunkCountPlugin
maxChunks: 2
new webpack.DefinePlugin
"typeof CONST_TYPEOF": JSON.stringify("typeof"),
CONST_UNDEFINED: undefined,
CONST_NULL: null,
CONST_TRUE: true,
CONST_FALSE: false,
CONST_FUNCTION: -> return "ok";
CONST_NUMBER: 123,
CONST_NUMBER_EXPR: "1*100+23",
CONST_OBJECT: {
A: 1,
B: JSON.stringify("B"),
C: -> return "C";
}
new webpack.ProvidePlugin
s3: "submodule3"
->
this.plugin "normal-module-factory", (nmf) ->
nmf.plugin "after-resolve", (data, callback) ->
data.resource = data.resource.replace /extra\.js/, "extra2.js";
callback null, data;
]
exports.default = new Promise (resolve, reject) ->
resolveIt = ->
resolve
output:
hashDigestLength: 5
module:
postLoaders: [
{ test: /extra2?\.js/, loader: "raw!extra!val?cacheable" }
]
amd:
fromOptions: true
resolve:
# cannot resolve should outside the outermost node_modules
# so it is injected here
alias:
should: require.resolve "should"
plugins: [
new webpack.optimize.LimitChunkCountPlugin
maxChunks: 2
new webpack.DefinePlugin
"typeof CONST_TYPEOF": JSON.stringify("typeof"),
CONST_UNDEFINED: undefined,
CONST_NULL: "null",
CONST_TRUE: true,
CONST_FALSE: false,
CONST_FUNCTION: -> return "ok";
CONST_NUMBER: 123,
CONST_NUMBER_EXPR: "1*100+23",
CONST_OBJECT: {
A: 1,
B: JSON.stringify("B"),
C: -> return "C";
}
new webpack.ProvidePlugin
s3: "submodule3"
->
this.plugin "normal-module-factory", (nmf) ->
nmf.plugin "after-resolve", (data, callback) ->
data.resource = data.resource.replace /extra\.js/, "extra2.js";
callback null, data;
]
setTimeout resolveIt, 300