123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- 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);
- };
|