build.js 1.5 KB
Newer Older
邓晓峰's avatar
邓晓峰 committed
1 2 3 4
const rm = require('rimraf');
const path = require('path');
const webpack = require('webpack');
const chalk = require('chalk');
杨思琦's avatar
杨思琦 committed
5
const ora = require('ora');
邓晓峰's avatar
邓晓峰 committed
6
const pkg = require('../../package.json');
杨思琦's avatar
杨思琦 committed
7
const webpackConfig = require('./webpack.prod.babel');
邓晓峰's avatar
邓晓峰 committed
8
const argv = require('../../server/argv');
邓晓峰's avatar
邓晓峰 committed
9
const spinner = ora('building for production...');
10
const { printFileSizes } = require('./buildDevUtil');
邓晓峰's avatar
邓晓峰 committed
11
// const Diff = require('diff');
12
/* eslint-disable */
邓晓峰's avatar
邓晓峰 committed
13 14
spinner.start();

15 16 17 18 19 20 21 22
rm(
  process.env.npm_config_releasepath
    ? path.resolve(
        process.env.npm_config_releasepath,
        pkg.name.toLocaleLowerCase(),
      )
    : `./${pkg.name.toLocaleLowerCase()}`,
  err => {
邓晓峰's avatar
邓晓峰 committed
23
  if (err) throw err
24 25 26
    // eslint-disable-next-line no-shadow
  webpack(webpackConfig, function (err, stats) {
    spinner.stop()
邓晓峰's avatar
邓晓峰 committed
27 28 29 30
    if (err) {
      console.log(err)
      throw err
    }
邓晓峰's avatar
邓晓峰 committed
31 32 33 34 35 36 37 38 39
    process.stdout.write(`${stats.toString({
      colors: true,
      color: true,
      progress: true,
      modules: false,
      children: false,
      chunks: true,
      chunkModules: true
    })  }\n\n`)
40
    printFileSizes(stats, path.relative(process.cwd(), webpackConfig.output.path));
41 42 43 44 45

    if (stats.hasErrors()) {
      console.log(chalk.red('  Build failed with errors.\n'))
      process.exit(1)
    }
邓晓峰's avatar
邓晓峰 committed
46

47 48 49 50 51 52 53 54
      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'
    ))
  })
})
邓晓峰's avatar
邓晓峰 committed
55