build.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. const path = require('path')
  2. const fs = require('fs-extra')
  3. const ora = require('ora')
  4. const chalk = require('chalk')
  5. const webpack = require('webpack')
  6. module.exports = (env, config) => {
  7. const spinner = ora(`Building for ${env}...`)
  8. spinner.start()
  9. fs.emptyDirSync(path.resolve(__dirname, '../dist'))
  10. const compiler = webpack(config)
  11. compiler.run((err, stats) => {
  12. spinner.stop()
  13. if (err) {
  14. console.error(err.stack || err)
  15. if (err.details) {
  16. console.error(err.details)
  17. }
  18. return
  19. }
  20. process.stdout.write(
  21. stats.toString({
  22. children: false,
  23. chunks: false,
  24. colors: true,
  25. modules: false,
  26. chunkModules: false,
  27. hash: false,
  28. }) +
  29. '\n\n',
  30. )
  31. if (stats.hasErrors()) {
  32. return console.log(chalk.white.bgRed('Build failed.\n'))
  33. }
  34. // if (stats.hasWarnings()) {
  35. // return console.log(chalk.white.bgGreenBright(
  36. // 'Build success, but with warnings.\n'))
  37. // }
  38. console.log(chalk.white.bgGreen('Build success.\n'))
  39. })
  40. }