Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: display used config path when logging level=log #2431

Merged
merged 12 commits into from Feb 11, 2021
30 changes: 11 additions & 19 deletions .github/workflows/nodejs.yml
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions packages/webpack-cli/__tests__/applyCLIPlugin.test.js
Expand Up @@ -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,
Expand All @@ -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([
Expand Down
26 changes: 21 additions & 5 deletions packages/webpack-cli/lib/plugins/CLIPlugin.js
Expand Up @@ -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) => {
Expand All @@ -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}'`);
}
anshumanv marked this conversation as resolved.
Show resolved Hide resolved
});

compiler.hooks.invalid.tap(pluginName, (filename, changeTime) => {
Expand All @@ -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...`);
}
});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack-cli/lib/webpack-cli.js
Expand Up @@ -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,
Expand Down
16 changes: 16 additions & 0 deletions 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', () => {
Expand Down Expand Up @@ -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();
});
});
6 changes: 6 additions & 0 deletions test/build/basic/log.config.js
@@ -0,0 +1,6 @@
module.exports = {
mode: 'development',
infrastructureLogging: {
level: 'log',
},
};
4 changes: 2 additions & 2 deletions test/core-flags/infrastructure-logging.test.js
Expand Up @@ -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'`);
});
});
15 changes: 10 additions & 5 deletions 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');
Expand Down Expand Up @@ -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();
});
Expand All @@ -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();

Expand Down
6 changes: 6 additions & 0 deletions test/serve/basic/log.config.js
@@ -0,0 +1,6 @@
module.exports = {
mode: 'development',
infrastructureLogging: {
level: 'log',
},
};
10 changes: 10 additions & 0 deletions test/serve/basic/serve-basic.test.js
Expand Up @@ -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']);

Expand Down
22 changes: 22 additions & 0 deletions test/watch/basic/basic.test.js
Expand Up @@ -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']);

Expand Down
6 changes: 6 additions & 0 deletions test/watch/basic/log.config.js
@@ -0,0 +1,6 @@
module.exports = {
mode: 'development',
infrastructureLogging: {
level: 'log',
},
};