'use strict' require('./check-versions')() const fs = require('fs'); process.env.NODE_ENV = 'production' const ora = require('ora') const rm = require('rimraf') const path = require('path') const chalk = require('chalk') const webpack = require('webpack') const config = require('../config') const webpackConfig = require('./webpack.prod.conf') const spinner = ora('building for production...') spinner.start() rm(path.join(config.build.assetsRoot, config.build.assetsSubDirectory), err => { if (err) throw err webpack(webpackConfig, function (err, stats) { spinner.stop() if (err) throw err 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' )) const fs = require('fs'); const git_hash = () => { try { const rev = fs.readFileSync('.git/HEAD').toString().trim().split(/.*[: ]/).slice(-1)[0]; if (rev.indexOf('/') === -1) { return rev; } else { return fs.readFileSync('.git/' + rev).toString().trim(); } } catch (e) { // eslint-disable-next-line no-console console.log(e); return 'git_hash_error'; } } const git_branch = () => { try { const revBase = fs.readFileSync('.git/HEAD').toString().trim() return revBase.split('refs/heads/')[1] }catch (e) { // eslint-disable-next-line no-console console.log(e); return 'git_branch_error'; } } const getVersion = () => { const raw = fs.readFileSync('./package.json').toString(); const packageJson = JSON.parse(/{[\s\S]*}/mg.exec(raw)[0]); // eslint-disable-next-line no-console // console.log(packageJson.version); return packageJson.version; } const PROJECT_NAME = '追迹者-第三方支付数据分析' const GIT_COMMIT = git_hash() const TICK = getVersion(); const BUILD_TIME_BASE = new Date(new Date().getTime() + 28800000) const BUILD_TIME = BUILD_TIME_BASE.toUTCString() // console.log(BUILD_TIME_BASE.toLocaleString(),BUILD_TIME_BASE.getFullYear(),BUILD_TIME_BASE.getMonth()+1,BUILD_TIME_BASE.getDate()) console.log(chalk.red(`PROJECT_NAME: ${PROJECT_NAME}`)); console.log(chalk.red('BUILD_TIME:', BUILD_TIME)) console.log(chalk.red('GIT_COMMIT:', GIT_COMMIT)) console.log(chalk.red('GIT_BRANCH:', git_branch())) console.log(chalk.red('FILE_NAME:', PROJECT_NAME+'版'+TICK+'-('+git_branch()+')-'+GIT_COMMIT.slice(0, 8)+'('+BUILD_TIME+')')) console.log(chalk.red('TICK:', TICK)) let html = fs.readFileSync('dist/index.html').toString(); html += '\r\n'; html = html.replace('VUE_APP_SHORT_VERSION_NAME', TICK); fs.writeFileSync('dist/index.html', html); // eslint-disable-next-line no-console console.log(chalk.green(`insert timestamp and env into index.html.`)); }) })