build.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. 'use strict'
  2. require('./check-versions')()
  3. const ora = require('ora')
  4. const rm = require('rimraf')
  5. const path = require('path')
  6. const chalk = require('chalk')
  7. const webpack = require('webpack')
  8. const config = require('../config')
  9. const webpackConfig = require('./webpack.prod.conf')
  10. var connect = require('connect')
  11. var serveStatic = require('serve-static')
  12. const spinner = ora(
  13. 'building for ' + process.env.env_config + ' environment...'
  14. )
  15. spinner.start()
  16. rm(path.join(config.build.assetsRoot, config.build.assetsSubDirectory), err => {
  17. if (err) throw err
  18. webpack(webpackConfig, (err, stats) => {
  19. spinner.stop()
  20. if (err)
  21. {
  22. console.log(chalk.red('webpack error \n'))
  23. console.log(chalk.red(err.toLocaleString()+' \n'))
  24. throw err
  25. }
  26. process.stdout.write(
  27. stats.toString({
  28. colors: true,
  29. modules: false,
  30. children: false,
  31. chunks: false,
  32. chunkModules: false
  33. }) + '\n\n'
  34. )
  35. if (stats.hasErrors()) {
  36. console.log(chalk.red(' Build failed with errors.\n'))
  37. process.exit(1)
  38. }
  39. console.log(chalk.cyan(' Build complete.\n'))
  40. console.log(
  41. chalk.yellow(
  42. ' Tip: built files are meant to be served over an HTTP server.\n' +
  43. " Opening index.html over file:// won't work.\n"
  44. )
  45. )
  46. if (process.env.npm_config_preview) {
  47. const port = 9526
  48. const host = 'http://localhost:' + port
  49. const basePath = config.build.assetsPublicPath
  50. const app = connect()
  51. app.use(
  52. basePath,
  53. serveStatic('./dist', {
  54. index: ['index.html', '/']
  55. })
  56. )
  57. app.listen(port, function() {
  58. console.log(
  59. chalk.green(`> Listening at http://localhost:${port}${basePath}`)
  60. )
  61. })
  62. }
  63. })
  64. })