Newer
Older
casic-smartcity-well-front / build / build.js
wangxitong on 11 Jan 2021 2 KB 'init'
'use strict'
// npm和node版本检查
require('./check-versions')()
//设置环境变量为production
process.env.NODE_ENV = 'production'
// ora是一个命令行转圈圈动画插件,好看用的
const ora = require('ora')
// rimraf插件是用来执行UNIX命令rm和-rf的用来删除文件夹和文件,清空旧的文件
const rm = require('rimraf')
// node.js路径模块
const path = require('path')
// chalk插件,用来在命令行中输入不同颜色的文字
const chalk = require('chalk')
// 引入webpack模块使用内置插件和webpack方法
const webpack = require('webpack')
// 引入config下的index.js配置文件,此配置文件我之前介绍了请自行查阅,主要配置的是一些通用的选项
const config = require('../config')
// 下面是生产模式的webpack配置文件
const webpackConfig = require('./webpack.prod.conf')
// 开启转圈圈动画
const spinner = ora('building for production...')
spinner.start()
// 调用rm方法,第一个参数的结果就是 dist/static,表示删除这个路径下面的所有文件
rm(path.join(config.build.assetsRoot, config.build.assetsSubDirectory), err => {
  // 如果删除的过程中出现错误,就抛出这个错误,同时程序终止
  if (err) throw err
  // 没有错误,就执行webpack编译
  webpack(webpackConfig, (err, stats) => {
    // 这个回调函数是webpack编译过程中执行
    spinner.stop()// 停止转圈圈动画
    if (err) throw err
    // 没有错误就执行下面的代码,process.stdout.write和console.log类似,输出对象
    process.stdout.write(
      stats.toString({
        colors: true,
        modules: false,
        children: false,
        chunks: false,
        chunkModules: false
      }) + '\n\n'
    )
    // 以上就是在编译过程中,持续打印消息
    if (stats.hasErrors()) {
      console.log(chalk.red('  Build failed with errors.\n'))
      process.exit(1)
    }
    // 下面是编译成功的消息
    console.log(chalk.cyan('  Build complete.\n'))
    console.log(
      chalk.yellow(
        '  Tip: built files are meant to be served over an HTTP server.\n' +
          "  Opening index.html over file:// won't work.\n"
      )
    )
  })
})