Skip to content

Commit

Permalink
feat: respect the infrastructureLogging.level option (potential bre…
Browse files Browse the repository at this point in the history
…aking change, logger uses `stderr`) (#2144)
  • Loading branch information
alexander-akait committed Dec 1, 2020
1 parent b4f3f8b commit 7daccc7
Show file tree
Hide file tree
Showing 134 changed files with 1,338 additions and 928 deletions.
12 changes: 8 additions & 4 deletions packages/webpack-cli/__tests__/serve/serve.test.js
Expand Up @@ -12,16 +12,20 @@ describe('Serve', () => {
}

it('should run with cli', async () => {
const { stdout, stderr } = await runServe([], __dirname);
const { stderr, stdout } = await runServe([], __dirname);

expect(stderr).toContain('Compilation starting...');
expect(stderr).toContain('Compilation finished');
expect(stdout).toContain('main.js');
expect(stdout).not.toContain('HotModuleReplacementPlugin');
expect(stderr).toHaveLength(0);
});

it('should work with flags', async () => {
const { stdout, stderr } = await runServe(['--hot'], __dirname);
const { stderr, stdout } = await runServe(['--hot'], __dirname);

expect(stderr).toContain('Compilation starting...');
expect(stderr).toContain('Compilation finished');
expect(stdout).toContain('main.js');
expect(stdout).toContain('HotModuleReplacementPlugin');
expect(stderr).toHaveLength(0);
});
});
21 changes: 9 additions & 12 deletions packages/webpack-cli/lib/plugins/CLIPlugin.js
@@ -1,6 +1,5 @@
const packageExists = require('../utils/package-exists');
const webpack = packageExists('webpack') ? require('webpack') : undefined;
const logger = require('../utils/logger');

