-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathDevLogPlugin.js
54 lines (45 loc) · 1.44 KB
/
DevLogPlugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'use strict'
/**
* Module dependencies.
*/
const { chalk, logger, performance } = require('@vuepress/shared-utils')
/**
* Expose DevLogPlugin class.
*/
module.exports = class DevLogPlugin {
constructor (options) {
this.options = options
}
apply (compiler) {
let isFirst = true
const { displayHost, port, publicPath, clearScreen: shouldClearScreen, isHttps } = this.options
compiler.hooks.done.tap('vuepress-log', stats => {
if (shouldClearScreen) {
clearScreen()
}
const time = new Date().toTimeString().match(/^[\d:]+/)[0]
const displayUrl = `http${isHttps ? 's' : ''}://${displayHost}:${port}${publicPath}`
logger.success(
`${chalk.gray(`[${time}]`)} Build ${chalk.italic(stats.hash.slice(0, 6))} `
+ `finished in ${stats.endTime - stats.startTime} ms! `
+ (
isFirst
? ''
: `${chalk.gray(`( ${displayUrl} )`)}`
)
)
if (isFirst) {
isFirst = false
console.log(`${chalk.gray('>')} VuePress dev server listening at ${chalk.cyan(displayUrl)}`)
const { duration } = performance.stop()
logger.developer(`It took a total of ${chalk.cyan(`${duration}ms`)} to run the ${chalk.cyan('vuepress dev')} for the first time.`)
}
})
if (shouldClearScreen) {
compiler.hooks.invalid.tap('vuepress-log', clearScreen)
}
}
}
function clearScreen () {
process.stdout.write('\x1Bc')
}