build.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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) throw err
  21. process.stdout.write(
  22. stats.toString({
  23. colors: true,
  24. modules: false,
  25. children: false,
  26. chunks: false,
  27. chunkModules: false
  28. }) + '\n\n'
  29. )
  30. if (stats.hasErrors()) {
  31. console.log(chalk.red(' Build failed with errors.\n'))
  32. process.exit(1)
  33. }
  34. console.log(chalk.cyan(' Build complete.\n'))
  35. console.log(
  36. chalk.yellow(
  37. ' Tip: built files are meant to be served over an HTTP server.\n' +
  38. " Opening index.html over file:// won't work.\n"
  39. )
  40. )
  41. if (process.env.npm_config_preview) {
  42. const port = 9526
  43. const host = 'http://localhost:' + port
  44. const basePath = config.build.assetsPublicPath
  45. const app = connect()
  46. app.use(
  47. basePath,
  48. serveStatic('./dist', {
  49. index: ['index.html', '/']
  50. })
  51. )
  52. app.listen(port, function() {
  53. console.log(
  54. chalk.green(`> Listening at http://localhost:${port}${basePath}`)
  55. )
  56. })
  57. }
  58. })
  59. })