Files
vuepress/packages/@vuepress/core/lib/webpack/DevLogPlugin.js
T
2019-02-04 02:02:44 +08:00

50 lines
1.3 KiB
JavaScript

'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
compiler.hooks.done.tap('vuepress-log', stats => {
clearScreen()
const { displayHost, port, publicPath } = this.options
const time = new Date().toTimeString().match(/^[\d:]+/)[0]
const displayUrl = `http://${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.`)
}
})
compiler.hooks.invalid.tap('vuepress-log', clearScreen)
}
}
function clearScreen () {
process.stdout.write('\x1Bc')
}