class CLIPlugin {
constructor(options) {
Expand Down Expand Up @@ -37,11 +36,6 @@ class CLIPlugin {
const progressPlugin = Boolean(compiler.options.plugins.find((plugin) => plugin instanceof ProgressPlugin));

if (!progressPlugin) {
if (typeof this.options.progress === 'string' && this.options.progress !== 'profile') {
logger.error(`'${this.options.progress}' is an invalid value for the --progress option. Only 'profile' is allowed.`);
process.exit(2);
}

new ProgressPlugin({ profile: this.options.progress === 'profile' }).apply(compiler);
}
}
Expand All @@ -51,37 +45,40 @@ class CLIPlugin {
const getCompilationName = (compilation) => (compilation.name ? ` '${compilation.name}'` : '');

compiler.hooks.run.tap(pluginName, (compiler) => {
logger.success(`Compilation${getCompilationName(compiler)} starting...`);
this.logger.info(`Compilation${getCompilationName(compiler)} starting...`);
});

compiler.hooks.watchRun.tap(pluginName, (compiler) => {
const { bail, watch } = compiler.options;

if (bail && watch) {
logger.warn('You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.');
this.logger.warn('You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.');
}

logger.success(`Compilation${getCompilationName(compiler)} starting...`);
this.logger.info(`Compilation${getCompilationName(compiler)} starting...`);
});

compiler.hooks.invalid.tap(pluginName, (filename, changeTime) => {
const date = new Date(changeTime * 1000);

logger.log(`File '${filename}' was modified, changed time is ${date} (timestamp is ${changeTime})`);
this.logger.info(`File '${filename}' was modified`);
this.logger.log(`Changed time is ${date} (timestamp is ${changeTime})`);
});

(compiler.webpack ? compiler.hooks.afterDone : compiler.hooks.done).tap(pluginName, (stats) => {
logger.success(`Compilation${getCompilationName(stats.compilation)} finished`);
this.logger.info(`Compilation${getCompilationName(stats.compilation)} finished`);

process.nextTick(() => {
if (compiler.watchMode) {
logger.success(`Compiler${getCompilationName(stats.compilation)} is watching files for updates...`);
this.logger.info(`Compiler${getCompilationName(stats.compilation)} is watching files for updates...`);
}
});
});
}

apply(compiler) {
this.logger = compiler.getInfrastructureLogger('webpack-cli');

if (this.options.progress && this.options.helpfulOutput) {
this.setupProgressPlugin(compiler);
}
Expand Down
5 changes: 5 additions & 0 deletions packages/webpack-cli/lib/webpack-cli.js
Expand Up @@ -219,6 +219,11 @@ class WebpackCLI {
}
}

if (typeof args.progress === 'string' && args.progress !== 'profile') {
logger.error(`'${args.progress}' is an invalid value for the --progress option. Only 'profile' is allowed.`);
process.exit(2);
}

if (Object.keys(args).length === 0 && !process.env.NODE_ENV) {
return config;
}
Expand Down
3 changes: 2 additions & 1 deletion test/analyze/analyze-flag.test.js
Expand Up @@ -22,7 +22,8 @@ describe('--analyze flag', () => {
const { exitCode, stderr, stdout } = run(__dirname, ['-c', './analyze.config.js', '--analyze']);

expect(exitCode).toBe(0);
expect(stderr).toBeFalsy();
expect(stderr).toContain('Compilation starting...');
expect(stderr).toContain('Compilation finished');
expect(stdout).toContain('Webpack Bundle Analyzer saved report to');
expect(stdout.match(/Webpack Bundle Analyzer saved report to/g)).toHaveLength(1);
});
Expand Down
33 changes: 29 additions & 4 deletions test/bail/bail.test.js
Expand Up @@ -4,59 +4,84 @@ const { run, runWatch } = require('../utils/test-utils');

describe('bail and watch warning', () => {
it('should not log warning in not watch mode', async () => {
const { stderr, stdout, exitCode } = await run(__dirname, ['-c', 'bail-webpack.config.js']);
const { exitCode, stderr, stdout } = await run(__dirname, ['-c', 'bail-webpack.config.js']);

expect(exitCode).toEqual(0);
expect(stderr).toContain('Compilation starting...');
expect(stderr).toContain('Compilation finished');
expect(stderr).not.toContain(`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`);
expect(stdout).toBeTruthy();
});

it('should not log warning in not watch mode without the "bail" option', async () => {
const { stderr, stdout, exitCode } = await run(__dirname, ['-c', 'no-bail-webpack.config.js']);
const { exitCode, stderr, stdout } = await run(__dirname, ['-c', 'no-bail-webpack.config.js']);

expect(exitCode).toEqual(0);
expect(stderr).toContain('Compilation starting...');
expect(stderr).toContain('Compilation finished');
expect(stderr).not.toContain(`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`);
expect(stdout).toBeTruthy();
});

it('should not log warning in not watch mode without the "watch" option', async () => {
const { stderr, stdout } = await runWatch(__dirname, ['-c', 'watch-webpack.config.js']);

expect(stderr).toContain('Compilation starting...');
expect(stderr).toContain('Compilation finished');
expect(stderr).not.toContain(`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`);
expect(stdout).toBeTruthy();
});

it('should not log warning without the "bail" option', async () => {
const { stderr, stdout } = await runWatch(__dirname, ['-c', 'no-bail-webpack.config.js', '--watch']);

expect(stderr).toContain('Compilation starting...');
expect(stderr).toContain('Compilation finished');
expect(stderr).not.toContain(`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`);
expect(stdout).toBeTruthy();
});

it('should not log warning without the "bail" option', async () => {
const { stderr, stdout } = await runWatch(__dirname, ['-c', 'no-bail-webpack.config.js', '--watch']);

expect(stderr).toContain('Compilation starting...');
expect(stderr).toContain('Compilation finished');
expect(stderr).not.toContain(`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`);
expect(stdout).toBeTruthy();
});

it('should log warning in watch mode', async () => {
const { stderr, stdout } = await runWatch(__dirname, ['-c', 'bail-webpack.config.js', '--watch']);

expect(stderr).toContain('Compilation starting...');
expect(stderr).toContain('Compilation finished');
expect(stderr).toContain(`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`);
expect(stdout).toBeTruthy();
});

it('should log warning in watch mode', async () => {
const { stderr, stdout } = await runWatch(__dirname, ['-c', 'bail-and-watch-webpack.config.js']);

expect(stderr).toContain('Compilation starting...');
expect(stderr).toContain('Compilation finished');
expect(stderr).toContain(`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`);
expect(stdout).toBeTruthy();
});

it('should log warning in case of multiple compilers', async () => {
const { stderr, stdout } = await runWatch(__dirname, ['-c', 'multi-webpack.config.js']);

const { stderr, stdout } = await runWatch(
__dirname,
['-c', 'multi-webpack.config.js'],
true,
"Compiler 'second' is watching files for updates...",
);

expect(stderr).toContain("Compilation 'first' starting...");
expect(stderr).toContain("Compilation 'first' finished");
expect(stderr).toContain("Compiler 'first' is watching files for updates...");
expect(stderr).toContain("Compilation 'second' starting...");
expect(stderr).toContain("Compilation 'second' finished");
expect(stderr).toContain("Compiler 'second' is watching files for updates...");
expect(stderr).toContain(`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`);
expect(stdout).toBeTruthy();
});
Expand Down
18 changes: 12 additions & 6 deletions test/build-errors/errors.test.js
Expand Up @@ -5,18 +5,22 @@ const { resolve } = require('path');

describe('errors', () => {
it('should output by default', () => {
const { stdout, exitCode } = run(__dirname);
const { exitCode, stderr, stdout } = run(__dirname);

expect(exitCode).toBe(1);
expect(stderr).toContain('Compilation starting...');
expect(stderr).toContain('Compilation finished');
expect(stdout).toMatch(/ERROR/);
expect(stdout).toMatch(/Error: Can't resolve/);
expect(exitCode).toBe(1);
});

it('should output JSON with the "json" flag', () => {
const { stdout, exitCode } = run(__dirname, ['--json']);
const { exitCode, stderr, stdout } = run(__dirname, ['--json']);

expect(() => JSON.parse(stdout)).not.toThrow();
expect(exitCode).toBe(1);
expect(stderr).not.toContain('Compilation starting...');
expect(stderr).not.toContain('Compilation finished');
expect(() => JSON.parse(stdout)).not.toThrow();

const json = JSON.parse(stdout);

Expand All @@ -27,10 +31,12 @@ describe('errors', () => {
});

it('should store json to a file', (done) => {
const { stdout, exitCode } = run(__dirname, ['--json', 'stats.json']);
const { exitCode, stderr, stdout } = run(__dirname, ['--json', 'stats.json']);

expect(stdout).toContain('stats are successfully stored as json to stats.json');
expect(exitCode).toBe(1);
expect(stderr).not.toContain('Compilation starting...');
expect(stderr).not.toContain('Compilation finished');
expect(stdout).toContain('stats are successfully stored as json to stats.json');

stat(resolve(__dirname, './stats.json'), (err, stats) => {
expect(err).toBe(null);
Expand Down
19 changes: 13 additions & 6 deletions test/build-warnings/warnings.test.js
Expand Up @@ -5,18 +5,23 @@ const { resolve } = require('path');

describe('warnings', () => {
it('should output by default', () => {
const { stdout, exitCode } = run(__dirname);
const { exitCode, stderr, stdout } = run(__dirname);

expect(exitCode).toBe(0);
expect(stderr).toContain('Compilation starting...');
expect(stderr).toContain('Compilation finished');
expect(stdout).toMatch(/WARNING/);
expect(stdout).toMatch(/Error: Can't resolve/);
expect(exitCode).toBe(0);
});

it('should output JSON with the "json" flag', () => {
const { stdout, exitCode } = run(__dirname, ['--json']);
const { exitCode, stderr, stdout } = run(__dirname, ['--json']);

expect(() => JSON.parse(stdout)).not.toThrow();
expect(exitCode).toBe(0);
expect(stderr).not.toContain('Compilation starting...');
expect(stderr).not.toContain('Compilation finished');

expect(() => JSON.parse(stdout)).not.toThrow();

const json = JSON.parse(stdout);

Expand All @@ -27,10 +32,12 @@ describe('warnings', () => {
});

it('should store json to a file', (done) => {
const { stdout, exitCode } = run(__dirname, ['--json', 'stats.json']);
const { exitCode, stderr, stdout } = run(__dirname, ['--json', 'stats.json']);

expect(stdout).toContain('stats are successfully stored as json to stats.json');
expect(exitCode).toBe(0);
expect(stderr).not.toContain('Compilation starting...');
expect(stderr).not.toContain('Compilation finished');
expect(stdout).toContain('stats are successfully stored as json to stats.json');

stat(resolve(__dirname, './stats.json'), (err, stats) => {
expect(err).toBe(null);
Expand Down
9 changes: 9 additions & 0 deletions test/cache/cache.test.js
Expand Up @@ -17,6 +17,8 @@ describe('cache', () => {
let { exitCode, stderr, stdout } = run(__dirname, ['-c', './webpack.config.js', '--cache-name', 'test'], false);

if (isWebpack5) {
expect(stderr).toContain('Compilation starting...');
expect(stderr).toContain('Compilation finished');
expect(stderr).toContain('No pack exists at');
expect(stderr).toContain('Stored pack');
expect(stdout).toBeTruthy();
Expand All @@ -28,6 +30,8 @@ describe('cache', () => {

if (isWebpack5) {
expect(exitCode).toEqual(0);
expect(stderr).toContain('Compilation starting...');
expect(stderr).toContain('Compilation finished');
expect(stderr).toContain('restore cache container');
expect(stderr).toContain('restore cache content metadata');
expect(stderr).toContain('restore cache content');
Expand All @@ -41,6 +45,9 @@ describe('cache', () => {
let { exitCode, stderr, stdout } = run(__dirname, ['--cache-name', 'test-1'], false);

if (isWebpack5) {
expect(exitCode).toEqual(0);
expect(stderr).toContain('Compilation starting...');
expect(stderr).toContain('Compilation finished');
expect(stderr).toContain('No pack exists at');
expect(stderr).toContain('Stored pack');
expect(stdout).toBeTruthy();
Expand All @@ -52,6 +59,8 @@ describe('cache', () => {

if (isWebpack5) {
expect(exitCode).toEqual(0);
expect(stderr).toContain('Compilation starting...');
expect(stderr).toContain('Compilation finished');
expect(stderr).toContain('restore cache container');
expect(stderr).toContain('restore cache content metadata');
expect(stderr).toContain('restore cache content');
Expand Down

0 comments on commit 7daccc7

Please sign in to comment.