Skip to content

Commit

Permalink
feat: added the stats option
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Jan 15, 2021
1 parent eb646ff commit 376cdba
Show file tree
Hide file tree
Showing 11 changed files with 1,137 additions and 10 deletions.
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,18 @@ Please see the documentation for [`mime-types`](https://github.com/jshttp/mime-t
### publicPath

Type: `String`
Default: `output.publicPath`
Default: `output.publicPath` (from a configuration)

The public path that the middleware is bound to.
_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)._

_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)._

### stats

Type: `Boolean|String|Object`
Default: `stats` (from a configuration)

Stats options object or preset name.

### serverSideRender

Expand Down
24 changes: 24 additions & 0 deletions src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@
}
]
},
"stats": {
"description": "Stats options object or preset name.",
"anyOf": [
{
"enum": [
"none",
"summary",
"errors-only",
"errors-warnings",
"minimal",
"normal",
"detailed",
"verbose"
]
},
{
"type": "boolean"
},
{
"type": "object",
"additionalProperties": true
}
]
},
"serverSideRender": {
"description": "Instructs the module to enable or disable the server-side rendering mode.",
"type": "boolean"
Expand Down
33 changes: 28 additions & 5 deletions src/utils/setupHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ export default function setupHooks(context) {
// eslint-disable-next-line no-param-reassign
statsOptions = webpack.Stats.presetToOptions(statsOptions);
}

return statsOptions;
}

if (typeof statsOptions === 'undefined') {
// eslint-disable-next-line no-param-reassign
statsOptions = { preset: 'normal' };
} else if (typeof statsOptions === 'boolean') {
// eslint-disable-next-line no-param-reassign
statsOptions = statsOptions ? { preset: 'normal' } : { preset: 'none' };
} else if (typeof statsOptions === 'string') {
// eslint-disable-next-line no-param-reassign
statsOptions = { preset: statsOptions };
}

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

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

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

logger.log('Compilation finished');

let statsOptions = compiler.compilers
? { children: compiler.compilers.map((child) => child.options.stats) }
: compiler.options.stats;
const isMultiCompilerMode = Boolean(compiler.compilers);

let statsOptions;

if (typeof options.stats !== 'undefined') {
statsOptions = isMultiCompilerMode
? { children: compiler.compilers.map(() => options.stats) }
: options.stats;
} else {
statsOptions = isMultiCompilerMode
? { children: compiler.compilers.map((child) => child.options.stats) }
: compiler.options.stats;
}

