From f8406e1c5253849fad741eb45f1ece23a7c603f4 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Thu, 11 Feb 2021 22:04:15 +0530 Subject: [PATCH] feat: display used config path when logging level=log (#2431) --- .github/workflows/nodejs.yml | 30 +++++++------------ package.json | 1 - .../__tests__/applyCLIPlugin.test.js | 4 +-- packages/webpack-cli/lib/plugins/CLIPlugin.js | 26 ++++++++++++---- packages/webpack-cli/lib/webpack-cli.js | 2 +- test/build/basic/basic.test.js | 16 ++++++++++ test/build/basic/log.config.js | 6 ++++ .../core-flags/infrastructure-logging.test.js | 4 +-- test/json/json.test.js | 15 ++++++---- test/serve/basic/log.config.js | 6 ++++ test/serve/basic/serve-basic.test.js | 10 +++++++ test/watch/basic/basic.test.js | 22 ++++++++++++++ test/watch/basic/log.config.js | 6 ++++ 13 files changed, 113 insertions(+), 35 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/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 351cdbadfa1..837f99b1ac1 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -35,12 +35,7 @@ jobs: node-version: ${{ matrix.node-version }} - name: Install dependencies - run: | - yarn - yarn bootstrap - - - name: Install webpack ${{ matrix.webpack-version }} - run: yarn add -W webpack@${{ matrix.webpack-version }} + run: yarn - name: Build run: yarn build @@ -57,7 +52,7 @@ jobs: matrix: os: [ubuntu-latest, windows-latest, macos-latest] node-version: [10.x, 12.x, 14.x] - webpack-version: [webpack-4, latest] + webpack-version: [4, latest] steps: - uses: actions/checkout@v2 @@ -76,27 +71,24 @@ jobs: path: | node_modules */*/node_modules - key: ${{ runner.os }}-${{ matrix.webpack-version }}-${{ hashFiles('**/yarn.lock', './yarn.lock') }} + key: ${{ runner.os }}-${{ matrix.webpack-version }}-yarn-${{ hashFiles('**/yarn.lock', './yarn.lock') }} + restore-keys: | + ${{ runner.os }}-${{ matrix.webpack-version }}-yarn- - name: Install dependencies - if: steps.cache.outputs.cache-hit != 'true' - run: | - yarn - yarn bootstrap + run: yarn - name: Install webpack ${{ matrix.webpack-version }} - if: matrix.webpack-version == 'webpack-4' + if: matrix.webpack-version == '4' run: yarn add -W webpack@${{ matrix.webpack-version }} - - name: Build and Bootstrap - run: | - yarn build:ci - yarn run lerna bootstrap + - name: Prepare environment for tests + run: yarn build:ci - - name: Run Smoketests + - name: Run smoketests run: yarn test:smoketests - - name: Test and Generate Coverage + - name: Run tests and generate coverage run: | yarn prepsuite yarn test:coverage diff --git a/package.json b/package.json index 64039a18326..368f125a321 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,6 @@ "./packages/*" ], "scripts": { - "bootstrap": "lerna bootstrap", "clean": "del-cli \"*.tsbuildinfo\" \"packages/**/*.tsbuildinfo\" \"packages/!(webpack-cli)/lib/!(*.tpl)\" \"**/.yo-rc.json\"", "prebuild": "yarn clean", "prebuild:ci": "yarn clean && node ./scripts/setupBuild.js", 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..363221e3fd6 100644 --- a/packages/webpack-cli/lib/plugins/CLIPlugin.js +++ b/packages/webpack-cli/lib/plugins/CLIPlugin.js @@ -39,10 +39,18 @@ class CLIPlugin { setupHelpfulOutput(compiler) { const pluginName = 'webpack-cli'; - const getCompilationName = () => (compiler.name ? ` '${compiler.name}'` : ''); + const getCompilationName = () => (compiler.name ? `'${compiler.name}'` : ''); + + const { configPath } = this.options; compiler.hooks.run.tap(pluginName, () => { - this.logger.log(`Compilation${getCompilationName()} starting...`); + const name = getCompilationName(); + + this.logger.log(`Compiler${name ? ` ${name}` : ''} starting...`); + + if (configPath) { + this.logger.log(`Compiler${name ? ` ${name}` : ''} is using config: '${configPath}'`); + } }); compiler.hooks.watchRun.tap(pluginName, (compiler) => { @@ -52,7 +60,13 @@ class CLIPlugin { this.logger.warn('You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.'); } - this.logger.log(`Compilation${getCompilationName()} starting...`); + const name = getCompilationName(); + + this.logger.log(`Compiler${name ? ` ${name}` : ''} starting...`); + + if (configPath) { + this.logger.log(`Compiler${name ? ` ${name}` : ''} is using config: '${configPath}'`); + } }); compiler.hooks.invalid.tap(pluginName, (filename, changeTime) => { @@ -63,11 +77,13 @@ class CLIPlugin { }); (compiler.webpack ? compiler.hooks.afterDone : compiler.hooks.done).tap(pluginName, () => { - this.logger.log(`Compilation${getCompilationName()} finished`); + const name = getCompilationName(); + + this.logger.log(`Compiler${name ? ` ${name}` : ''} finished`); process.nextTick(() => { if (compiler.watchMode) { - this.logger.log(`Compiler${getCompilationName()} is watching files for updates...`); + this.logger.log(`Compiler${name ? `${name}` : ''} is watching files for updates...`); } }); }); 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/build/basic/basic.test.js b/test/build/basic/basic.test.js index 01481187f45..1b559e77599 100644 --- a/test/build/basic/basic.test.js +++ b/test/build/basic/basic.test.js @@ -1,5 +1,7 @@ 'use strict'; +const stripAnsi = require('strip-ansi'); +const { resolve } = require('path'); const { run } = require('../../utils/test-utils'); describe('bundle command', () => { @@ -140,4 +142,18 @@ 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', () => { + const { exitCode, stderr, stdout } = run(__dirname, ['--config', './log.config.js']); + const configPath = resolve(__dirname, './log.config.js'); + + expect(exitCode).toBe(0); + + const pureStderr = stripAnsi(stderr); + + expect(pureStderr).toContain('Compiler starting...'); + expect(pureStderr).toContain(`Compiler is using config: '${configPath}'`); + expect(pureStderr).toContain('Compiler finished'); + 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/core-flags/infrastructure-logging.test.js b/test/core-flags/infrastructure-logging.test.js index 9e408f2d46b..8c2f8645169 100644 --- a/test/core-flags/infrastructure-logging.test.js +++ b/test/core-flags/infrastructure-logging.test.js @@ -23,8 +23,8 @@ describe('infrastructure logging related flag', () => { const { exitCode, stderr, stdout } = run(__dirname, ['--infrastructure-logging-level', 'log']); expect(exitCode).toBe(0); - expect(stderr).toContain("Compilation 'compiler' starting..."); - expect(stderr).toContain("Compilation 'compiler' finished"); + expect(stderr).toContain("Compiler 'compiler' starting..."); + expect(stderr).toContain("Compiler 'compiler' finished"); expect(stdout).toContain(`level: 'log'`); }); }); diff --git a/test/json/json.test.js b/test/json/json.test.js index 64441e4504a..49022409705 100644 --- a/test/json/json.test.js +++ b/test/json/json.test.js @@ -1,4 +1,6 @@ 'use strict'; + +const stripAnsi = require('strip-ansi'); const { run } = require('../utils/test-utils'); const { existsSync, readFile } = require('fs'); const { resolve } = require('path'); @@ -111,8 +113,8 @@ describe('json', () => { const { exitCode, stderr, stdout } = run(__dirname, ['--json', '--config', 'logging.config.js']); expect(exitCode).toBe(0); - expect(stderr).toContain('Compilation starting'); - expect(stderr).toContain('Compilation finished'); + expect(stderr).toContain('Compiler starting...'); + expect(stderr).toContain('Compiler finished'); expect(() => JSON.parse(stdout)).not.toThrow(); expect(JSON.parse(stdout)['hash']).toBeDefined(); }); @@ -121,9 +123,12 @@ describe('json', () => { const { exitCode, stderr, stdout } = run(__dirname, ['--json', 'stats.json', '--config', 'logging.config.js']); expect(exitCode).toBe(0); - expect(stderr).toContain('Compilation starting'); - expect(stderr).toContain('Compilation finished'); - expect(stderr).toContain(successMessage); + + const pureStderr = stripAnsi(stderr); + + expect(stderr).toContain('Compiler starting...'); + expect(pureStderr).toContain('Compiler finished'); + expect(pureStderr).toContain(successMessage); expect(stdout).toBeFalsy(); expect(existsSync(resolve(__dirname, './stats.json'))).toBeTruthy(); 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 d215cea596e..e5b1335bb60 100644 --- a/test/serve/basic/serve-basic.test.js +++ b/test/serve/basic/serve-basic.test.js @@ -306,6 +306,16 @@ describe('basic serve usage', () => { expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull(); }); + it('should log used supplied config with serve', async () => { + const { stderr, stdout } = await runServe(__dirname, ['--config', 'log.config.js', '--port', port]); + const configPath = path.resolve(__dirname, './log.config.js'); + + expect(stderr).toContain('Compiler starting...'); + expect(stderr).toContain(`Compiler is using config: '${configPath}'`); + expect(stderr).toContain('Compiler finished'); + expect(stdout).toBeTruthy(); + }); + it("should log error on using '--watch' flag with serve", async () => { const { stdout, stderr } = await runServe(testPath, ['--watch']); diff --git a/test/watch/basic/basic.test.js b/test/watch/basic/basic.test.js index 29013daeff3..bced22c1988 100644 --- a/test/watch/basic/basic.test.js +++ b/test/watch/basic/basic.test.js @@ -159,6 +159,28 @@ describe('basic', () => { }); }); + it('should log supplied config with watch', (done) => { + const proc = runAndGetWatchProc(__dirname, ['watch', '--config', 'log.config.js']); + const configPath = resolve(__dirname, './log.config.js'); + + let stderr = ''; + + proc.stderr.on('data', (chunk) => { + const data = stripAnsi(chunk.toString()); + + stderr += stripAnsi(data); + + if (/Compiler finished/.test(data)) { + expect(stderr).toContain('Compiler starting...'); + expect(stderr).toContain(`Compiler is using config: '${configPath}'`); + expect(stderr).toContain('Compiler finished'); + + proc.kill(); + done(); + } + }); + }); + it('should recompile upon file change using the `command` option and the `--watch` option and log warning', async () => { const { exitCode, stderr, stdout } = await run(__dirname, ['watch', '--watch', '--mode', 'development']); 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', + }, +};