build.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict'
  2. require('./check-versions')()
  3. process.env.NODE_ENV = 'production'
  4. const ora = require('ora')
  5. const rm = require('rimraf')
  6. const path = require('path')
  7. const chalk = require('chalk')
  8. const webpack = require('webpack')
  9. const config = require('../config')
  10. const webpackConfig = require('./webpack.prod.conf')
  11. // 在node.js的console上會有轉圈圈的動畫
  12. const spinner = ora('building for production...')
  13. spinner.start()
  14. // rm(pathName, function(err){}) 這個方法是用來刪除文件或資料夾的
  15. // 先清空發佈資料夾的東西
  16. rm(path.join(config.build.assetsRoot, config.build.assetsSubDirectory), err => {
  17. if (err) throw err
  18. //開始打包
  19. webpack(webpackConfig, (err, stats) => {
  20. spinner.stop()
  21. if (err) throw err
  22. process.stdout.write(stats.toString({
  23. colors: true,
  24. modules: false,
  25. children: false, // If you are using ts-loader, setting this to true will make TypeScript errors show up during build.
  26. chunks: false,
  27. chunkModules: false
  28. }) + '\n\n')
  29. if (stats.hasErrors()) {
  30. console.log(chalk.red(' Build failed with errors.\n'))
  31. process.exit(1)
  32. }
  33. console.log(chalk.cyan(' Build complete.\n'))
  34. console.log(chalk.yellow(
  35. ' Tip: built files are meant to be served over an HTTP server.\n' +
  36. ' Opening index.html over file:// won\'t work.\n'
  37. ))
  38. })
  39. })