build.js 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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('building for ' + process.env.env_config + ' environment...')
  13. spinner.start()
  14. rm(path.join(config.build.assetsRoot, config.build.assetsSubDirectory), err => {
  15. if (err) throw err
  16. webpack(webpackConfig, (err, stats) => {
  17. spinner.stop()
  18. if (err) throw err
  19. process.stdout.write(stats.toString({
  20. colors: true,
  21. modules: false,
  22. children: false,
  23. chunks: false,
  24. chunkModules: false
  25. }) + '\n\n')
  26. if (stats.hasErrors()) {
  27. console.log(chalk.red(' Build failed with errors.\n'))
  28. process.exit(1)
  29. }
  30. console.log(chalk.cyan(' Build complete.\n'))
  31. console.log(chalk.yellow(
  32. ' Tip: built files are meant to be served over an HTTP server.\n' +
  33. ' Opening index.html over file:// won\'t work.\n'
  34. ))
  35. if (process.env.npm_config_preview) {
  36. const port = 9526
  37. const host = "http://localhost:" + port
  38. const basePath = config.build.assetsPublicPath
  39. const app = connect()
  40. app.use(basePath, serveStatic('./dist', {
  41. 'index': ['index.html', '/']
  42. }))
  43. app.listen(port, function () {
  44. console.log(chalk.green(`> Listening at http://localhost:${port}${basePath}`))
  45. });
  46. }
  47. })
  48. })