From dd8091c6894a509916537b37e0e4f3cc983a76f9 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Tue, 9 Feb 2021 23:48:37 +0530 Subject: [PATCH 1/4] feat: display used config path when logging level=log --- .../__tests__/applyCLIPlugin.test.js | 4 +- packages/webpack-cli/lib/plugins/CLIPlugin.js | 9 +++ packages/webpack-cli/lib/webpack-cli.js | 2 +- .../core-flags/infrastructure-logging.test.js | 55 ++++++++++++++++++- 4 files changed, 66 insertions(+), 4 deletions(-) diff --git a/packages/webpack-cli/__tests__/applyCLIPlugin.test.js b/packages/webpack-cli/__tests__/applyCLIPlugin.test.js index 7c90277c5a1..7889a7af624 100644 --- a/packages/webpack-cli/__tests__/applyCLIPlugin.test.js +++ b/packages/webpack-cli/__tests__/applyCLIPlugin.test.js @@ -5,7 +5,7 @@ const applyCLIPlugin = new webpackCLI().applyCLIPlugin; describe('CLIPluginResolver', () => { it('should add CLI plugin to single compiler object', async () => { - const result = await applyCLIPlugin({ options: {} }, { hot: true, prefetch: true }); + const result = await applyCLIPlugin({ options: {}, path: new WeakMap() }, { hot: true, prefetch: true }); expect(result.options.plugins[0] instanceof CLIPlugin).toBeTruthy(); expect(result.options.plugins[0].options).toEqual({ configPath: undefined, @@ -18,7 +18,7 @@ describe('CLIPluginResolver', () => { }); it('should add CLI plugin to multi compiler object', async () => { - const result = await applyCLIPlugin({ options: [{}, {}] }, { hot: true, prefetch: true }); + const result = await applyCLIPlugin({ options: [{}, {}], path: new WeakMap() }, { hot: true, prefetch: true }); expect(result.options[0].plugins[0] instanceof CLIPlugin).toBeTruthy(); expect(result.options[1].plugins[0] instanceof CLIPlugin).toBeTruthy(); expect(result.options).toEqual([ diff --git a/packages/webpack-cli/lib/plugins/CLIPlugin.js b/packages/webpack-cli/lib/plugins/CLIPlugin.js index 350ee33e573..b6a2ead5562 100644 --- a/packages/webpack-cli/lib/plugins/CLIPlugin.js +++ b/packages/webpack-cli/lib/plugins/CLIPlugin.js @@ -41,7 +41,12 @@ class CLIPlugin { const pluginName = 'webpack-cli'; const getCompilationName = () => (compiler.name ? ` '${compiler.name}'` : ''); + const { configPath } = this.options; + compiler.hooks.run.tap(pluginName, () => { + if (configPath) { + this.logger.log(`Using config ${configPath}`); + } this.logger.log(`Compilation${getCompilationName()} starting...`); }); @@ -52,6 +57,10 @@ class CLIPlugin { this.logger.warn('You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.'); } + if (configPath) { + this.logger.log(`Using config ${configPath}`); + } + this.logger.log(`Compilation${getCompilationName()} starting...`); }); diff --git a/packages/webpack-cli/lib/webpack-cli.js b/packages/webpack-cli/lib/webpack-cli.js index 6e049c5b19d..4ce7ad3254f 100644 --- a/packages/webpack-cli/lib/webpack-cli.js +++ b/packages/webpack-cli/lib/webpack-cli.js @@ -1631,7 +1631,7 @@ class WebpackCLI { configOptions.plugins.unshift( new CLIPlugin({ - configPath: config.path, + configPath: config.path.get(configOptions), helpfulOutput: !cliOptions.json, hot: cliOptions.hot, progress: cliOptions.progress, diff --git a/test/core-flags/infrastructure-logging.test.js b/test/core-flags/infrastructure-logging.test.js index 9e408f2d46b..ee25c3f6d68 100644 --- a/test/core-flags/infrastructure-logging.test.js +++ b/test/core-flags/infrastructure-logging.test.js @@ -1,6 +1,7 @@ 'use strict'; -const { run } = require('../utils/test-utils'); +const { run, runWatch } = require('../utils/test-utils'); +const { resolve } = require('path'); describe('infrastructure logging related flag', () => { it('should set infrastructureLogging.debug properly', () => { @@ -27,4 +28,56 @@ describe('infrastructure logging related flag', () => { expect(stderr).toContain("Compilation 'compiler' finished"); expect(stdout).toContain(`level: 'log'`); }); + + it('should log used default config when level is log', () => { + const { exitCode, stderr, stdout } = run(__dirname, ['--infrastructure-logging-level', 'log']); + + expect(exitCode).toBe(0); + const configPath = resolve(__dirname, './webpack.config.js'); + expect(stderr).toContain("Compilation 'compiler' starting..."); + expect(stderr).toContain(`Using config ${configPath}`); + expect(stderr).toContain("Compilation 'compiler' finished"); + expect(stdout).toContain(`level: 'log'`); + }); + + it('should log used supplied config when level is log', () => { + const { exitCode, stderr, stdout } = run(__dirname, [ + '--config', + 'webpack.cache.config.js', + '--infrastructure-logging-level', + 'log', + ]); + + expect(exitCode).toBe(0); + const configPath = resolve(__dirname, './webpack.cache.config.js'); + expect(stderr).toContain(`Using config ${configPath}`); + expect(stdout).toContain(`level: 'log'`); + }); + + it('should log used supplied config with watch', async () => { + const { stderr, stdout } = await runWatch(__dirname, [ + '--config', + 'webpack.cache.config.js', + '--infrastructure-logging-level', + 'log', + ]); + + const configPath = resolve(__dirname, './webpack.cache.config.js'); + expect(stderr).toContain(`Using config ${configPath}`); + expect(stdout).toContain(`level: 'log'`); + }); + + it('should log used supplied config with serve', async () => { + const { stderr, stdout } = await runWatch(__dirname, [ + 'serve', + '--config', + 'webpack.cache.config.js', + '--infrastructure-logging-level', + 'log', + ]); + + const configPath = resolve(__dirname, './webpack.cache.config.js'); + expect(stderr).toContain(`Using config ${configPath}`); + expect(stdout).toContain(`level: 'log'`); + }); }); From 72f8777d59e7edf98e1f236864d89d61a0879d00 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Wed, 10 Feb 2021 00:37:21 +0530 Subject: [PATCH 2/4] feat: display used config path when logging level=log --- test/build/basic/basic.test.js | 14 +++++- .../core-flags/infrastructure-logging.test.js | 43 +------------------ test/serve/basic/serve-basic.test.js | 15 +++++++ test/watch/basic/basic.test.js | 16 +++++++ 4 files changed, 44 insertions(+), 44 deletions(-) diff --git a/test/build/basic/basic.test.js b/test/build/basic/basic.test.js index 01481187f45..56ed3503732 100644 --- a/test/build/basic/basic.test.js +++ b/test/build/basic/basic.test.js @@ -1,6 +1,6 @@ 'use strict'; - -const { run } = require('../../utils/test-utils'); +const { resolve } = require('path'); +const { run, isWebpack5 } = require('../../utils/test-utils'); describe('bundle command', () => { it('should work without command (default command)', async () => { @@ -140,4 +140,14 @@ describe('bundle command', () => { expect(stderr).toContain("Run 'webpack --help' to see available commands and options"); expect(stdout).toBeFalsy(); }); + + it('should log supplied config when logging level is log', () => { + if (isWebpack5) { + const { exitCode, stderr, stdout } = run(__dirname, ['--config', './entry.config.js', '--infrastructure-logging-level', 'log']); + expect(exitCode).toBe(0); + const configPath = resolve(__dirname, './entry.config.js'); + expect(stderr).toContain(`Using config ${configPath}`); + expect(stdout).toBeFalsy(); + } + }); }); diff --git a/test/core-flags/infrastructure-logging.test.js b/test/core-flags/infrastructure-logging.test.js index ee25c3f6d68..59f5df6c0c9 100644 --- a/test/core-flags/infrastructure-logging.test.js +++ b/test/core-flags/infrastructure-logging.test.js @@ -1,6 +1,6 @@ 'use strict'; -const { run, runWatch } = require('../utils/test-utils'); +const { run } = require('../utils/test-utils'); const { resolve } = require('path'); describe('infrastructure logging related flag', () => { @@ -39,45 +39,4 @@ describe('infrastructure logging related flag', () => { expect(stderr).toContain("Compilation 'compiler' finished"); expect(stdout).toContain(`level: 'log'`); }); - - it('should log used supplied config when level is log', () => { - const { exitCode, stderr, stdout } = run(__dirname, [ - '--config', - 'webpack.cache.config.js', - '--infrastructure-logging-level', - 'log', - ]); - - expect(exitCode).toBe(0); - const configPath = resolve(__dirname, './webpack.cache.config.js'); - expect(stderr).toContain(`Using config ${configPath}`); - expect(stdout).toContain(`level: 'log'`); - }); - - it('should log used supplied config with watch', async () => { - const { stderr, stdout } = await runWatch(__dirname, [ - '--config', - 'webpack.cache.config.js', - '--infrastructure-logging-level', - 'log', - ]); - - const configPath = resolve(__dirname, './webpack.cache.config.js'); - expect(stderr).toContain(`Using config ${configPath}`); - expect(stdout).toContain(`level: 'log'`); - }); - - it('should log used supplied config with serve', async () => { - const { stderr, stdout } = await runWatch(__dirname, [ - 'serve', - '--config', - 'webpack.cache.config.js', - '--infrastructure-logging-level', - 'log', - ]); - - const configPath = resolve(__dirname, './webpack.cache.config.js'); - expect(stderr).toContain(`Using config ${configPath}`); - expect(stdout).toContain(`level: 'log'`); - }); }); diff --git a/test/serve/basic/serve-basic.test.js b/test/serve/basic/serve-basic.test.js index d215cea596e..e3c6c876619 100644 --- a/test/serve/basic/serve-basic.test.js +++ b/test/serve/basic/serve-basic.test.js @@ -329,4 +329,19 @@ describe('basic serve usage', () => { expect(stderr).toContain("Error: Unknown option '--unknown-flag'"); expect(stdout).toBeFalsy(); }); + + it('should log used supplied config with serve', async () => { + if (isWebpack5) { + const { stderr, stdout } = await runServe(__dirname, [ + '--config', + 'webpack.config.js', + '--infrastructure-logging-level', + 'log', + ]); + + const configPath = path.resolve(__dirname, './webpack.cache.config.js'); + expect(stderr).toContain(`Using config ${configPath}`); + expect(stdout).toBeFalsy(); + } + }); }); diff --git a/test/watch/basic/basic.test.js b/test/watch/basic/basic.test.js index 29013daeff3..84c7d2c8900 100644 --- a/test/watch/basic/basic.test.js +++ b/test/watch/basic/basic.test.js @@ -176,4 +176,20 @@ describe('basic', () => { expect(stderr).toContain("Run 'webpack --help' to see available commands and options"); expect(stdout).toBeFalsy(); }); + + it('should log supplied config with watch', async () => { + if (isWebpack5) { + const { stderr, stdout } = await run(__dirname, [ + 'watch', + '--config', + 'watch.config.js', + '--infrastructure-logging-level', + 'log', + ]); + + const configPath = resolve(__dirname, './watch.config.js'); + expect(stderr).toContain(`Using config ${configPath}`); + expect(stdout).toBeFalsy(); + } + }); }); From d25ca14c9fac18bd85bb703b3ffb403973e02884 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Wed, 10 Feb 2021 01:51:16 +0530 Subject: [PATCH 3/4] fix: tests --- test/build/basic/basic.test.js | 14 ++++++-------- test/build/basic/log.config.js | 6 ++++++ test/serve/basic/log.config.js | 6 ++++++ test/serve/basic/serve-basic.test.js | 17 +++++------------ test/watch/basic/basic.test.js | 18 +++++------------- test/watch/basic/log.config.js | 6 ++++++ 6 files changed, 34 insertions(+), 33 deletions(-) create mode 100644 test/build/basic/log.config.js create mode 100644 test/serve/basic/log.config.js create mode 100644 test/watch/basic/log.config.js diff --git a/test/build/basic/basic.test.js b/test/build/basic/basic.test.js index 56ed3503732..ce3f8f7beee 100644 --- a/test/build/basic/basic.test.js +++ b/test/build/basic/basic.test.js @@ -1,6 +1,6 @@ 'use strict'; const { resolve } = require('path'); -const { run, isWebpack5 } = require('../../utils/test-utils'); +const { run } = require('../../utils/test-utils'); describe('bundle command', () => { it('should work without command (default command)', async () => { @@ -142,12 +142,10 @@ describe('bundle command', () => { }); it('should log supplied config when logging level is log', () => { - if (isWebpack5) { - const { exitCode, stderr, stdout } = run(__dirname, ['--config', './entry.config.js', '--infrastructure-logging-level', 'log']); - expect(exitCode).toBe(0); - const configPath = resolve(__dirname, './entry.config.js'); - expect(stderr).toContain(`Using config ${configPath}`); - expect(stdout).toBeFalsy(); - } + const { exitCode, stderr, stdout } = run(__dirname, ['--config', './log.config.js']); + expect(exitCode).toBe(0); + const configPath = resolve(__dirname, './log.config.js'); + expect(stderr).toContain(`Using config ${configPath}`); + expect(stdout).toBeTruthy(); }); }); diff --git a/test/build/basic/log.config.js b/test/build/basic/log.config.js new file mode 100644 index 00000000000..70619a29563 --- /dev/null +++ b/test/build/basic/log.config.js @@ -0,0 +1,6 @@ +module.exports = { + mode: 'development', + infrastructureLogging: { + level: 'log', + }, +}; diff --git a/test/serve/basic/log.config.js b/test/serve/basic/log.config.js new file mode 100644 index 00000000000..70619a29563 --- /dev/null +++ b/test/serve/basic/log.config.js @@ -0,0 +1,6 @@ +module.exports = { + mode: 'development', + infrastructureLogging: { + level: 'log', + }, +}; diff --git a/test/serve/basic/serve-basic.test.js b/test/serve/basic/serve-basic.test.js index e3c6c876619..e7b0b14170e 100644 --- a/test/serve/basic/serve-basic.test.js +++ b/test/serve/basic/serve-basic.test.js @@ -331,17 +331,10 @@ describe('basic serve usage', () => { }); it('should log used supplied config with serve', async () => { - if (isWebpack5) { - const { stderr, stdout } = await runServe(__dirname, [ - '--config', - 'webpack.config.js', - '--infrastructure-logging-level', - 'log', - ]); - - const configPath = path.resolve(__dirname, './webpack.cache.config.js'); - expect(stderr).toContain(`Using config ${configPath}`); - expect(stdout).toBeFalsy(); - } + const { stderr, stdout } = await runServe(__dirname, ['--config', 'log.config.js']); + + const configPath = path.resolve(__dirname, './log.config.js'); + expect(stderr).toContain(`Using config ${configPath}`); + expect(stdout).toBeTruthy(); }); }); diff --git a/test/watch/basic/basic.test.js b/test/watch/basic/basic.test.js index 84c7d2c8900..0b47cfc05eb 100644 --- a/test/watch/basic/basic.test.js +++ b/test/watch/basic/basic.test.js @@ -178,18 +178,10 @@ describe('basic', () => { }); it('should log supplied config with watch', async () => { - if (isWebpack5) { - const { stderr, stdout } = await run(__dirname, [ - 'watch', - '--config', - 'watch.config.js', - '--infrastructure-logging-level', - 'log', - ]); - - const configPath = resolve(__dirname, './watch.config.js'); - expect(stderr).toContain(`Using config ${configPath}`); - expect(stdout).toBeFalsy(); - } + const { stderr, stdout } = await run(__dirname, ['watch', '--config', 'log.config.js']); + + const configPath = resolve(__dirname, './log.config.js'); + expect(stderr).toContain(`Using config ${configPath}`); + expect(stdout).toBeTruthy(); }); }); diff --git a/test/watch/basic/log.config.js b/test/watch/basic/log.config.js new file mode 100644 index 00000000000..70619a29563 --- /dev/null +++ b/test/watch/basic/log.config.js @@ -0,0 +1,6 @@ +module.exports = { + mode: 'development', + infrastructureLogging: { + level: 'log', + }, +}; From b75081e58e46a62f6baebdf83552cc46f570f005 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Wed, 10 Feb 2021 02:09:02 +0530 Subject: [PATCH 4/4] fix: tests --- test/core-flags/infrastructure-logging.test.js | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/test/core-flags/infrastructure-logging.test.js b/test/core-flags/infrastructure-logging.test.js index 59f5df6c0c9..9e408f2d46b 100644 --- a/test/core-flags/infrastructure-logging.test.js +++ b/test/core-flags/infrastructure-logging.test.js @@ -1,7 +1,6 @@ 'use strict'; const { run } = require('../utils/test-utils'); -const { resolve } = require('path'); describe('infrastructure logging related flag', () => { it('should set infrastructureLogging.debug properly', () => { @@ -28,15 +27,4 @@ describe('infrastructure logging related flag', () => { expect(stderr).toContain("Compilation 'compiler' finished"); expect(stdout).toContain(`level: 'log'`); }); - - it('should log used default config when level is log', () => { - const { exitCode, stderr, stdout } = run(__dirname, ['--infrastructure-logging-level', 'log']); - - expect(exitCode).toBe(0); - const configPath = resolve(__dirname, './webpack.config.js'); - expect(stderr).toContain("Compilation 'compiler' starting..."); - expect(stderr).toContain(`Using config ${configPath}`); - expect(stderr).toContain("Compilation 'compiler' finished"); - expect(stdout).toContain(`level: 'log'`); - }); });