diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c7e81fb..35b43fac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,11 @@ _Note: Gaps between patch versions are faulty, broken or test releases._ ## UNRELEASED +## 4.10.3 + +* **New Feature** + * Allows flags to support webpack-cli. ([#628](https://github.com/webpack-contrib/webpack-bundle-analyzer/pull/628) by [@info-arnav](https://github.com/info-arnav)) + ## 4.10.2 * **Bug Fix** diff --git a/README.md b/README.md index 5a45e738..10f3796a 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,7 @@ webpack-bundle-analyzer bundle/output/path/stats.json ```bash webpack-bundle-analyzer [bundleDir] [options] +webpack build --analyze [cliOptions] ``` Arguments are documented below: @@ -130,6 +131,28 @@ Directory containing all generated bundles. -h, --help output usage information ``` +### `cliOptions` + +``` + --analyze-version output the version number + --analyze-mode Analyzer mode. Should be `server`, `static` or `json`. + In `server` mode analyzer will start HTTP server to show bundle report. + In `static` mode single HTML file with bundle report will be generated. + In `json` mode single JSON file with bundle report will be generated. (default: server) + --analyze-host Host that will be used in `server` mode to start HTTP server. (default: 127.0.0.1) + --analyze-port Port that will be used in `server` mode to start HTTP server. Should be a number or `auto` (default: 8888) + --analyze-report Path to bundle report file that will be generated in `static` mode. (default: report.html) + --analyze-title String to use in title element of html report. (default: pretty printed current date) + --analyze-default-sizes <type> Module sizes to show in treemap by default. + Possible values: stat, parsed, gzip (default: parsed) + --analyze-no-open Don't open report in default browser automatically. + --analyze-exclude <regexp> Assets that should be excluded from the report. + Can be specified multiple times. + --analyze-log-level <level> Log level. + Possible values: debug, info, warn, error, silent (default: info) + --analyze-help output usage information +``` + <h2 align="center" id="size-definitions">Size definitions</h2> webpack-bundle-analyzer reports three values for sizes. `defaultSizes` can be used to control which of these is shown by default. The different reported sizes are: diff --git a/package.json b/package.json index 80630f19..8939dee3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "webpack-bundle-analyzer", - "version": "4.10.2", + "version": "4.10.3", "description": "Webpack plugin and CLI utility that represents bundle content as convenient interactive zoomable treemap", "author": "Yury Grunin <grunin.ya@ya.ru>", "license": "MIT", diff --git a/src/bin/analyzer.js b/src/bin/analyzer.js index c5535bdb..93251681 100755 --- a/src/bin/analyzer.js +++ b/src/bin/analyzer.js @@ -10,6 +10,9 @@ const viewer = require('../viewer'); const Logger = require('../Logger'); const utils = require('../utils'); +const options = require("./bundle-analyzer-flags.js"); +const { cli } = require("webpack"); + const SIZES = new Set(['stat', 'parsed', 'gzip']); const program = commander @@ -24,57 +27,44 @@ const program = commander You should provided it if you want analyzer to show you the real parsed module sizes. By default a directory of stats file is used.` ) - .option( - '-m, --mode <mode>', - 'Analyzer mode. Should be `server`,`static` or `json`.' + - br('In `server` mode analyzer will start HTTP server to show bundle report.') + - br('In `static` mode single HTML file with bundle report will be generated.') + - br('In `json` mode single JSON file with bundle report will be generated.'), - 'server' - ) - .option( - // Had to make `host` parameter optional in order to let `-h` flag output help message - // Fixes https://github.com/webpack-contrib/webpack-bundle-analyzer/issues/239 - '-h, --host [host]', - 'Host that will be used in `server` mode to start HTTP server.', - '127.0.0.1' - ) - .option( - '-p, --port <n>', - 'Port that will be used in `server` mode to start HTTP server.', - 8888 - ) - .option( - '-r, --report <file>', - 'Path to bundle report file that will be generated in `static` mode.' - ) - .option( - '-t, --title <title>', - 'String to use in title element of html report.' - ) - .option( - '-s, --default-sizes <type>', - 'Module sizes to show in treemap by default.' + - br(`Possible values: ${[...SIZES].join(', ')}`), - 'parsed' - ) - .option( - '-O, --no-open', - "Don't open report in default browser automatically." - ) - .option( - '-e, --exclude <regexp>', - 'Assets that should be excluded from the report.' + - br('Can be specified multiple times.'), - array() - ) - .option( - '-l, --log-level <level>', - 'Log level.' + - br(`Possible values: ${[...Logger.levels].join(', ')}`), - Logger.defaultLevel - ) - .parse(process.argv); + + const logger = new Logger(logLevel); + + Object.entries(options).forEach(([key, value]) => { + const optionName = key.replace(/([A-Z])/g, "-$1").toLowerCase(); + const defaultValue = value.configs[0].defaultValue; + const description = value.description; + + switch (value.configs[0].type) { + case "boolean": + program.option( + `-${optionName.charAt(0)}, --${optionName}`, + description, + defaultValue + ); + break; + case "enum": + const values = value.configs[0].values.join("|"); + program.option( + `-${optionName.charAt(0)}, --${optionName} <${values}>`, + description, + defaultValue + ); + break; + case "string": + case "number": + program.option( + `-${optionName.charAt(0)}, --${optionName} <value>`, + description, + defaultValue.toString() + ); + break; + default: + logger.warn(`Unknown type: ${value.configs[0].type}`); + } + }); + + program.parse(process.argv); let [bundleStatsFile, bundleDir] = program.args; let { @@ -88,7 +78,13 @@ let { open: openBrowser, exclude: excludeAssets } = program.opts(); -const logger = new Logger(logLevel); + +try { + cli.processArguments(options, process.argv); +} catch (problem) { + logger.error(`An error occurred processing arguments: ${problem}`); + process.exit(1); +} if (typeof reportTitle === 'undefined') { reportTitle = utils.defaultTitle; diff --git a/src/bin/bundle-analyzer-flags.js b/src/bin/bundle-analyzer-flags.js new file mode 100644 index 00000000..7780ff65 --- /dev/null +++ b/src/bin/bundle-analyzer-flags.js @@ -0,0 +1,165 @@ +"use strict"; + +module.exports = { + version: { + configs: [ + { + type: "boolean", + multiple: false, + description: "Output the version number", + path: "version", + }, + ], + description: "Output the version number", + simpleType: "boolean", + multiple: false, + }, + mode: { + configs: [ + { + type: "enum", + values: ["server", "static", "json"], + multiple: false, + description: + "The mode to run the analyzer in: server, static, or json.", + path: "mode", + defaultValue: "server", + }, + ], + description: "The mode to run the analyzer in: server, static, or json.", + simpleType: "string", + multiple: false, + }, + report: { + configs: [ + { + type: "string", + multiple: false, + description: + 'Path to bundle report file that will be generated in "static" mode.', + path: "report", + defaultValue: "report.html", + }, + ], + description: + 'Path to bundle report file that will be generated in "static" mode.', + simpleType: "string", + multiple: false, + }, + title: { + configs: [ + { + type: "string", + multiple: false, + description: "String to use in title element of html report.", + path: "title", + defaultValue: "Webpack Bundle Report", + }, + ], + description: "String to use in title element of html report.", + simpleType: "string", + multiple: false, + }, + size: { + configs: [ + { + type: "enum", + values: ["stat", "parsed", "gzip"], + multiple: false, + description: "Module sizes to show in treemap by default.", + path: "defaultSizes", + defaultValue: "parsed", + }, + ], + description: "Module sizes to show in treemap by default.", + simpleType: "string", + multiple: false, + }, + "Dont Open": { + configs: [ + { + type: "boolean", + multiple: false, + description: "Don't open report in default browser automatically", + path: "noOpen", + defaultValue: false, + }, + ], + description: "Don't open report in default browser automatically", + simpleType: "boolean", + multiple: false, + }, + port: { + configs: [ + { + type: "number", + multiple: false, + description: + "Port that will be used in `server` mode, default is 8888.", + path: "port", + defaultValue: 8888, + }, + ], + description: "Port that will be used in `server` mode, default is 8888.", + simpleType: "number", + multiple: false, + }, + host: { + configs: [ + { + type: "string", + multiple: false, + description: + "Host that will be used in `server` mode, default is 127.0.0.1.", + path: "host", + defaultValue: "127.0.0.1", + }, + ], + description: + "Host that will be used in `server` mode, default is 127.0.0.1.", + simpleType: "string", + multiple: false, + }, + "log-level": { + configs: [ + { + type: "enum", + values: ["debug", "info", "warn", "error", "silent"], + multiple: true, + description: "Level of logger (info, warn, error, silent).", + path: "logLevel", + defaultValue: "info", + }, + ], + description: "Level of logger (info, warn, error, silent).", + simpleType: "string", + multiple: true, + }, + exclude: { + configs: [ + { + type: "string", + multiple: false, + description: "Assets that should be excluded from the report.", + path: "exclude", + defaultValue: "", + }, + ], + description: "Assets that should be excluded from the report.", + simpleType: "string", + multiple: false, + }, + help: { + configs: [ + { + type: "boolean", + multiple: false, + description: "output usage information", + path: "help", + }, + ], + description: "Output usage information", + simpleType: "boolean", + multiple: false, + }, +};