build.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. 'use strict'
  2. require('./check-versions')()
  3. const fs = require('fs');
  4. process.env.NODE_ENV = 'production'
  5. const ora = require('ora')
  6. const rm = require('rimraf')
  7. const path = require('path')
  8. const chalk = require('chalk')
  9. const webpack = require('webpack')
  10. const config = require('../config')
  11. const webpackConfig = require('./webpack.prod.conf')
  12. const spinner = ora('building for production...')
  13. spinner.start()
  14. rm(path.join(config.build.assetsRoot, config.build.assetsSubDirectory), err => {
  15. if (err) throw err
  16. webpack(webpackConfig, function (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. const fs = require('fs');
  36. const git_hash = () => {
  37. try {
  38. const rev = fs.readFileSync('.git/HEAD').toString().trim().split(/.*[: ]/).slice(-1)[0];
  39. if (rev.indexOf('/') === -1) {
  40. return rev;
  41. } else {
  42. return fs.readFileSync('.git/' + rev).toString().trim();
  43. }
  44. } catch (e) {
  45. // eslint-disable-next-line no-console
  46. console.log(e);
  47. return 'git_hash_error';
  48. }
  49. }
  50. const git_branch = () => {
  51. try {
  52. const revBase = fs.readFileSync('.git/HEAD').toString().trim()
  53. return revBase.split('refs/heads/')[1]
  54. }catch (e) {
  55. // eslint-disable-next-line no-console
  56. console.log(e);
  57. return 'git_branch_error';
  58. }
  59. }
  60. const getVersion = () => {
  61. const raw = fs.readFileSync('./package.json').toString();
  62. const packageJson = JSON.parse(/{[\s\S]*}/mg.exec(raw)[0]);
  63. // eslint-disable-next-line no-console
  64. // console.log(packageJson.version);
  65. return packageJson.version;
  66. }
  67. const PROJECT_NAME = '追迹者-第三方支付数据分析'
  68. const GIT_COMMIT = git_hash()
  69. const TICK = getVersion();
  70. const BUILD_TIME_BASE = new Date(new Date().getTime() + 28800000)
  71. const BUILD_TIME = BUILD_TIME_BASE.toUTCString()
  72. // console.log(BUILD_TIME_BASE.toLocaleString(),BUILD_TIME_BASE.getFullYear(),BUILD_TIME_BASE.getMonth()+1,BUILD_TIME_BASE.getDate())
  73. console.log(chalk.red(`PROJECT_NAME: ${PROJECT_NAME}`));
  74. console.log(chalk.red('BUILD_TIME:', BUILD_TIME))
  75. console.log(chalk.red('GIT_COMMIT:', GIT_COMMIT))
  76. console.log(chalk.red('GIT_BRANCH:', git_branch()))
  77. console.log(chalk.red('FILE_NAME:', PROJECT_NAME+'版'+TICK+'-('+git_branch()+')-'+GIT_COMMIT.slice(0, 8)+'('+BUILD_TIME+')'))
  78. console.log(chalk.red('TICK:', TICK))
  79. let html = fs.readFileSync('dist/index.html').toString();
  80. html += '\r\n<!-- ';
  81. html = html + '\r\n PROJECT_NAME: ' + PROJECT_NAME + ';';
  82. html = html + '\r\n BUILD_TIME:' + BUILD_TIME + '; ';
  83. html = html + '\r\n GIT_BRANCH:' + git_branch() + '; ';
  84. html = html + '\r\n GIT_COMMIT:' + GIT_COMMIT + '; ';
  85. html = html + '\r\n TICK:' + TICK + '; ';
  86. html += '\r\n -->';
  87. html = html.replace('VUE_APP_SHORT_VERSION_NAME', TICK);
  88. fs.writeFileSync('dist/index.html', html);
  89. // eslint-disable-next-line no-console
  90. console.log(chalk.green(`insert timestamp and env into index.html.`));
  91. })
  92. })