if (compiler.compilers) {
if (isMultiCompilerMode) {
statsOptions.children = statsOptions.children.map(
(childStatsOptions) => {
// eslint-disable-next-line no-param-reassign
Expand Down
209 changes: 209 additions & 0 deletions test/__snapshots__/logging.test.js.snap.webpack4
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,23 @@ Entrypoint main = bundle.js
[./svg.svg] x bytes {main} [built]"
`;

exports[`logging should logging on successfully build and respect the "NO_COLOR" env: stderr 1`] = `""`;

exports[`logging should logging on successfully build and respect the "NO_COLOR" env: stdout 1`] = `
"Hash: xxxx
Version: webpack x.x.x
Time: Xms
Built at: x
Asset Size Chunks Chunk Names
bundle.js x KiB main [emitted] main
index.html x bytes [emitted]
svg.svg x KiB [emitted]
Entrypoint main = bundle.js
[./foo.js] x bytes {main} [built]
[./index.html] x bytes {main} [built]
[./svg.svg] x bytes {main} [built]"
`;

exports[`logging should logging on successfully build and respect the "stats" option from configuration with custom object value: stderr 1`] = `""`;

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

exports[`logging should logging on successfully build using the "stats" option for middleware with object value and no colors: stderr 1`] = `""`;

exports[`logging should logging on successfully build using the "stats" option for middleware with object value and no colors: stdout 1`] = `
"Asset Size Chunks Chunk Names
bundle.js x KiB main [emitted] main
index.html x bytes [emitted]
svg.svg x KiB [emitted]"
`;

exports[`logging should logging on successfully build using the "stats" option for middleware with object value: stderr 1`] = `""`;

exports[`logging should logging on successfully build using the "stats" option for middleware with object value: stdout 1`] = `
"Asset Size Chunks Chunk Names
bundle.js x KiB main [emitted] main
index.html x bytes [emitted]
svg.svg x KiB [emitted]"
`;

exports[`logging should logging on successfully build using the "stats" option for middleware with the "false" value: stderr 1`] = `""`;

exports[`logging should logging on successfully build using the "stats" option for middleware with the "false" value: stdout 1`] = `""`;

exports[`logging should logging on successfully build using the "stats" option for middleware with the "none" value: stderr 1`] = `""`;

exports[`logging should logging on successfully build using the "stats" option for middleware with the "none" value: stdout 1`] = `""`;

exports[`logging should logging on successfully build using the "stats" option for middleware with the "normal" value: stderr 1`] = `""`;

exports[`logging should logging on successfully build using the "stats" option for middleware with the "normal" value: stdout 1`] = `
"Hash: xxxx
Version: webpack x.x.x
Time: Xms
Built at: x
Asset Size Chunks Chunk Names
bundle.js x KiB main [emitted] main
index.html x bytes [emitted]
svg.svg x KiB [emitted]
Entrypoint main = bundle.js
[./foo.js] x bytes {main} [built]
[./index.html] x bytes {main} [built]
[./svg.svg] x bytes {main} [built]"
`;

exports[`logging should logging on successfully build using the "stats" option for middleware with the "true" value: stderr 1`] = `""`;

exports[`logging should logging on successfully build using the "stats" option for middleware with the "true" value: stdout 1`] = `
"Hash: xxxx
Version: webpack x.x.x
Time: Xms
Built at: x
Asset Size Chunks Chunk Names
bundle.js x KiB main [emitted] main
index.html x bytes [emitted]
svg.svg x KiB [emitted]
Entrypoint main = bundle.js
[./foo.js] x bytes {main} [built]
[./index.html] x bytes {main} [built]
[./svg.svg] x bytes {main} [built]"
`;

exports[`logging should logging on successfully build using the "stats" option for middleware with the "verbose" value: stderr 1`] = `""`;

exports[`logging should logging on successfully build using the "stats" option for middleware with the "verbose" value: stdout 1`] = `
"Hash: xxxx
Version: webpack x.x.x
Time: Xms
Built at: x
Asset Size Chunks Chunk Names
bundle.js x KiB main [emitted] main
index.html x bytes [emitted]
svg.svg x KiB [emitted]
Entrypoint main = bundle.js
chunk {main} bundle.js (xxxx) x bytes [entry] [rendered]
> ./foo.js main
[./foo.js] x bytes {main} [depth 0] [built]
single entry ./foo.js main
[./index.html] x bytes {main} [depth 1] [built]
[exports: default]
cjs require ./index.html [./foo.js] 4:0-23
[./svg.svg] x bytes {main} [depth 1] [built]
[exports: default]
cjs require ./svg.svg [./foo.js] 3:0-20

LOG from xxx"
`;

exports[`logging should logging on successfully build using the "stats" option for middleware with the object value and colors: stderr 1`] = `""`;

exports[`logging should logging on successfully build using the "stats" option for middleware with the object value and colors: stdout 1`] = `
"Asset Size Chunks Chunk Names
bundle.js x KiB main [emitted] main
index.html x bytes [emitted]
svg.svg x KiB [emitted]"
`;

exports[`logging should logging on successfully build when the 'stats' doesn't exist: stderr 1`] = `""`;

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

exports[`logging should logging on successfully multi compiler build using the "stats" option for middleware with object value and colors: stderr 1`] = `""`;

exports[`logging should logging on successfully multi compiler build using the "stats" option for middleware with object value and colors: stdout 1`] = `
"Child
Asset Size Chunks Chunk Names
bundle.js x KiB main [emitted] main
index.html x bytes [emitted]
svg.svg x KiB [emitted]
Child
Asset Size Chunks Chunk Names
bundle.js x KiB main [emitted] main"
`;

exports[`logging should logging on successfully multi compiler build using the "stats" option for middleware with object value and no colors: stderr 1`] = `""`;

exports[`logging should logging on successfully multi compiler build using the "stats" option for middleware with object value and no colors: stdout 1`] = `
"Child
Asset Size Chunks Chunk Names
bundle.js x KiB main [emitted] main
index.html x bytes [emitted]
svg.svg x KiB [emitted]
Child
Asset Size Chunks Chunk Names
bundle.js x KiB main [emitted] main"
`;

exports[`logging should logging on successfully multi compiler build using the "stats" option for middleware with the "false" value: stderr 1`] = `""`;

exports[`logging should logging on successfully multi compiler build using the "stats" option for middleware with the "false" value: stdout 1`] = `""`;

exports[`logging should logging on successfully multi compiler build using the "stats" option for middleware with the "normal" value: stderr 1`] = `""`;

exports[`logging should logging on successfully multi compiler build using the "stats" option for middleware with the "normal" value: stdout 1`] = `
"Hash: xxxx
Version: webpack x.x.x
Child
Hash: xxxx
Time: Xms
Built at: x
Asset Size Chunks Chunk Names
bundle.js x KiB main [emitted] main
index.html x bytes [emitted]
svg.svg x KiB [emitted]
Entrypoint main = bundle.js
[./foo.js] x bytes {main} [built]
[./index.html] x bytes {main} [built]
[./svg.svg] x bytes {main} [built]
Child
Hash: xxxx
Time: Xms
Built at: x
Asset Size Chunks Chunk Names
bundle.js x KiB main [emitted] main
Entrypoint main = bundle.js
[./bar.js] x bytes {main} [built]"
`;

exports[`logging should logging on successfully multi compiler build using the "stats" option for middleware with the "true" value: stderr 1`] = `""`;

exports[`logging should logging on successfully multi compiler build using the "stats" option for middleware with the "true" value: stdout 1`] = `
"Hash: xxxx
Version: webpack x.x.x
Child
Hash: xxxx
Time: Xms
Built at: x
Asset Size Chunks Chunk Names
bundle.js x KiB main [emitted] main
index.html x bytes [emitted]
svg.svg x KiB [emitted]
Entrypoint main = bundle.js
[./foo.js] x bytes {main} [built]
[./index.html] x bytes {main} [built]
[./svg.svg] x bytes {main} [built]
Child
Hash: xxxx
Time: Xms
Built at: x
Asset Size Chunks Chunk Names
bundle.js x KiB main [emitted] main
Entrypoint main = bundle.js
[./bar.js] x bytes {main} [built]"
`;

exports[`logging should logging on successfully multi compiler build using the "stats" option for middleware with the object value: stderr 1`] = `""`;

exports[`logging should logging on successfully multi compiler build using the "stats" option for middleware with the object value: stdout 1`] = `
"Child
Asset Size Chunks Chunk Names
bundle.js x KiB main [emitted] main
index.html x bytes [emitted]
svg.svg x KiB [emitted]
Child
Asset Size Chunks Chunk Names
bundle.js x KiB main [emitted] main"
`;

exports[`logging should logging on unsuccessful build in multi-compiler: stderr 1`] = `""`;

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

0 comments on commit 376cdba

Please # to comment.