123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515 |
- #!/usr/bin/env node
- /*
- MIT License http://www.opensource.org/licenses/mit-license.php
- Author Tobias Koppers @sokra
- */
- (function() {
- // wrap in IIFE to be able to use return
- const importLocal = require("import-local");
- // Prefer the local installation of webpack-cli
- if (importLocal(__filename)) {
- return;
- }
- require("v8-compile-cache");
- const ErrorHelpers = require("./errorHelpers");
- const NON_COMPILATION_ARGS = [
- "init",
- "migrate",
- /*
- "add",
- "remove",
- "update",
- "make",
- */
- "serve",
- "generate-loader",
- "generate-plugin",
- "info"
- ];
- const NON_COMPILATION_CMD = process.argv.find(arg => {
- if (arg === "serve") {
- global.process.argv = global.process.argv.filter(a => a !== "serve");
- process.argv = global.process.argv;
- }
- return NON_COMPILATION_ARGS.find(a => a === arg);
- });
- if (NON_COMPILATION_CMD) {
- // eslint-disable-next-line
- require("../lib/index")(NON_COMPILATION_CMD, process.argv);
- return;
- }
- const yargs = require("yargs").usage(`webpack-cli ${
- require("../package.json").version
- }
- Usage: webpack-cli [options]
- webpack-cli [options] --entry <entry> --output <output>
- webpack-cli [options] <entries...> --output <output>
- webpack-cli <command> [options]
- For more information, see https://webpack.js.org/api/cli/.`);
- require("./config-yargs")(yargs);
- const DISPLAY_GROUP = "Stats options:";
- const BASIC_GROUP = "Basic options:";
- yargs.options({
- silent: {
- type: "boolean",
- describe: "Prevent output from being displayed in stdout"
- },
- json: {
- type: "boolean",
- alias: "j",
- describe: "Prints the result as JSON."
- },
- progress: {
- type: "boolean",
- describe: "Print compilation progress in percentage",
- group: BASIC_GROUP
- },
- color: {
- type: "boolean",
- alias: "colors",
- default: function supportsColor() {
- return require("supports-color").supportsColor;
- },
- group: DISPLAY_GROUP,
- describe: "Enables/Disables colors on the console"
- },
- "sort-modules-by": {
- type: "string",
- group: DISPLAY_GROUP,
- describe: "Sorts the modules list by property in module"
- },
- "sort-chunks-by": {
- type: "string",
- group: DISPLAY_GROUP,
- describe: "Sorts the chunks list by property in chunk"
- },
- "sort-assets-by": {
- type: "string",
- group: DISPLAY_GROUP,
- describe: "Sorts the assets list by property in asset"
- },
- "hide-modules": {
- type: "boolean",
- group: DISPLAY_GROUP,
- describe: "Hides info about modules"
- },
- "display-exclude": {
- type: "string",
- group: DISPLAY_GROUP,
- describe: "Exclude modules in the output"
- },
- "display-modules": {
- type: "boolean",
- group: DISPLAY_GROUP,
- describe: "Display even excluded modules in the output"
- },
- "display-max-modules": {
- type: "number",
- group: DISPLAY_GROUP,
- describe: "Sets the maximum number of visible modules in output"
- },
- "display-chunks": {
- type: "boolean",
- group: DISPLAY_GROUP,
- describe: "Display chunks in the output"
- },
- "display-entrypoints": {
- type: "boolean",
- group: DISPLAY_GROUP,
- describe: "Display entry points in the output"
- },
- "display-origins": {
- type: "boolean",
- group: DISPLAY_GROUP,
- describe: "Display origins of chunks in the output"
- },
- "display-cached": {
- type: "boolean",
- group: DISPLAY_GROUP,
- describe: "Display also cached modules in the output"
- },
- "display-cached-assets": {
- type: "boolean",
- group: DISPLAY_GROUP,
- describe: "Display also cached assets in the output"
- },
- "display-reasons": {
- type: "boolean",
- group: DISPLAY_GROUP,
- describe: "Display reasons about module inclusion in the output"
- },
- "display-depth": {
- type: "boolean",
- group: DISPLAY_GROUP,
- describe: "Display distance from entry point for each module"
- },
- "display-used-exports": {
- type: "boolean",
- group: DISPLAY_GROUP,
- describe:
- "Display information about used exports in modules (Tree Shaking)"
- },
- "display-provided-exports": {
- type: "boolean",
- group: DISPLAY_GROUP,
- describe: "Display information about exports provided from modules"
- },
- "display-optimization-bailout": {
- type: "boolean",
- group: DISPLAY_GROUP,
- describe:
- "Display information about why optimization bailed out for modules"
- },
- "display-error-details": {
- type: "boolean",
- group: DISPLAY_GROUP,
- describe: "Display details about errors"
- },
- display: {
- type: "string",
- choices: [
- "",
- "verbose",
- "detailed",
- "normal",
- "minimal",
- "errors-only",
- "none"
- ],
- group: DISPLAY_GROUP,
- describe: "Select display preset"
- },
- verbose: {
- type: "boolean",
- group: DISPLAY_GROUP,
- describe: "Show more details"
- },
- "info-verbosity": {
- type: "string",
- default: "info",
- choices: ["none", "info", "verbose"],
- group: DISPLAY_GROUP,
- describe:
- "Controls the output of lifecycle messaging e.g. Started watching files..."
- },
- "build-delimiter": {
- type: "string",
- group: DISPLAY_GROUP,
- describe: "Display custom text after build output"
- }
- });
- // yargs will terminate the process early when the user uses help or version.
- // This causes large help outputs to be cut short (https://github.com/nodejs/node/wiki/API-changes-between-v0.10-and-v4#process).
- // To prevent this we use the yargs.parse API and exit the process normally
- yargs.parse(process.argv.slice(2), (err, argv, output) => {
- Error.stackTraceLimit = 30;
- // arguments validation failed
- if (err && output) {
- console.error(output);
- process.exitCode = 1;
- return;
- }
- // help or version info
- if (output) {
- console.log(output);
- return;
- }
- if (argv.verbose) {
- argv["display"] = "verbose";
- }
- let options;
- try {
- options = require("./convert-argv")(argv);
- } catch (err) {
- if (err.name !== "ValidationError") {
- throw err;
- }
- const stack = ErrorHelpers.cleanUpWebpackOptions(err.stack, err.message);
- const message = err.message + "\n" + stack;
- if (argv.color) {
- console.error(`\u001b[1m\u001b[31m${message}\u001b[39m\u001b[22m`);
- } else {
- console.error(message);
- }
- process.exitCode = 1;
- return;
- }
- /**
- * When --silent flag is present, an object with a no-op write method is
- * used in place of process.stout
- */
- const stdout = argv.silent
- ? {
- write: () => {}
- } // eslint-disable-line
- : process.stdout;
- 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);
- }
- }
- function processOptions(options) {
- // process Promise
- if (typeof options.then === "function") {
- options.then(processOptions).catch(function(err) {
- console.error(err.stack || err);
- process.exit(1); // eslint-disable-line
- });
- return;
- }
- const firstOptions = [].concat(options)[0];
- const statsPresetToOptions = require("webpack").Stats.presetToOptions;
- let outputOptions = options.stats;
- if (
- typeof outputOptions === "boolean" ||
- typeof outputOptions === "string"
- ) {
- outputOptions = statsPresetToOptions(outputOptions);
- } else if (!outputOptions) {
- outputOptions = {};
- }
- ifArg("display", function(preset) {
- outputOptions = statsPresetToOptions(preset);
- });
- outputOptions = Object.create(outputOptions);
- if (Array.isArray(options) && !outputOptions.children) {
- outputOptions.children = options.map(o => o.stats);
- }
- if (typeof outputOptions.context === "undefined")
- outputOptions.context = firstOptions.context;
- ifArg("env", function(value) {
- if (outputOptions.env) {
- outputOptions._env = value;
- }
- });
- ifArg("json", function(bool) {
- if (bool) {
- outputOptions.json = bool;
- outputOptions.modules = bool;
- }
- });
- if (typeof outputOptions.colors === "undefined")
- outputOptions.colors = require("supports-color").stdout;
- 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) {
- if (bool) {
- outputOptions.modules = false;
- outputOptions.chunks = true;
- outputOptions.chunkModules = true;
- }
- });
- ifArg("display-entrypoints", function(bool) {
- outputOptions.entrypoints = bool;
- });
- ifArg("display-reasons", function(bool) {
- if (bool) outputOptions.reasons = true;
- });
- ifArg("display-depth", function(bool) {
- if (bool) outputOptions.depth = true;
- });
- ifArg("display-used-exports", function(bool) {
- if (bool) outputOptions.usedExports = true;
- });
- ifArg("display-provided-exports", function(bool) {
- if (bool) outputOptions.providedExports = true;
- });
- ifArg("display-optimization-bailout", function(bool) {
- if (bool) outputOptions.optimizationBailout = bool;
- });
- ifArg("display-error-details", function(bool) {
- if (bool) outputOptions.errorDetails = true;
- });
- ifArg("display-origins", function(bool) {
- if (bool) outputOptions.chunkOrigins = true;
- });
- ifArg("display-max-modules", function(value) {
- outputOptions.maxModules = +value;
- });
- ifArg("display-cached", function(bool) {
- if (bool) outputOptions.cached = true;
- });
- ifArg("display-cached-assets", function(bool) {
- if (bool) outputOptions.cachedAssets = true;
- });
- if (!outputOptions.exclude)
- outputOptions.exclude = [
- "node_modules",
- "bower_components",
- "components"
- ];
- if (argv["display-modules"]) {
- outputOptions.maxModules = Infinity;
- outputOptions.exclude = undefined;
- outputOptions.modules = true;
- }
- }
- ifArg("hide-modules", function(bool) {
- if (bool) {
- outputOptions.modules = false;
- outputOptions.chunkModules = false;
- }
- });
- ifArg("info-verbosity", function(value) {
- outputOptions.infoVerbosity = value;
- });
- ifArg("build-delimiter", function(value) {
- outputOptions.buildDelimiter = value;
- });
- const webpack = require("webpack");
- let lastHash = null;
- let compiler;
- try {
- compiler = webpack(options);
- } catch (err) {
- if (err.name === "WebpackOptionsValidationError") {
- if (argv.color)
- console.error(
- `\u001b[1m\u001b[31m${err.message}\u001b[39m\u001b[22m`
- );
- else console.error(err.message);
- // eslint-disable-next-line no-process-exit
- process.exit(1);
- }
- throw err;
- }
- if (argv.progress) {
- const ProgressPlugin = require("webpack").ProgressPlugin;
- new ProgressPlugin({
- profile: argv.profile
- }).apply(compiler);
- }
- if (outputOptions.infoVerbosity === "verbose") {
- compiler.hooks.beforeCompile.tap("WebpackInfo", compilation => {
- console.log("\nCompilation starting…\n");
- });
- compiler.hooks.afterCompile.tap("WebpackInfo", compilation => {
- console.log("\nCompilation finished\n");
- });
- }
- 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.exit(1); // eslint-disable-line
- }
- if (outputOptions.json) {
- stdout.write(
- JSON.stringify(stats.toJson(outputOptions), null, 2) + "\n"
- );
- } else if (stats.hash !== lastHash) {
- lastHash = stats.hash;
- const statsString = stats.toString(outputOptions);
- const delimiter = outputOptions.buildDelimiter
- ? `${outputOptions.buildDelimiter}\n`
- : "";
- if (statsString) stdout.write(`${statsString}\n${delimiter}`);
- }
- if (!options.watch && stats.hasErrors()) {
- process.exitCode = 2;
- }
- }
- if (firstOptions.watch || options.watch) {
- const watchOptions =
- firstOptions.watchOptions ||
- firstOptions.watch ||
- options.watch ||
- {};
- if (watchOptions.stdin) {
- process.stdin.on("end", function(_) {
- process.exit(); // eslint-disable-line
- });
- process.stdin.resume();
- }
- compiler.watch(watchOptions, compilerCallback);
- if (outputOptions.infoVerbosity !== "none")
- console.log("\nWebpack is watching the files…\n");
- } else compiler.run(compilerCallback);
- }
- processOptions(options);
- });
- })();
|