Skip to content
This repository was archived by the owner on Sep 11, 2018. It is now read-only.

Commit 76f6869

Browse files
authored
feat: add JSON reporter. fixes #16 (#39)
* feat: add JSON reporter. fixes #16 * fix: rogress typo * fix: JsonReporter type * chore: pretty print json by request * fix: default to stylish for bad reporter names
1 parent 5afbcee commit 76f6869

File tree

10 files changed

+383
-5
lines changed

10 files changed

+383
-5
lines changed

lib/compiler.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const webpack = require('webpack');
66
const weblog = require('webpack-log');
77

88
const progress = require('./progress');
9+
const StylishReporter = require('./reporters/StylishReporter');
910

1011
function makeCallback(options) {
1112
const { compiler, config, reporter, resolve, reject } = options;
@@ -87,7 +88,13 @@ module.exports = (config) => {
8788
const configs = [].concat(config);
8889
const [first] = configs;
8990
const { reporter: reporterName, watchOptions } = first;
90-
const ReporterClass = requireReporter(reporterName || 'stylish');
91+
let ReporterClass = requireReporter(reporterName || 'stylish');
92+
93+
if (!ReporterClass) {
94+
log.error(`The reporter specified (${reporterName}) could not be located`);
95+
ReporterClass = StylishReporter;
96+
}
97+
9198
const reporter = new ReporterClass({ compiler, config });
9299

93100
if (first.progress) {

lib/reporters/JsonReporter.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const weblog = require('webpack-log');
2+
3+
const WebpackCommandError = require('../WebpackCommandError');
4+
5+
const Reporter = require('./Reporter');
6+
7+
module.exports = class JsonReporter extends Reporter {
8+
constructor(...args) {
9+
super(...args);
10+
11+
const log = weblog({ name: 'webpack', id: 'webpack-command' });
12+
this.originalLevel = log.level;
13+
log.level = 'silent';
14+
this.log = log;
15+
}
16+
17+
/* istanbul ignore next */
18+
progress() {
19+
throw new WebpackCommandError(
20+
'Build progress display is not supported when using the JSON reporter'
21+
);
22+
}
23+
24+
render(error, stats) {
25+
const json = stats.toJson();
26+
const result = JSON.stringify(json, null, 2);
27+
28+
process.stdout.write(result);
29+
30+
this.log.level = this.originalLevel;
31+
32+
return result;
33+
}
34+
};

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"release:ci": "conventional-github-releaser -p angular",
2828
"release:validate": "commitlint --from=$(git describe --tags --abbrev=0) --to=$(git rev-parse HEAD)",
2929
"security": "nsp check",
30-
"test": "npm run prepublishOnly && mocha test/test.js --check-leaks --exit",
30+
"test": "npm run prepublishOnly && mocha test/test.js --exit",
3131
"test:coverage": "mkdir -p coverage && nyc --silent npm test && npm run test:coverage:report",
3232
"test:coverage:report": "nyc report --reporter=lcov --reporter=text-lcov --reporter=json --reporter=clover > coverage/lcov.info",
3333
"ci:lint": "npm run lint && npm run security",
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const { resolve } = require('path');
2+
3+
const config = require('../../common/webpack.config');
4+
5+
module.exports = {
6+
arguments: [],
7+
8+
config: [
9+
Object.assign({}, config, {
10+
entry: resolve(__dirname, '../../common/entry-a.js'),
11+
reporter: 'json',
12+
}),
13+
Object.assign({}, config, {
14+
entry: [
15+
resolve(__dirname, '../../common/entry-b.js'),
16+
resolve(__dirname, '../../common/entry-c.js'),
17+
],
18+
reporter: 'json',
19+
}),
20+
],
21+
22+
inspect: 'stdout',
23+
24+
group: 'general',
25+
};

test/fixtures/reporters/json/json.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const { resolve } = require('path');
2+
3+
const config = require('../../common/webpack.config');
4+
5+
module.exports = {
6+
arguments: [],
7+
8+
config: Object.assign({}, config, {
9+
entry: resolve(__dirname, '../../common/entry-a.js'),
10+
reporter: 'json',
11+
}),
12+
13+
inspect: 'stdout',
14+
15+
group: 'general',
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// warning
2+
console.log(require); // eslint-disable-line
3+
4+
// error
5+
if (!window) { require('test'); } // eslint-disable-line
6+
7+
require('./image.jpg');
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const { resolve } = require('path');
2+
3+
const config = require('../../../common/webpack.config');
4+
5+
module.exports = {
6+
arguments: [],
7+
8+
config: Object.assign({}, config, {
9+
entry: resolve(__dirname, './entry-problems.js'),
10+
reporter: 'json',
11+
}),
12+
13+
inspect: 'stdout',
14+
15+
group: 'general',
16+
};

test/tests/__snapshots__/reporters.js.snap

+214
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,63 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`JsonReporter > json: reporter should apply #0 1`] = `
4+
Object {
5+
"context": "<PROJECT_ROOT>",
6+
"entry": "<PROJECT_ROOT>/test/fixtures/common/entry-a.js",
7+
"mode": "development",
8+
"plugins": Array [
9+
NamedModulesPlugin {
10+
"options": Object {},
11+
},
12+
],
13+
"reporter": "json",
14+
}
15+
`;
16+
17+
exports[`JsonReporter > json-multi: reporter should apply #0 1`] = `
18+
Array [
19+
Object {
20+
"context": "<PROJECT_ROOT>",
21+
"entry": "<PROJECT_ROOT>/test/fixtures/common/entry-a.js",
22+
"mode": "development",
23+
"plugins": Array [
24+
NamedModulesPlugin {
25+
"options": Object {},
26+
},
27+
],
28+
"reporter": "json",
29+
},
30+
Object {
31+
"context": "<PROJECT_ROOT>",
32+
"entry": Array [
33+
"<PROJECT_ROOT>/test/fixtures/common/entry-b.js",
34+
"<PROJECT_ROOT>/test/fixtures/common/entry-c.js",
35+
],
36+
"mode": "development",
37+
"plugins": Array [
38+
NamedModulesPlugin {
39+
"options": Object {},
40+
},
41+
],
42+
"reporter": "json",
43+
},
44+
]
45+
`;
46+
47+
exports[`JsonReporter > problems/problems: reporter should apply #0 1`] = `
48+
Object {
49+
"context": "<PROJECT_ROOT>",
50+
"entry": "<PROJECT_ROOT>/test/fixtures/reporters/json/problems/entry-problems.js",
51+
"mode": "development",
52+
"plugins": Array [
53+
NamedModulesPlugin {
54+
"options": Object {},
55+
},
56+
],
57+
"reporter": "json",
58+
}
59+
`;
60+
361
exports[`StylishReporter > problems/stylish-problems reporter should apply #0 1`] = `
462
Object {
563
"entry": "<PROJECT_ROOT>/test/fixtures/reporters/stylish/problems/entry-problems.js",
@@ -28,6 +86,50 @@ webpack v4.6.0
2886
<duration>
2987
3088
89+
./test/fixtures/reporters/stylish/problems/entry-problems.js
90+
5:15 error Module not found: Error: Can't resolve 'test' in
91+
'<PROJECT_ROOT>/test/fixtures/reporters/stylish/problems'
92+
2:12 warning Critical dependency: require function is used in a way in which
93+
dependencies cannot be statically extracted
94+
95+
./test/fixtures/reporters/stylish/problems/image.jpg
96+
1:0 error Module parse failed: Unexpected character '�' You may need an
97+
appropriate loader to handle this file type. (Source code omitted for
98+
this binary file) @
99+
./test/fixtures/reporters/stylish/problems/entry-problems.js 7:0-22
100+
101+
✖ 3 problems (2 errors, 1 warning)"
102+
`;
103+
104+
exports[`StylishReporter > problems/stylish-problems: reporter should apply #0 1`] = `
105+
Object {
106+
"context": "<PROJECT_ROOT>",
107+
"entry": "<PROJECT_ROOT>/test/fixtures/reporters/stylish/problems/entry-problems.js",
108+
"mode": "development",
109+
"plugins": Array [
110+
NamedModulesPlugin {
111+
"options": Object {},
112+
},
113+
],
114+
}
115+
`;
116+
117+
exports[`StylishReporter > problems/stylish-problems: reporter should build #0 1`] = `
118+
"
119+
webpack v4.6.0
120+
121+
<hash>
122+
size name module status
123+
160 B problems sync recursive ./test/fixtures/reporters/stylish/problems sync built
124+
148 B entry-problems.js ./test/fixtures/reporters/stylish/problems/entry-problems.js built ⚠ ✖
125+
177 B image.jpg ./test/fixtures/reporters/stylish/problems/image.jpg built failed ✖
126+
127+
size name asset status
128+
<size> main main.js emitted
129+
130+
<duration>
131+
132+
31133
./test/fixtures/reporters/stylish/problems/entry-problems.js
32134
5:15 error Module not found: Error: Can't resolve 'test' in
33135
'<PROJECT_ROOT>/test/fixtures/reporters/stylish/problems'
@@ -70,6 +172,34 @@ webpack v4.6.0
70172
"
71173
`;
72174
175+
exports[`StylishReporter > stylish: reporter should apply #0 1`] = `
176+
Object {
177+
"context": "<PROJECT_ROOT>",
178+
"entry": "<PROJECT_ROOT>/test/fixtures/common/entry-a.js",
179+
"mode": "development",
180+
"plugins": Array [
181+
NamedModulesPlugin {
182+
"options": Object {},
183+
},
184+
],
185+
}
186+
`;
187+
188+
exports[`StylishReporter > stylish: reporter should build #0 1`] = `
189+
"
190+
webpack v4.6.0
191+
192+
<hash>
193+
size name module status
194+
41 B entry-a.js ./test/fixtures/common/entry-a.js built
195+
196+
size name asset status
197+
<size> main main.js emitted
198+
199+
<duration>
200+
"
201+
`;
202+
73203
exports[`StylishReporter > stylish-multi reporter should apply #0 1`] = `
74204
Array [
75205
Object {
@@ -123,6 +253,90 @@ webpack v4.6.0
123253
total <duration>"
124254
`;
125255
256+
exports[`StylishReporter > stylish-multi: reporter should apply #0 1`] = `
257+
Array [
258+
Object {
259+
"context": "<PROJECT_ROOT>",
260+
"entry": "<PROJECT_ROOT>/test/fixtures/common/entry-a.js",
261+
"mode": "development",
262+
"plugins": Array [
263+
NamedModulesPlugin {
264+
"options": Object {},
265+
},
266+
],
267+
},
268+
Object {
269+
"context": "<PROJECT_ROOT>",
270+
"entry": Array [
271+
"<PROJECT_ROOT>/test/fixtures/common/entry-b.js",
272+
"<PROJECT_ROOT>/test/fixtures/common/entry-c.js",
273+
],
274+
"mode": "development",
275+
"plugins": Array [
276+
NamedModulesPlugin {
277+
"options": Object {},
278+
},
279+
],
280+
},
281+
]
282+
`;
283+
284+
exports[`StylishReporter > stylish-multi: reporter should build #0 1`] = `
285+
"
286+
webpack v4.6.0
287+
288+
<hash>
289+
size name module status
290+
41 B entry-a.js ./test/fixtures/common/entry-a.js built
291+
292+
size name asset status
293+
<size> main main.js emitted
294+
295+
<duration>
296+
297+
<hash>
298+
size name module status
299+
41 B entry-b.js ./test/fixtures/common/entry-b.js built
300+
41 B entry-c.js ./test/fixtures/common/entry-c.js built
301+
40 B 0 multi ./test/fixtures/common/entry-b.js ./test/fixtures/common/entry-c.js built
302+
303+
size name asset status
304+
<size> main main.js emitted
305+
306+
<duration>
307+
308+
total <duration>"
309+
`;
310+
311+
exports[`bad reporter name > should apply #0 1`] = `
312+
Object {
313+
"context": "<PROJECT_ROOT>",
314+
"entry": "<PROJECT_ROOT>/test/fixtures/common/entry-a.js",
315+
"mode": "development",
316+
"plugins": Array [
317+
NamedModulesPlugin {
318+
"options": Object {},
319+
},
320+
],
321+
"reporter": "batman",
322+
}
323+
`;
324+
325+
exports[`bad reporter name > should default to stylish #0 1`] = `
326+
"
327+
webpack v4.6.0
328+
329+
<hash>
330+
size name module status
331+
41 B entry-a.js ./test/fixtures/common/entry-a.js built
332+
333+
size name asset status
334+
<size> main main.js emitted
335+
336+
<duration>
337+
"
338+
`;
339+
126340
exports[`reporter parse util > should parse hidden #0 1`] = `"(1 asset, 2 modules hidden)"`;
127341
128342
exports[`reporter parse util > should parse status: cacheable #0 1`] = `

0 commit comments

Comments
 (0)