This repository has been archived by the owner on May 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move logging into plugin, use svgo for svg compression
- Loading branch information
1 parent
93d8559
commit 1ac3a30
Showing
4 changed files
with
104 additions
and
76 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
const readline = require('readline') | ||
const formatMessages = require('webpack-format-messages') | ||
const chalk = require('chalk') | ||
const clearScreen = require('brighten') | ||
|
||
function getCompileTime(stats) { | ||
if (isMultiStats(stats)) { | ||
// Webpack multi compilations run in parallel so using the longest duration. | ||
// https://webpack.github.io/docs/configuration.html#multiple-configurations | ||
return stats.stats.reduce( | ||
(time, stats) => Math.max(time, getCompileTime(stats)), | ||
0 | ||
) | ||
} | ||
return stats.endTime - stats.startTime | ||
} | ||
|
||
function isMultiStats(stats) { | ||
return stats.stats | ||
} | ||
|
||
function displayTook(stats) { | ||
const time = getCompileTime(stats) | ||
|
||
if (time > 1000) { | ||
return time / 1000 + 's' | ||
} | ||
|
||
return time + 'ms' | ||
} | ||
|
||
module.exports = class { | ||
apply(compiler) { | ||
clearScreen() | ||
|
||
let first = true | ||
|
||
compiler.plugin('invalid', () => { | ||
clearScreen() | ||
console.log(chalk.yellow('Compiling...')) | ||
}) | ||
|
||
compiler.plugin('done', stats => { | ||
if (first) { | ||
// Leave the first line showing the webserver address | ||
readline.cursorTo(process.stdout, 0, 1) | ||
readline.clearScreenDown(process.stdout) | ||
} else { | ||
// Whipe the screen including contents | ||
readline.cursorTo(process.stdout, 0, 0) | ||
readline.clearScreenDown(process.stdout) | ||
} | ||
|
||
const messages = formatMessages(stats) | ||
|
||
if (messages.errors.length === 0 && messages.warnings.length === 0) { | ||
// Don't clear `Starting server on HOST` | ||
console.log( | ||
`${chalk.green('Compiled successfully!')} ${chalk.dim( | ||
`in ${displayTook(stats)}` | ||
)}` | ||
) | ||
} | ||
|
||
if (messages.errors.length > 0) { | ||
console.log(chalk.inverse('Failed to compile.')) | ||
messages.errors.forEach(e => console.log(e)) | ||
return | ||
} | ||
|
||
if (messages.warnings.length > 0) { | ||
console.log('Compiled with warnings.') | ||
messages.warnings.forEach(w => console.log(w)) | ||
} | ||
|
||
first = false | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters