Skip to content

Commit 16d8e07

Browse files
richerfuvalscion
andauthored
feat: support custom loginfo with server mode (#520)
* feat: support custom loginfo with server mode * fix: change logInfo to analyzerUrl & change some logic * docs: update CHANGELOG * docs: update README * docs: Tweak analyzerUrl changelog entry Co-authored-by: Vesa Laakso <[email protected]>
1 parent 7d6039e commit 16d8e07

File tree

6 files changed

+21
-4
lines changed

6 files changed

+21
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ _Note: Gaps between patch versions are faulty, broken or test releases._
1212

1313
## UNRELEASED
1414

15+
* **New Feature**
16+
* Support outputting different URL in server mode ([#520](https://github.com/webpack-contrib/webpack-bundle-analyzer/pull/520) by [@southorange1228](https://github.com/southorange1228))
17+
1518
## 4.5.0
1619

1720
* **Improvement**

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ new BundleAnalyzerPlugin(options?: object)
5959
|**`analyzerMode`**|One of: `server`, `static`, `json`, `disabled`|Default: `server`. 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. In `disabled` mode you can use this plugin to just generate Webpack Stats JSON file by setting `generateStatsFile` to `true`. |
6060
|**`analyzerHost`**|`{String}`|Default: `127.0.0.1`. Host that will be used in `server` mode to start HTTP server.|
6161
|**`analyzerPort`**|`{Number}` or `auto`|Default: `8888`. Port that will be used in `server` mode to start HTTP server.|
62+
|**`analyzerUrl`**|`{Function}` called with `{ listenHost: string, listenHost: string, boundAddress: server.address}`. [server.address comes from Node.js](https://nodejs.org/api/net.html#serveraddress)| Default: `http://${listenHost}:${boundAddress.port}`. The URL printed to console with server mode.|
6263
|**`reportFilename`**|`{String}`|Default: `report.html`. Path to bundle report file that will be generated in `static` mode. It can be either an absolute path or a path relative to a bundle output directory (which is output.path in webpack config).|
6364
|**`reportTitle`**|`{String\|function}`|Default: function that returns pretty printed current date and time. Content of the HTML `title` element; or a function of the form `() => string` that provides the content.|
6465
|**`defaultSizes`**|One of: `stat`, `parsed`, `gzip`|Default: `parsed`. Module sizes to show in report by default. [Size definitions](#size-definitions) section describes what these values mean.|

src/BundleAnalyzerPlugin.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class BundleAnalyzerPlugin {
2323
logLevel: 'info',
2424
// deprecated
2525
startAnalyzer: true,
26+
analyzerUrl: utils.defaultAnalyzerUrl,
2627
...opts,
2728
analyzerPort: 'analyzerPort' in opts ? (opts.analyzerPort === 'auto' ? 0 : opts.analyzerPort) : 8888
2829
};
@@ -107,7 +108,8 @@ class BundleAnalyzerPlugin {
107108
bundleDir: this.getBundleDirFromCompiler(),
108109
logger: this.logger,
109110
defaultSizes: this.opts.defaultSizes,
110-
excludeAssets: this.opts.excludeAssets
111+
excludeAssets: this.opts.excludeAssets,
112+
analyzerUrl: this.opts.analyzerUrl
111113
});
112114
}
113115
}

src/utils.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ exports.defaultTitle = function () {
5353
return `${process.env.npm_package_name || 'Webpack Bundle Analyzer'} [${currentTime}]`;
5454
};
5555

56+
exports.defaultAnalyzerUrl = function (options) {
57+
const {listenHost, boundAddress} = options;
58+
return `http://${listenHost}:${boundAddress.port}`;
59+
};
60+
5661
/**
5762
* Calls opener on a URI, but silently try / catches it.
5863
*/

src/viewer.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ async function startServer(bundleStats, opts) {
3939
logger = new Logger(),
4040
defaultSizes = 'parsed',
4141
excludeAssets = null,
42-
reportTitle
42+
reportTitle,
43+
analyzerUrl
4344
} = opts || {};
4445

4546
const analyzerOpts = {logger, excludeAssets};
@@ -73,7 +74,11 @@ async function startServer(bundleStats, opts) {
7374
server.listen(port, host, () => {
7475
resolve();
7576

76-
const url = `http://${host}:${server.address().port}`;
77+
const url = analyzerUrl({
78+
listenPort: port,
79+
listenHost: host,
80+
boundAddress: server.address()
81+
});
7782

7883
logger.info(
7984
`${bold('Webpack Bundle Analyzer')} is started at ${bold(url)}\n` +

test/viewer.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ describe('WebSocket server', function () {
1616
const options = {
1717
openBrowser: false,
1818
logger: new Logger('silent'),
19-
port: 0
19+
port: 0,
20+
analyzerUrl: () => ''
2021
};
2122

2223
startServer(bundleStats, options)

0 commit comments

Comments
 (0)