Skip to content

Commit

Permalink
fix: 修复 build 时间跟随系统的问题
Browse files Browse the repository at this point in the history
原方案将 moment 嵌入 showInfo 会导致时间跟随系统
现在会在 build 之前将时间写入 /temp/build-time.json 并在 build 时调用
  • Loading branch information
crrashh1542 committed Jul 10, 2023
1 parent ce1605e commit c338844
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.DS_Store
node_modules
/dist
/temp


# local env files
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "crash-homepage",
"version": "2.2.0-beta.0",
"version": "2.2.0-beta.1",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
Expand Down
19 changes: 9 additions & 10 deletions src/functions/showInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@
* @version 1.1.0
*/

import info from "../../package.json"
import moment from "moment"
import info from '../../package.json'
import time from '../../temp/build-time.json'

// 获取项目和框架版本
const repoVer = info.version
const frameVer = info.dependencies.vue.split("^")[1]
const frameVer = info.dependencies.vue.split('^')[1]
// 获取时间和日期
const date = moment(Date.now()).format("YYYYMMDD")
const time = moment(Date.now()).format("HHmmss")
let build = "Version: " + repoVer + "@" + date + "T" + time + "+0800"

let build = 'Version: ' + repoVer + '@' + time.t

export default function showInfo(){
console.log("%c%s", "font-size: 14px; margin: 5px", build)
console.log("%c%s",
"color: #fff; background: #42b883; padding: 5px 10px; border-radius: 4px; font-size: 14px;",
"Built with Vue " + frameVer + ".")
console.log('%c%s', 'font-size: 14px; margin: 5px', build)
console.log('%c%s',
'color: #fff; background: #42b883; padding: 5px 10px; border-radius: 4px; font-size: 14px;',
'Built with Vue ' + frameVer + '.')
}
19 changes: 17 additions & 2 deletions vue.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,29 @@

// 引入配置及插件
const { defineConfig } = require('@vue/cli-service')
const { resolve } = require("path")
const { resolve } = require('path')
const fs = require('fs')
const moment = require('moment')
const SpeedMeasurePlugin = require('speed-measure-webpack-plugin')
const CompressionWebpackPlugin = require("compression-webpack-plugin")
const CompressionWebpackPlugin = require('compression-webpack-plugin')

// 生产模式配置
const productionGzipExtensions = /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i
const IS_PROD = ['production', 'prod'].includes(process.env.NODE_ENV)

// 获取 build 的时间并写入
const date = moment(Date.now()).format('YYYYMMDD')
const time = moment(Date.now()).format('HHmmss')
const timeData = '{"t": "' + date + 'T' + time + '+0800"}'
fs.writeFile('./temp/build-time.json', timeData, err => {
// 如果 build 时间写入成功,返回 null
if(err === null) {
console.log('[buildTimer] build 时间写入成功!')
} else {
console.log('[buildTimer] build 时间写入失败,详情请参阅:\n' + err)
}
})

module.exports = defineConfig({

transpileDependencies: true,
Expand Down

0 comments on commit c338844

Please sign in to comment.