From 45b4db76dff3f3b21cea5ae7f9859c60f2cb0f7d Mon Sep 17 00:00:00 2001 From: southorange1228 <15280970040@163.com> Date: Tue, 16 Aug 2022 14:42:09 +0800 Subject: [PATCH 1/5] feat: support custom loginfo with server mode --- src/BundleAnalyzerPlugin.js | 4 +++- src/utils.js | 9 +++++++++ src/viewer.js | 8 ++++++-- test/viewer.js | 3 ++- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/BundleAnalyzerPlugin.js b/src/BundleAnalyzerPlugin.js index 9e5c2842..c1b065fa 100644 --- a/src/BundleAnalyzerPlugin.js +++ b/src/BundleAnalyzerPlugin.js @@ -23,6 +23,7 @@ class BundleAnalyzerPlugin { logLevel: 'info', // deprecated startAnalyzer: true, + logInfo: utils.defaultLogInfo, ...opts, analyzerPort: 'analyzerPort' in opts ? (opts.analyzerPort === 'auto' ? 0 : opts.analyzerPort) : 8888 }; @@ -107,7 +108,8 @@ class BundleAnalyzerPlugin { bundleDir: this.getBundleDirFromCompiler(), logger: this.logger, defaultSizes: this.opts.defaultSizes, - excludeAssets: this.opts.excludeAssets + excludeAssets: this.opts.excludeAssets, + logInfo: this.opts.logInfo }); } } diff --git a/src/utils.js b/src/utils.js index be4e40cf..b4157782 100644 --- a/src/utils.js +++ b/src/utils.js @@ -53,6 +53,15 @@ exports.defaultTitle = function () { return `${process.env.npm_package_name || 'Webpack Bundle Analyzer'} [${currentTime}]`; }; +exports.defaultLogInfo = function (options) { + const {host, port} = options; + const url = `http://${host}:${port}`; + return ( + `${bold("Webpack Bundle Analyzer")} is started at ${bold(url)}\n` + + `Use ${bold("Ctrl+C")} to close it` + ); +}; + /** * Calls opener on a URI, but silently try / catches it. */ diff --git a/src/viewer.js b/src/viewer.js index 87304f54..58730d90 100644 --- a/src/viewer.js +++ b/src/viewer.js @@ -39,7 +39,8 @@ async function startServer(bundleStats, opts) { logger = new Logger(), defaultSizes = 'parsed', excludeAssets = null, - reportTitle + reportTitle, + logInfo } = opts || {}; const analyzerOpts = {logger, excludeAssets}; @@ -73,7 +74,10 @@ async function startServer(bundleStats, opts) { server.listen(port, host, () => { resolve(); - const url = `http://${host}:${server.address().port}`; + const url = logInfo({ + host, + port: server.address().port + }); logger.info( `${bold('Webpack Bundle Analyzer')} is started at ${bold(url)}\n` + diff --git a/test/viewer.js b/test/viewer.js index 717b1e1d..515723d3 100644 --- a/test/viewer.js +++ b/test/viewer.js @@ -16,7 +16,8 @@ describe('WebSocket server', function () { const options = { openBrowser: false, logger: new Logger('silent'), - port: 0 + port: 0, + logInfo: () => '' }; startServer(bundleStats, options) From 86991e808749e97ee5c59d1bb9ef84fc52a4c695 Mon Sep 17 00:00:00 2001 From: southorange1228 <15280970040@163.com> Date: Tue, 16 Aug 2022 17:20:57 +0800 Subject: [PATCH 2/5] fix: change logInfo to analyzerUrl & change some logic --- src/BundleAnalyzerPlugin.js | 4 ++-- src/utils.js | 10 +++------- src/viewer.js | 9 +++++---- test/viewer.js | 2 +- 4 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/BundleAnalyzerPlugin.js b/src/BundleAnalyzerPlugin.js index c1b065fa..8f493aab 100644 --- a/src/BundleAnalyzerPlugin.js +++ b/src/BundleAnalyzerPlugin.js @@ -23,7 +23,7 @@ class BundleAnalyzerPlugin { logLevel: 'info', // deprecated startAnalyzer: true, - logInfo: utils.defaultLogInfo, + analyzerUrl: utils.defaultAnalyzerUrl, ...opts, analyzerPort: 'analyzerPort' in opts ? (opts.analyzerPort === 'auto' ? 0 : opts.analyzerPort) : 8888 }; @@ -109,7 +109,7 @@ class BundleAnalyzerPlugin { logger: this.logger, defaultSizes: this.opts.defaultSizes, excludeAssets: this.opts.excludeAssets, - logInfo: this.opts.logInfo + analyzerUrl: this.opts.analyzerUrl }); } } diff --git a/src/utils.js b/src/utils.js index b4157782..5886871a 100644 --- a/src/utils.js +++ b/src/utils.js @@ -53,13 +53,9 @@ exports.defaultTitle = function () { return `${process.env.npm_package_name || 'Webpack Bundle Analyzer'} [${currentTime}]`; }; -exports.defaultLogInfo = function (options) { - const {host, port} = options; - const url = `http://${host}:${port}`; - return ( - `${bold("Webpack Bundle Analyzer")} is started at ${bold(url)}\n` + - `Use ${bold("Ctrl+C")} to close it` - ); +exports.defaultAnalyzerUrl = function (options) { + const {listenHost, boundAddress} = options; + return `http://${listenHost}:${boundAddress.port}`; }; /** diff --git a/src/viewer.js b/src/viewer.js index 58730d90..3d071140 100644 --- a/src/viewer.js +++ b/src/viewer.js @@ -40,7 +40,7 @@ async function startServer(bundleStats, opts) { defaultSizes = 'parsed', excludeAssets = null, reportTitle, - logInfo + analyzerUrl } = opts || {}; const analyzerOpts = {logger, excludeAssets}; @@ -74,9 +74,10 @@ async function startServer(bundleStats, opts) { server.listen(port, host, () => { resolve(); - const url = logInfo({ - host, - port: server.address().port + const url = analyzerUrl({ + listenPort: port, + listenHost: host, + boundAddress: server.address() }); logger.info( diff --git a/test/viewer.js b/test/viewer.js index 515723d3..27fda99b 100644 --- a/test/viewer.js +++ b/test/viewer.js @@ -17,7 +17,7 @@ describe('WebSocket server', function () { openBrowser: false, logger: new Logger('silent'), port: 0, - logInfo: () => '' + analyzerUrl: () => '' }; startServer(bundleStats, options) From d70bb0f8c089e59a17a5821abb8bd5e9ef90ecb7 Mon Sep 17 00:00:00 2001 From: southorange1228 <15280970040@163.com> Date: Tue, 16 Aug 2022 19:24:57 +0800 Subject: [PATCH 3/5] docs: update CHANGELOG --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7abc54d7..3bb80e00 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ _Note: Gaps between patch versions are faulty, broken or test releases._ ## UNRELEASED +* **New Feature** + * Support custom server log with server mode ([#520](https://github.com/webpack-contrib/webpack-bundle-analyzer/pull/520) by [@southorange1228](https://github.com/southorange1228)) ## 4.5.0 From bbf987f49fe70e3cc3843859552497943dc236a4 Mon Sep 17 00:00:00 2001 From: southorange1228 <15280970040@163.com> Date: Tue, 16 Aug 2022 20:21:30 +0800 Subject: [PATCH 4/5] docs: update README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index ebd37218..4df0b58f 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,7 @@ new BundleAnalyzerPlugin(options?: object) |**`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`. | |**`analyzerHost`**|`{String}`|Default: `127.0.0.1`. Host that will be used in `server` mode to start HTTP server.| |**`analyzerPort`**|`{Number}` or `auto`|Default: `8888`. Port that will be used in `server` mode to start HTTP server.| +|**`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.| |**`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).| |**`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.| |**`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.| From e1f3398e9ad0228057a19083be358bc924c2e43a Mon Sep 17 00:00:00 2001 From: Vesa Laakso <482561+valscion@users.noreply.github.com> Date: Wed, 17 Aug 2022 09:54:43 +0300 Subject: [PATCH 5/5] docs: Tweak analyzerUrl changelog entry --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3bb80e00..c10e2838 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,8 +11,9 @@ _Note: Gaps between patch versions are faulty, broken or test releases._ ## UNRELEASED + * **New Feature** - * Support custom server log with server mode ([#520](https://github.com/webpack-contrib/webpack-bundle-analyzer/pull/520) by [@southorange1228](https://github.com/southorange1228)) + * Support outputting different URL in server mode ([#520](https://github.com/webpack-contrib/webpack-bundle-analyzer/pull/520) by [@southorange1228](https://github.com/southorange1228)) ## 4.5.0