From a04d649050d47cd0865bf7c67e42666efcf0a15f Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Wed, 17 Jun 2020 23:37:34 +0530 Subject: [PATCH 1/3] chore: consume webpack built-in logger --- src/Logger.js | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/Logger.js b/src/Logger.js index cdd7ae10..35e8c201 100644 --- a/src/Logger.js +++ b/src/Logger.js @@ -6,14 +6,28 @@ const LEVELS = [ 'silent' ]; +const loggerMethods = [ + 'log', + 'trace', + 'group', + 'groupEnd', + 'groupEndCollapsed', + 'status', + 'clear', + 'profile' +]; + const LEVEL_TO_CONSOLE_METHOD = new Map([ ['debug', 'log'], ['info', 'log'], ['warn', 'log'] ]); -class Logger { +const webpackLogger = require('webpack/lib/logging/runtime') +if (webpackLogger.getLogger) LEVELS.push(...loggerMethods); + +class Logger { static levels = LEVELS; static defaultLevel = 'info'; @@ -35,7 +49,11 @@ class Logger { } _log(level, ...args) { - console[LEVEL_TO_CONSOLE_METHOD.get(level) || level](...args); + if (webpackLogger.getLogger) { + webpackLogger.getLogger('webpack-bundle-analyzer')[level](...args) + } else { + console[LEVEL_TO_CONSOLE_METHOD.get(level) || level](...args); + } } }; @@ -48,4 +66,4 @@ LEVELS.forEach(level => { }; }); -module.exports = Logger; +module.exports = Logger From d277ab028295052c4123491f901ce5bd17fa9f31 Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Thu, 18 Jun 2020 18:13:24 +0530 Subject: [PATCH 2/3] chore: respect default logging level from webpack --- src/BundleAnalyzerPlugin.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/BundleAnalyzerPlugin.js b/src/BundleAnalyzerPlugin.js index 6d294730..1fa5253a 100644 --- a/src/BundleAnalyzerPlugin.js +++ b/src/BundleAnalyzerPlugin.js @@ -34,6 +34,10 @@ class BundleAnalyzerPlugin { apply(compiler) { this.compiler = compiler; + // Respect default logging level from webpack + const defaultLoggerLevel = compiler.options.infrastructureLogging && compiler.options.infrastructureLogging.level ? compiler.options.infrastructureLogging.level : this.opts.logLevel; + this.logger = compiler.getInfrastructureLogger ? compiler.getInfrastructureLogger('webpack-bundle-analyzer') : new Logger(defaultLoggerLevel); + const done = (stats, callback) => { callback = callback || (() => {}); From 33a7f5295a08dd017d295b38fc6d97a8f94abb38 Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Thu, 18 Jun 2020 18:19:06 +0530 Subject: [PATCH 3/3] fix: lint --- src/BundleAnalyzerPlugin.js | 7 ++++--- src/Logger.js | 6 +++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/BundleAnalyzerPlugin.js b/src/BundleAnalyzerPlugin.js index 1fa5253a..20b10443 100644 --- a/src/BundleAnalyzerPlugin.js +++ b/src/BundleAnalyzerPlugin.js @@ -28,15 +28,16 @@ class BundleAnalyzerPlugin { }; this.server = null; - this.logger = new Logger(this.opts.logLevel); } apply(compiler) { this.compiler = compiler; // Respect default logging level from webpack - const defaultLoggerLevel = compiler.options.infrastructureLogging && compiler.options.infrastructureLogging.level ? compiler.options.infrastructureLogging.level : this.opts.logLevel; - this.logger = compiler.getInfrastructureLogger ? compiler.getInfrastructureLogger('webpack-bundle-analyzer') : new Logger(defaultLoggerLevel); + const defaultLoggerLevel = compiler.options.infrastructureLogging && compiler.options.infrastructureLogging.level ? + compiler.options.infrastructureLogging.level : this.opts.logLevel; + this.logger = compiler.getInfrastructureLogger ? + compiler.getInfrastructureLogger('webpack-bundle-analyzer') : new Logger(defaultLoggerLevel); const done = (stats, callback) => { callback = callback || (() => {}); diff --git a/src/Logger.js b/src/Logger.js index 35e8c201..77478bad 100644 --- a/src/Logger.js +++ b/src/Logger.js @@ -23,7 +23,7 @@ const LEVEL_TO_CONSOLE_METHOD = new Map([ ['warn', 'log'] ]); -const webpackLogger = require('webpack/lib/logging/runtime') +const webpackLogger = require('webpack/lib/logging/runtime'); if (webpackLogger.getLogger) LEVELS.push(...loggerMethods); @@ -50,7 +50,7 @@ class Logger { _log(level, ...args) { if (webpackLogger.getLogger) { - webpackLogger.getLogger('webpack-bundle-analyzer')[level](...args) + webpackLogger.getLogger('webpack-bundle-analyzer')[level](...args); } else { console[LEVEL_TO_CONSOLE_METHOD.get(level) || level](...args); } @@ -66,4 +66,4 @@ LEVELS.forEach(level => { }; }); -module.exports = Logger +module.exports = Logger;