Skip to content

Commit 376cdba

Browse files
feat: added the stats option
1 parent eb646ff commit 376cdba

11 files changed

+1137
-10
lines changed

README.md

+10-3
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,18 @@ Please see the documentation for [`mime-types`](https://github.com/jshttp/mime-t
9696
### publicPath
9797

9898
Type: `String`
99-
Default: `output.publicPath`
99+
Default: `output.publicPath` (from a configuration)
100100

101101
The public path that the middleware is bound to.
102-
_Best Practice: use the same `publicPath` defined in your webpack config.
103-
For more information about `publicPath`, please see [the webpack documentation](https://webpack.js.org/guides/public-path)._
102+
103+
_Best Practice: use the same `publicPath` defined in your webpack config. For more information about `publicPath`, please see [the webpack documentation](https://webpack.js.org/guides/public-path)._
104+
105+
### stats
106+
107+
Type: `Boolean|String|Object`
108+
Default: `stats` (from a configuration)
109+
110+
Stats options object or preset name.
104111

105112
### serverSideRender
106113

src/options.json

+24
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,30 @@
4141
}
4242
]
4343
},
44+
"stats": {
45+
"description": "Stats options object or preset name.",
46+
"anyOf": [
47+
{
48+
"enum": [
49+
"none",
50+
"summary",
51+
"errors-only",
52+
"errors-warnings",
53+
"minimal",
54+
"normal",
55+
"detailed",
56+
"verbose"
57+
]
58+
},
59+
{
60+
"type": "boolean"
61+
},
62+
{
63+
"type": "object",
64+
"additionalProperties": true
65+
}
66+
]
67+
},
4468
"serverSideRender": {
4569
"description": "Instructs the module to enable or disable the server-side rendering mode.",
4670
"type": "boolean"

src/utils/setupHooks.js

+28-5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ export default function setupHooks(context) {
2828
// eslint-disable-next-line no-param-reassign
2929
statsOptions = webpack.Stats.presetToOptions(statsOptions);
3030
}
31+
32+
return statsOptions;
33+
}
34+
35+
if (typeof statsOptions === 'undefined') {
36+
// eslint-disable-next-line no-param-reassign
37+
statsOptions = { preset: 'normal' };
38+
} else if (typeof statsOptions === 'boolean') {
39+
// eslint-disable-next-line no-param-reassign
40+
statsOptions = statsOptions ? { preset: 'normal' } : { preset: 'none' };
41+
} else if (typeof statsOptions === 'string') {
42+
// eslint-disable-next-line no-param-reassign
43+
statsOptions = { preset: statsOptions };
3144
}
3245

