Nuxt/lib/builder/webpack/plugins/progress.js

70 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-03-11 23:15:14 +00:00
const webpack = require('webpack')
2018-03-13 10:33:02 +00:00
const chalk = require('chalk')
const _ = require('lodash')
const sharedState = {}
const BLOCK_CHAR = '█'
2018-03-11 23:15:14 +00:00
2018-03-13 08:29:34 +00:00
module.exports = class ProgressPlugin extends webpack.ProgressPlugin {
constructor(options) {
super(options)
this.handler = (percent, msg) => this.updateProgress(percent, msg)
2018-03-13 10:33:02 +00:00
this.options = options
if (!sharedState[options.name]) {
sharedState[options.name] = {
color: options.color
}
}
2018-03-13 08:29:34 +00:00
2018-03-13 10:33:02 +00:00
this.spinner = options.spinner
}
2018-03-13 08:29:34 +00:00
2018-03-13 10:33:02 +00:00
get state() {
return sharedState[this.options.name]
}
2018-03-12 15:16:08 +00:00
2018-03-13 08:29:34 +00:00
updateProgress(percent, msg) {
2018-03-13 10:33:02 +00:00
const progress = Math.floor(percent * 100)
2018-03-12 15:16:08 +00:00
2018-03-13 10:33:02 +00:00
this.state.progress = progress
this.state.succeed =
this.state.msg = msg
2018-03-12 15:16:08 +00:00
2018-03-13 10:33:02 +00:00
// Update spinner using shared state
let isInProgress = false
const width = 25
let line = _.range(width).fill(chalk.white(BLOCK_CHAR))
let additional = []
2018-03-12 15:16:08 +00:00
2018-03-13 10:33:02 +00:00
Object.keys(sharedState).forEach(name => {
const state = sharedState[name]
2018-03-12 15:16:08 +00:00
2018-03-13 10:33:02 +00:00
if (state.progress >= 100) {
return
}
2018-03-12 15:16:08 +00:00
2018-03-13 10:33:02 +00:00
isInProgress = true
2018-03-13 08:29:34 +00:00
2018-03-13 10:33:02 +00:00
const w = state.progress * (width / 100)
const b = chalk.keyword(state.color)(BLOCK_CHAR)
2018-03-12 15:16:08 +00:00
2018-03-13 10:33:02 +00:00
for (let i = 0; i < w; i++) {
line[i] = b
}
additional.push(`${b} ${name} (${state.progress}%) `)
})
if (isInProgress) {
this.spinner.start()
this.spinner.text = 'Compiling ' + line.join('') + ' ' + additional.join(' ')
} else {
this.spinner.succeed('Compiled ' + this.options.name)
}
}
2018-03-12 15:16:08 +00:00
}