Skip to content

Commit

Permalink
feat: support custom loginfo with server mode
Browse files Browse the repository at this point in the history
  • Loading branch information
southorange1228 committed Aug 16, 2022
1 parent 7d6039e commit 45b4db7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/BundleAnalyzerPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand Down Expand Up @@ -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
});
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
8 changes: 6 additions & 2 deletions src/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ async function startServer(bundleStats, opts) {
logger = new Logger(),
defaultSizes = 'parsed',
excludeAssets = null,
reportTitle
reportTitle,
logInfo
} = opts || {};

const analyzerOpts = {logger, excludeAssets};
Expand Down Expand Up @@ -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` +
Expand Down
3 changes: 2 additions & 1 deletion test/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ describe('WebSocket server', function () {
const options = {
openBrowser: false,
logger: new Logger('silent'),
port: 0
port: 0,
logInfo: () => ''
};

startServer(bundleStats, options)
Expand Down

0 comments on commit 45b4db7

Please sign in to comment.