Repository URL to install this package:
|
Version:
1.2.9 ▾
|
import log from 'fliplog'
import { ProgressPlugin } from 'webpack'
import { Configuration, Options } from '../../typings'
const ora = log.requirePkg('ora')
let prevName
const spinner = ora('building').start()
function toSpinnerColor(rounded: number) {
switch (true) {
case rounded <= 50:
return 'grey'
case rounded <= 70:
return 'yellow'
case rounded >= 70:
return 'cyan'
case rounded >= 90:
default:
return 'grey'
}
}
let spinnerTimeoutId
export default function metaPlugin(config: Configuration, options: Options) {
const { isClient, isAnalyze } = options
const name = isClient ? '[web]' : '[server]'
const logName = prevName !== name ? name : undefined
prevName = name
console.log('\n\n\n')
/**
* @see https://stackoverflow.com/questions/31052991/webpack-progress-using-node-js-api
* @description logs the progress when building
* @todo spinner?
*/
const progressConfig = (percentage, msg) => {
// log name when it changes
const rounded = percentage * 100
const text = rounded + '%' + ' ' + msg + ' ' + name
spinner.color = toSpinnerColor(rounded)
spinner.text = text
if (logName) {
spinner.text = logName + ' ' + text
}
clearTimeout(spinnerTimeoutId)
const onTimeout = () => {
console.log('\n\n\n')
spinner.stopAndPersist({ text: 'built!' })
console.log('\n\n\n')
}
spinnerTimeoutId = setTimeout(
onTimeout,
process.env.NODE_ENV === 'production' ? 20000 : 1000
)
// if (percentage >= 100) {
// // setTimeout(() => {
// // console.log('\n\n\n')
// // spinner.stopAndPersist({ text: 'built!' })
// // console.log('\n\n\n')
// // }, 100)
// }
// console.log((percentage * 100) + '%', msg, name)
}
if (process.env.LOG_BUILD_PROGRESS) {
const progressPlugin = new ProgressPlugin(progressConfig)
config.plugins.push(progressPlugin)
}
// @todo?
// if (process.env.SHOULD_NOT_SAVE_FILE_IF_YOUR_CODE_ERRORS) {
// const plugin = new NoEmitOnErrorsPlugin()
// config.plugins.push(plugin)
// }
if (isAnalyze) {
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer')
const analyzePlugin = new BundleAnalyzerPlugin()
config.plugins.push(analyzePlugin)
}
}