Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/use-webpack-infrastructure-logger.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"webpack-bundle-analyzer": minor
---

Use Webpack's infrastructure logger when available (`compiler.getInfrastructureLogger('webpack-bundle-analyzer')`) and deprecate the plugin's `logLevel` option in favor of Webpack's native `infrastructureLogging` configuration.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ new BundleAnalyzerPlugin(options?: object)
| **`statsFilename`** | `{String}` | Default: `stats.json`. Name of webpack stats JSON file that will be generated if `generateStatsFile` is `true`. It can be either an absolute path or a path relative to a bundle output directory (which is output.path in webpack config). |
| **`statsOptions`** | `null` or `{Object}` | Default: `null`. Options for `stats.toJson()` method. For example you can exclude sources of your modules from stats file with `source: false` option. [See more options here](https://webpack.js.org/configuration/stats/). |
| **`excludeAssets`** | `{null\|pattern\|pattern[]}` where `pattern` equals to `{String\|RegExp\|function}` | Default: `null`. Patterns that will be used to match against asset names to exclude them from the report. If pattern is a string it will be converted to RegExp via `new RegExp(str)`. If pattern is a function it should have the following signature `(assetName: string) => boolean` and should return `true` to _exclude_ matching asset. If multiple patterns are provided asset should match at least one of them to be excluded. |
| **`logLevel`** | One of: `info`, `warn`, `error`, `silent` | Default: `info`. Used to control how much details the plugin outputs. |
| **`logLevel`** | One of: `info`, `warn`, `error`, `silent` | **Deprecated**. Default: `info`. Used to control how much details the plugin outputs. Please use webpack's [`infrastructureLogging`](https://webpack.js.org/configuration/infrastructureLogging/) configuration instead. |

### Absolute output paths

Expand Down
20 changes: 19 additions & 1 deletion src/BundleAnalyzerPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const viewer = require("./viewer");
/** @typedef {import("webpack").StatsCompilation} StatsCompilation */
/** @typedef {import("./sizeUtils").Algorithm} CompressionAlgorithm */
/** @typedef {import("./Logger").Level} LogLever */
/** @typedef {ReturnType<import("webpack").Compiler["getInfrastructureLogger"]>} WebpackLogger */
/** @typedef {import("./viewer").ViewerServerObj} ViewerServerObj */

/** @typedef {string | boolean | StatsOptions} PluginStatsOptions */
Expand Down Expand Up @@ -70,7 +71,7 @@ const analyzerStatsOptions = {
* @property {string=} statsFilename stats filename
* @property {PluginStatsOptions=} statsOptions stats options
* @property {ExcludeAssets=} excludeAssets exclude assets
* @property {LogLever=} logLevel exclude assets
* @property {LogLever=} logLevel (deprecated) log level
* @property {boolean=} startAnalyzer start analyzer
* @property {AnalyzerUrl=} analyzerUrl start analyzer
*/
Expand All @@ -80,6 +81,8 @@ class BundleAnalyzerPlugin {
* @param {Options=} opts options
*/
constructor(opts = {}) {
const hasCustomLogLevel = typeof opts.logLevel !== "undefined";

/** @type {Required<Omit<Options, "analyzerPort" | "statsOptions">> & { analyzerPort: number, statsOptions: undefined | PluginStatsOptions }} */
this.opts = {
analyzerMode: "server",
Expand All @@ -102,10 +105,13 @@ class BundleAnalyzerPlugin {
opts.analyzerPort === "auto" ? 0 : (opts.analyzerPort ?? 8888),
};

/** @type {boolean} */
this.hasCustomLogLevel = hasCustomLogLevel;
/** @type {Compiler | null} */
this.compiler = null;
/** @type {Promise<ViewerServerObj> | null} */
this.server = null;
/** @type {Logger | WebpackLogger} */
this.logger = new Logger(this.opts.logLevel);
}

Expand All @@ -115,6 +121,18 @@ class BundleAnalyzerPlugin {
apply(compiler) {
this.compiler = compiler;

if (compiler.getInfrastructureLogger) {
const infraLogger = compiler.getInfrastructureLogger(
"webpack-bundle-analyzer",
);
this.logger = Logger.createInfrastructureLoggerAdapter(
infraLogger,
this.hasCustomLogLevel ? this.opts.logLevel : undefined,
);
} else {
this.logger = new Logger(this.opts.logLevel);
}

/**
* @param {Stats} stats stats
* @param {(err?: Error) => void} callback callback
Expand Down
Loading