3346
return statsOptions;
@@ -42,7 +55,7 @@ export default function setupHooks(context) {
4255

4356
// Do the stuff in nextTick, because bundle may be invalidated if a change happened while compiling
4457
process.nextTick(() => {
45-
const { compiler, logger, state, callbacks } = context;
58+
const { compiler, logger, options, state, callbacks } = context;
4659

4760
// Check if still in valid state
4861
if (!state) {
@@ -51,11 +64,21 @@ export default function setupHooks(context) {
5164

5265
logger.log('Compilation finished');
5366

54-
let statsOptions = compiler.compilers
55-
? { children: compiler.compilers.map((child) => child.options.stats) }
56-
: compiler.options.stats;
67+
const isMultiCompilerMode = Boolean(compiler.compilers);
68+
69+
let statsOptions;
70+
71+
if (typeof options.stats !== 'undefined') {
72+
statsOptions = isMultiCompilerMode
73+
? { children: compiler.compilers.map(() => options.stats) }
74+
: options.stats;
75+
} else {
76+
statsOptions = isMultiCompilerMode
77+
? { children: compiler.compilers.map((child) => child.options.stats) }
78+
: compiler.options.stats;
79+
}
5780

58-
if (compiler.compilers) {
81+
if (isMultiCompilerMode) {
5982
statsOptions.children = statsOptions.children.map(
6083
(childStatsOptions) => {
6184
// eslint-disable-next-line no-param-reassign

test/__snapshots__/logging.test.js.snap.webpack4

+209
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,23 @@ Entrypoint main = bundle.js
208208
[./svg.svg] x bytes {main} [built]"
209209
`;
210210

211+
exports[`logging should logging on successfully build and respect the "NO_COLOR" env: stderr 1`] = `""`;
212+
213+
exports[`logging should logging on successfully build and respect the "NO_COLOR" env: stdout 1`] = `
214+
"Hash: xxxx
215+
Version: webpack x.x.x
216+
Time: Xms
217+
Built at: x
218+
Asset Size Chunks Chunk Names
219+
bundle.js x KiB main [emitted] main
220+
index.html x bytes [emitted]
221+
svg.svg x KiB [emitted]
222+
Entrypoint main = bundle.js
223+
[./foo.js] x bytes {main} [built]
224+
[./index.html] x bytes {main} [built]
225+
[./svg.svg] x bytes {main} [built]"
226+
`;
227+
211228
exports[`logging should logging on successfully build and respect the "stats" option from configuration with custom object value: stderr 1`] = `""`;
212229

213230
exports[`logging should logging on successfully build and respect the "stats" option from configuration with custom object value: stdout 1`] = `
@@ -299,6 +316,101 @@ Entrypoint main = bundle.js
299316
[./bar.js] x bytes {main} [built]"
300317
`;
301318

319+
exports[`logging should logging on successfully build using the "stats" option for middleware with object value and no colors: stderr 1`] = `""`;
320+
321+
exports[`logging should logging on successfully build using the "stats" option for middleware with object value and no colors: stdout 1`] = `
322+
"Asset Size Chunks Chunk Names
323+
bundle.js x KiB main [emitted] main
324+
index.html x bytes [emitted]
325+
svg.svg x KiB [emitted]"
326+
`;
327+
328+
exports[`logging should logging on successfully build using the "stats" option for middleware with object value: stderr 1`] = `""`;
329+
330+
exports[`logging should logging on successfully build using the "stats" option for middleware with object value: stdout 1`] = `
331+
"Asset Size Chunks Chunk Names
332+
bundle.js x KiB main [emitted] main
333+
index.html x bytes [emitted]
334+
svg.svg x KiB [emitted]"
335+
`;
336+
337+
exports[`logging should logging on successfully build using the "stats" option for middleware with the "false" value: stderr 1`] = `""`;
338+
339+
exports[`logging should logging on successfully build using the "stats" option for middleware with the "false" value: stdout 1`] = `""`;
340+
341+
exports[`logging should logging on successfully build using the "stats" option for middleware with the "none" value: stderr 1`] = `""`;
342+
343+
exports[`logging should logging on successfully build using the "stats" option for middleware with the "none" value: stdout 1`] = `""`;
344+
345+
exports[`logging should logging on successfully build using the "stats" option for middleware with the "normal" value: stderr 1`] = `""`;
346+
347+
exports[`logging should logging on successfully build using the "stats" option for middleware with the "normal" value: stdout 1`] = `
348+
"Hash: xxxx
349+
Version: webpack x.x.x
350+
Time: Xms
351+
Built at: x
352+
Asset Size Chunks Chunk Names
353+
bundle.js x KiB main [emitted] main
354+
index.html x bytes [emitted]
355+
svg.svg x KiB [emitted]
356+
Entrypoint main = bundle.js
357+
[./foo.js] x bytes {main} [built]
358+
[./index.html] x bytes {main} [built]
359+
[./svg.svg] x bytes {main} [built]"
360+
`;
361+
362+
exports[`logging should logging on successfully build using the "stats" option for middleware with the "true" value: stderr 1`] = `""`;
363+
364+
exports[`logging should logging on successfully build using the "stats" option for middleware with the "true" value: stdout 1`] = `
365+
"Hash: xxxx
366+
Version: webpack x.x.x
367+
Time: Xms
368+
Built at: x
369+
Asset Size Chunks Chunk Names
370+
bundle.js x KiB main [emitted] main
371+
index.html x bytes [emitted]
372+
svg.svg x KiB [emitted]
373+
Entrypoint main = bundle.js
374+
[./foo.js] x bytes {main} [built]
375+
[./index.html] x bytes {main} [built]
376+
[./svg.svg] x bytes {main} [built]"
377+
`;
378+
379+
exports[`logging should logging on successfully build using the "stats" option for middleware with the "verbose" value: stderr 1`] = `""`;
380+
381+
exports[`logging should logging on successfully build using the "stats" option for middleware with the "verbose" value: stdout 1`] = `
382+
"Hash: xxxx
383+
Version: webpack x.x.x
384+
Time: Xms
385+
Built at: x
386+
Asset Size Chunks Chunk Names
387+
bundle.js x KiB main [emitted] main
388+
index.html x bytes [emitted]
389+
svg.svg x KiB [emitted]
390+
Entrypoint main = bundle.js
391+
chunk {main} bundle.js (xxxx) x bytes [entry] [rendered]
392+
> ./foo.js main
393+
[./foo.js] x bytes {main} [depth 0] [built]
394+
single entry ./foo.js main
395+
[./index.html] x bytes {main} [depth 1] [built]
396+
[exports: default]
397+
cjs require ./index.html [./foo.js] 4:0-23
398+
[./svg.svg] x bytes {main} [depth 1] [built]
399+
[exports: default]
400+
cjs require ./svg.svg [./foo.js] 3:0-20
401+
402+
LOG from xxx"
403+
`;
404+
405+
exports[`logging should logging on successfully build using the "stats" option for middleware with the object value and colors: stderr 1`] = `""`;
406+
407+
exports[`logging should logging on successfully build using the "stats" option for middleware with the object value and colors: stdout 1`] = `
408+
"Asset Size Chunks Chunk Names
409+
bundle.js x KiB main [emitted] main
410+
index.html x bytes [emitted]
411+
svg.svg x KiB [emitted]"
412+
`;
413+
302414
exports[`logging should logging on successfully build when the 'stats' doesn't exist: stderr 1`] = `""`;
303415

304416
exports[`logging should logging on successfully build when the 'stats' doesn't exist: stdout 1`] = `
@@ -333,6 +445,103 @@ Entrypoint main = bundle.js
333445
[./svg.svg] x bytes {main} [built]"
334446
`;
335447

448+
exports[`logging should logging on successfully multi compiler build using the "stats" option for middleware with object value and colors: stderr 1`] = `""`;
449+
450+
exports[`logging should logging on successfully multi compiler build using the "stats" option for middleware with object value and colors: stdout 1`] = `
451+
"Child
452+
Asset Size Chunks Chunk Names
453+
bundle.js x KiB main [emitted] main
454+
index.html x bytes [emitted]
455+
svg.svg x KiB [emitted]
456+
Child
457+
Asset Size Chunks Chunk Names
458+
bundle.js x KiB main [emitted] main"
459+
`;
460+
461+
exports[`logging should logging on successfully multi compiler build using the "stats" option for middleware with object value and no colors: stderr 1`] = `""`;
462+
463+
exports[`logging should logging on successfully multi compiler build using the "stats" option for middleware with object value and no colors: stdout 1`] = `
464+
"Child
465+
Asset Size Chunks Chunk Names
466+
bundle.js x KiB main [emitted] main
467+
index.html x bytes [emitted]
468+
svg.svg x KiB [emitted]
469+
Child
470+
Asset Size Chunks Chunk Names
471+
bundle.js x KiB main [emitted] main"
472+
`;
473+
474+
exports[`logging should logging on successfully multi compiler build using the "stats" option for middleware with the "false" value: stderr 1`] = `""`;
475+
476+
exports[`logging should logging on successfully multi compiler build using the "stats" option for middleware with the "false" value: stdout 1`] = `""`;
477+
478+
exports[`logging should logging on successfully multi compiler build using the "stats" option for middleware with the "normal" value: stderr 1`] = `""`;
479+
480+
exports[`logging should logging on successfully multi compiler build using the "stats" option for middleware with the "normal" value: stdout 1`] = `
481+
"Hash: xxxx
482+
Version: webpack x.x.x
483+
Child
484+
Hash: xxxx
485+
Time: Xms
486+
Built at: x
487+
Asset Size Chunks Chunk Names
488+
bundle.js x KiB main [emitted] main
489+
index.html x bytes [emitted]
490+
svg.svg x KiB [emitted]
491+
Entrypoint main = bundle.js
492+
[./foo.js] x bytes {main} [built]
493+
[./index.html] x bytes {main} [built]
494+
[./svg.svg] x bytes {main} [built]
495+
Child
496+
Hash: xxxx
497+
Time: Xms
498+
Built at: x
499+
Asset Size Chunks Chunk Names
500+
bundle.js x KiB main [emitted] main
501+
Entrypoint main = bundle.js
502+
[./bar.js] x bytes {main} [built]"
503+
`;
504+
505+
exports[`logging should logging on successfully multi compiler build using the "stats" option for middleware with the "true" value: stderr 1`] = `""`;
506+
507+
exports[`logging should logging on successfully multi compiler build using the "stats" option for middleware with the "true" value: stdout 1`] = `
508+
"Hash: xxxx
509+
Version: webpack x.x.x
510+
Child
511+
Hash: xxxx
512+
Time: Xms
513+
Built at: x
514+
Asset Size Chunks Chunk Names
515+
bundle.js x KiB main [emitted] main
516+
index.html x bytes [emitted]
517+
svg.svg x KiB [emitted]
518+
Entrypoint main = bundle.js
519+
[./foo.js] x bytes {main} [built]
520+
[./index.html] x bytes {main} [built]
521+
[./svg.svg] x bytes {main} [built]
522+
Child
523+
Hash: xxxx
524+
Time: Xms
525+
Built at: x
526+
Asset Size Chunks Chunk Names
527+
bundle.js x KiB main [emitted] main
528+
Entrypoint main = bundle.js
529+
[./bar.js] x bytes {main} [built]"
530+
`;
531+
532+
exports[`logging should logging on successfully multi compiler build using the "stats" option for middleware with the object value: stderr 1`] = `""`;
533+
534+
exports[`logging should logging on successfully multi compiler build using the "stats" option for middleware with the object value: stdout 1`] = `
535+
"Child
536+
Asset Size Chunks Chunk Names
537+
bundle.js x KiB main [emitted] main
538+
index.html x bytes [emitted]
539+
svg.svg x KiB [emitted]
540+
Child
541+
Asset Size Chunks Chunk Names
542+
bundle.js x KiB main [emitted] main"
543+
`;
544+
336545
exports[`logging should logging on unsuccessful build in multi-compiler: stderr 1`] = `""`;
337546

338547
exports[`logging should logging on unsuccessful build in multi-compiler: stdout 1`] = `

0 commit comments

Comments
 (0)