diff --git a/bin/cli.js b/bin/cli.js index 757a8220cd8..f9084b66214 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -79,7 +79,9 @@ For more information, see https://webpack.js.org/api/cli/.`); type: "boolean", alias: "colors", default: function supportsColor() { - return require("supports-color").supportsColor; + if (process.stdout.isTTY === true) { + return require("supports-color").supportsColor; + } }, group: DISPLAY_GROUP, describe: "Enables/Disables colors on the console" @@ -321,7 +323,10 @@ For more information, see https://webpack.js.org/api/cli/.`); } }); - if (typeof outputOptions.colors === "undefined") + if ( + typeof outputOptions.colors === "undefined" && + process.stdout.isTTY === true + ) outputOptions.colors = require("supports-color").stdout; ifArg("sort-modules-by", function(value) { diff --git a/bin/process-options.js b/bin/process-options.js deleted file mode 100644 index ba1fd64ad84..00000000000 --- a/bin/process-options.js +++ /dev/null @@ -1,208 +0,0 @@ -module.exports = function processOptions(yargs, argv) { - // process Promise - function ifArg(name, fn, init) { - if (Array.isArray(argv[name])) { - if (init) init(); - argv[name].forEach(fn); - } else if (typeof argv[name] !== "undefined") { - if (init) init(); - fn(argv[name], -1); - } - } - const options = require("./convert-argv")(argv); - - if (typeof options.then === "function") { - options.then(processOptions).catch(function(err) { - console.error(err.stack || err); - process.exitCode = 1; - }); - return; - } - - const firstOptions = Array.isArray(options) ? options[0] || {} : options; - - if (typeof options.stats === "boolean" || typeof options.stats === "string") { - const statsPresetToOptions = require("webpack").Stats.presetToOptions; - options.stats = statsPresetToOptions(options.stats); - } - - const 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-entrypoints", function(bool) { - outputOptions.entrypoints = bool; - }); - - ifArg("display-reasons", function(bool) { - outputOptions.reasons = bool; - }); - - ifArg("display-used-exports", function(bool) { - outputOptions.usedExports = bool; - }); - - ifArg("display-provided-exports", function(bool) { - outputOptions.providedExports = 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; - }); - - ifArg("build-delimiter", function(value) { - outputOptions.buildDelimiter = value; - }); - - 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.entrypoints === "undefined") - outputOptions.entrypoints = 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; - } - }); - - const webpack = require("webpack"); - - Error.stackTraceLimit = 30; - let lastHash = null; - let compiler; - try { - compiler = webpack(options); - } catch (e) { - const WebpackOptionsValidationError = require("webpack") - .WebpackOptionsValidationError; - - if (e instanceof WebpackOptionsValidationError) { - if (argv.color) - console.error( - "\u001b[1m\u001b[31m" + e.message + "\u001b[39m\u001b[22m" - ); - else console.error(e.message); - process.exitCode = 1; - } - throw e; - } - - if (argv.progress) { - const ProgressPlugin = require("webpack").ProgressPlugin; - new ProgressPlugin({ - profile: argv.profile - }).apply(compiler); - } - - function compilerCallback(err, stats) { - if (!options.watch || err) { - // Do not keep cache anymore - compiler.purgeInputFileSystem(); - } - if (err) { - lastHash = null; - console.error(err.stack || err); - if (err.details) console.error(err.details); - process.exitCode = 1; - } - if (outputOptions.json) { - process.stdout.write( - JSON.stringify(stats.toJson(outputOptions), null, 2) + "\n" - ); - } else if (stats.hash !== lastHash) { - lastHash = stats.hash; - const delimiter = outputOptions.buildDelimiter - ? `${outputOptions.buildDelimiter}\n` - : ""; - process.stdout.write("\n" + new Date() + "\n" + "\n"); - process.stdout.write(`${stats.toString(outputOptions)}\n${delimiter}`); - if (argv.s) lastHash = null; - } - if (!options.watch && stats.hasErrors()) { - process.on("exit", function(_) { - process.exitCode = 2; - }); - } - } - if (options.watch) { - const primaryOptions = !Array.isArray(options) ? options : options[0]; - const watchOptions = - primaryOptions.watchOptions || primaryOptions.watch || {}; - if (watchOptions.stdin) { - process.stdin.on("end", function(_) { - process.exitCode = 0; - }); - process.stdin.resume(); - } - compiler.watch(watchOptions, compilerCallback); - console.log("\nWebpack is watching the files…\n"); - } else compiler.run(compilerCallback); -};