Skip to content

Commit 0cbb270

Browse files
authored
feat(webpack-cli): webpack stats (#1299)
* feat(webpack-cli): stats Amended also the usage of output. `--standard` has been removed and `--pretty` will show the fancy output, which is experimental. * tests: added git ignore to the tests * tests: fixed missing tests * chore(ci): fix travis build step * tests: remove dodgy test case * tests: removed snapshot * docs: fixed grammar
1 parent a6930ac commit 0cbb270

File tree

30 files changed

+209
-167
lines changed

30 files changed

+209
-167
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ install:
2626
- lerna bootstrap
2727
- yarn global add codecov
2828
- yarn global add eslint
29+
- yarn prepsuite
2930
script:
3031
- yarn travis:lint
3132
- yarn test:ci

packages/webpack-cli/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Options
5050
-h, --hot Enables Hot Module Replacement
5151
-s, --sourcemap string Determine source maps to use
5252
--prefetch string Prefetch this request
53+
--pretty Prints a fancy output
5354
-j, --json Prints result as JSON
5455
--standard Prints standard output
5556
-d, --dev Run development build
@@ -58,6 +59,8 @@ Options
5859
--no-mode Sets mode="none" which disables any default behavior
5960
--version Get current version
6061
--node-args string[] NodeJS flags
62+
--stats type It instructs webpack on how to treat the stats
63+
--verbose It tells webpack to output all the information
6164
```
6265

6366
## Defaults
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const StatsGroup = require('../lib/groups/StatsGroup');
2+
3+
describe('StatsGroup', function() {
4+
{
5+
StatsGroup.validOptions().map(option => {
6+
it(`should handle ${option} option`, () => {
7+
const statsGroup = new StatsGroup([
8+
{
9+
stats: option,
10+
},
11+
]);
12+
13+
const result = statsGroup.run();
14+
expect(result.options.stats).toEqual(option);
15+
});
16+
});
17+
}
18+
});

packages/webpack-cli/lib/groups/StatsGroup.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
11
const GroupHelper = require('../utils/GroupHelper');
2-
2+
const { logger } = require('@webpack-cli/logger');
33
/**
44
* StatsGroup gathers information about the stats options
55
*/
66
class StatsGroup extends GroupHelper {
7+
static validOptions() {
8+
return ['minimal', 'none', 'normal', 'verbose', 'errors-warnings', 'errors-only'];
9+
}
10+
711
constructor(options) {
812
super(options);
913
}
1014

1115
resolveOptions() {
12-
Object.keys(this.args).forEach(arg => {
13-
if (['quiet', 'verbose', 'json', 'silent', 'standard'].includes(arg)) {
14-
this.opts.outputOptions[arg] = this.args[arg];
16+
if (this.args.verbose && this.args.stats) {
17+
logger.warn('Conflict between "verbose" and "stats" options. Using verbose.');
18+
this.opts.option.stats = {
19+
verbose: true,
20+
};
21+
} else {
22+
if (this.args.verbose) {
23+
this.opts.option.stats = {
24+
verbose: true,
25+
};
1526
} else {
16-
this.opts.options[arg] = this.args[arg];
27+
this.opts.options.stats = this.args.stats;
1728
}
18-
});
29+
}
30+
31+
if (this.args.pretty) {
32+
this.opts.outputOptions.pretty = true;
33+
}
1934
}
2035

2136
run() {

packages/webpack-cli/lib/utils/Compiler.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ class Compiler {
7878
}
7979

8080
generateOutput(outputOptions, stats, statsErrors, processingMessageBuffer) {
81-
if (outputOptions.standard) {
82-
this.output.generateRawOutput(stats);
83-
} else {
81+
if (outputOptions.pretty) {
8482
const statsObj = stats.toJson(outputOptions);
8583
if (statsObj.children && Array.isArray(statsObj.children) && this.compilerOptions && Array.isArray(this.compilerOptions)) {
8684
statsObj.children.forEach(child => {
@@ -89,6 +87,8 @@ class Compiler {
8987
return;
9088
}
9189
this.output.generateFancyOutput(statsObj, statsErrors, processingMessageBuffer);
90+
} else {
91+
this.output.generateRawOutput(stats, this.compilerOptions);
9292
}
9393
process.stdout.write('\n');
9494
if (outputOptions.watch) {

packages/webpack-cli/lib/utils/CompilerOutput.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ class CompilerOutput {
110110
return statsObj;
111111
}
112112

113-
generateRawOutput(stats) {
114-
process.stdout.write(stats.toString());
113+
generateRawOutput(stats, options) {
114+
process.stdout.write(stats.toString(options.stats));
115115
}
116116

117117
generateJsonOutput() {}

packages/webpack-cli/lib/utils/cli-flags.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
const { logger } = require('@webpack-cli/logger');
2+
const StatsGroup = require('../groups/StatsGroup');
3+
24
const HELP_GROUP = 'help';
35
const CONFIG_GROUP = 'config';
46
const BASIC_GROUP = 'basic';
@@ -243,25 +245,25 @@ module.exports = {
243245
},
244246
{
245247
name: 'prefetch',
246-
usage: '--prefetch <request>',
248+
usage: 'webpack --prefetch <request>',
247249
type: String,
248250
group: ADVANCED_GROUP,
249251
description: 'Prefetch this request',
250252
link: 'https://webpack.js.org/plugins/prefetch-plugin/',
251253
},
252254
{
253255
name: 'json',
254-
usage: '--json',
256+
usage: 'webpack --json',
255257
type: Boolean,
256258
alias: 'j',
257259
description: 'Prints result as JSON',
258260
group: DISPLAY_GROUP,
259261
},
260262
{
261-
name: 'standard',
262-
usage: '--standard',
263+
name: 'pretty',
264+
usage: 'webpack --pretty',
263265
type: Boolean,
264-
description: 'Prints standard output',
266+
description: 'Prints a fancy output',
265267
group: DISPLAY_GROUP,
266268
},
267269
{
@@ -322,6 +324,27 @@ module.exports = {
322324
group: BASIC_GROUP,
323325
description: 'NodeJS flags',
324326
},
327+
{
328+
name: 'stats',
329+
usage: 'webpack --stats verbose',
330+
type: value => {
331+
if (StatsGroup.validOptions().includes(value)) {
332+
return value;
333+
}
334+
logger.warn('No value recognised for "stats" option');
335+
return 'normal';
336+
},
337+
group: DISPLAY_GROUP,
338+
description: 'It instructs webpack on how to treat the stats',
339+
link: 'https://webpack.js.org/configuration/stats/#stats',
340+
},
341+
{
342+
name: 'verbose',
343+
usage: 'webpack --verbose',
344+
type: Boolean,
345+
group: DISPLAY_GROUP,
346+
description: 'It tells webpack to output all the information',
347+
},
325348
/* {
326349
name: "analyze",
327350
type: Boolean,

test/.eslintrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": ["eslint:recommended", "plugin:node/recommended", "plugin:prettier/recommended"],
3+
"env": {
4+
"node": true,
5+
"es6": true,
6+
"jest": true
7+
},
8+
"plugins": ["node", "prettier"],
9+
"parserOptions": { "ecmaVersion": 2020, "sourceType": "module" }
10+
}

test/config-lookup/dotfolder-array/dotfolder-array.test.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
'use strict';
22
const { stat } = require('fs');
3-
const { resolve, sep } = require('path');
4-
const { run, extractSummary } = require('../../utils/test-utils');
3+
const { resolve } = require('path');
4+
const { run } = require('../../utils/test-utils');
55

66
describe('dotfolder array config lookup', () => {
77
it('should find a webpack array configuration in a dotfolder', done => {
88
const { stdout, stderr } = run(__dirname, [], false);
99
expect(stderr).not.toBeUndefined();
1010
expect(stdout).not.toBeUndefined();
1111

12-
const summary = extractSummary(stdout);
13-
const outputDir = 'config-lookup/dotfolder-array/dist';
14-
const outDirectoryFromCompiler = summary['Output Directory'].split(sep);
15-
const outDirToMatch = outDirectoryFromCompiler
16-
.slice(outDirectoryFromCompiler.length - 3, outDirectoryFromCompiler.length)
17-
.join('/');
18-
expect(outDirToMatch).toContain(outputDir);
1912
stat(resolve(__dirname, './dist/dist-commonjs.js'), (err, stats) => {
2013
expect(err).toBe(null);
2114
expect(stats.isFile()).toBe(true);

test/config-lookup/dotfolder-single/dotfolder-single.test.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,15 @@
33
const { stat } = require('fs');
44
const { resolve } = require('path');
55

6-
const { run, extractSummary } = require('../../utils/test-utils');
6+
const { run } = require('../../utils/test-utils');
77

88
describe('dotfolder single config lookup', () => {
99
it('should find a webpack configuration in a dotfolder', done => {
1010
const { stdout, stderr } = run(__dirname, [], false);
1111
expect(stderr).not.toBeUndefined();
1212
expect(stdout).not.toBeUndefined();
1313

14-
const summary = extractSummary(stdout);
15-
const outputDir = 'config-lookup/dotfolder-single/dist';
16-
17-
expect(summary['Output Directory']).toContain(outputDir);
18-
expect(stderr).not.toContain('Module not found');
14+
expect(stdout).not.toContain('Module not found');
1915
stat(resolve(__dirname, './dist/main.js'), (err, stats) => {
2016
expect(err).toBe(null);
2117
expect(stats.isFile()).toBe(true);

0 commit comments

Comments
 (0)