Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Allow flags to support the webpack-cli #628

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand Down
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ webpack-bundle-analyzer bundle/output/path/stats.json

```bash
webpack-bundle-analyzer <bundleStatsFile> [bundleDir] [options]
webpack build --analyze [cliOptions]
```

Arguments are documented below:
Expand Down Expand Up @@ -130,6 +131,28 @@ Directory containing all generated bundles.
-h, --help output usage information
```

### `cliOptions`

```
--analyze-version output the version number
--analyze-mode <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> Host that will be used in `server` mode to start HTTP server. (default: 127.0.0.1)
--analyze-port <n> Port that will be used in `server` mode to start HTTP server. Should be a number or `auto` (default: 8888)
--analyze-report <file> Path to bundle report file that will be generated in `static` mode. (default: report.html)
--analyze-title <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:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
100 changes: 48 additions & 52 deletions src/bin/analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
const Logger = require('../Logger');
const utils = require('../utils');

const options = require("./bundle-analyzer-flags.js");

Check failure on line 13 in src/bin/analyzer.js

View workflow job for this annotation

GitHub Actions / lint

Strings must use singlequote
const { cli } = require("webpack");

Check failure on line 14 in src/bin/analyzer.js

View workflow job for this annotation

GitHub Actions / lint

There should be no space after '{'

Check failure on line 14 in src/bin/analyzer.js

View workflow job for this annotation

GitHub Actions / lint

There should be no space before '}'

Check failure on line 14 in src/bin/analyzer.js

View workflow job for this annotation

GitHub Actions / lint

Strings must use singlequote

const SIZES = new Set(['stat', 'parsed', 'gzip']);

const program = commander
Expand All @@ -23,58 +26,45 @@
bundleDir Directory containing all generated bundles.
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.`
)

Check failure on line 29 in src/bin/analyzer.js

View workflow job for this annotation

GitHub Actions / lint

Missing semicolon
.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);

Check failure on line 31 in src/bin/analyzer.js

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 0 spaces but found 2

Check warning on line 31 in src/bin/analyzer.js

View workflow job for this annotation

GitHub Actions / lint

'logLevel' was used before it was defined

Object.entries(options).forEach(([key, value]) => {

Check failure on line 33 in src/bin/analyzer.js

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 0 spaces but found 2
const optionName = key.replace(/([A-Z])/g, "-$1").toLowerCase();

Check failure on line 34 in src/bin/analyzer.js

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 2 spaces but found 4

Check failure on line 34 in src/bin/analyzer.js

View workflow job for this annotation

GitHub Actions / lint

Use the 'u' flag

Check failure on line 34 in src/bin/analyzer.js

View workflow job for this annotation

GitHub Actions / lint

Strings must use singlequote
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 {
Expand All @@ -88,7 +78,13 @@
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;
Expand Down
165 changes: 165 additions & 0 deletions src/bin/bundle-analyzer-flags.js
Original file line number Diff line number Diff line change
@@ -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,
},
};