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
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
13 changes: 12 additions & 1 deletion test/build/basic/basic.test.js
@@ -1,5 +1,5 @@
'use strict';

const { resolve } = require('path');
const { run } = require('../../utils/test-utils');

describe('bundle command', () => {
Expand Down Expand Up @@ -140,4 +140,15 @@ 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);
expect(stderr).toContain('Compiler starting...');
expect(stderr).toContain(`Compiler is using config: '${configPath}'`);
expect(stderr).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'`);
});
});
8 changes: 4 additions & 4 deletions test/json/json.test.js
Expand Up @@ -111,8 +111,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,8 +121,8 @@ 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('Compiler starting...');
expect(stderr).toContain('Compiler finished');
expect(stderr).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 = 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',
},
};