|
|
|
@ -0,0 +1,28 @@
|
|
|
|
|
const fs = require('fs')
|
|
|
|
|
const path = require('path')
|
|
|
|
|
const sep = path.sep
|
|
|
|
|
|
|
|
|
|
function VersionPlugin (options) {
|
|
|
|
|
this.options = options || {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VersionPlugin.prototype.apply = function (compiler) {
|
|
|
|
|
var self = this
|
|
|
|
|
compiler.plugin('afterPlugins', function (params) { // 在 webpack 完成插件加载后执行
|
|
|
|
|
const packageJsonPath = path.join(params.context, sep + 'package.json') // 获取 package.json 文件路径
|
|
|
|
|
const dateStr = getDateStr() // 获取日期字符串
|
|
|
|
|
let packageJsonStr = fs.readFileSync(packageJsonPath, 'utf8') // 读取 package.json 文件内容
|
|
|
|
|
const r = new RegExp('(?<=version\\":\\s*\\")(.*)(?=")') // 构造正则表达式匹配 version 字段
|
|
|
|
|
packageJsonStr = packageJsonStr.replace(r, "1.0." + dateStr) // 更新 version 字段 dateStr是当天的日期没有秒,获取到秒可能更好 ⌈ 自行判断是否需要 now.getSeconds() ⌋
|
|
|
|
|
fs.writeFileSync(packageJsonPath, packageJsonStr, 'utf8') // 将更新后的内容写回 package.json 文件
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
function getDateStr () {
|
|
|
|
|
const now = new Date()
|
|
|
|
|
return now.getFullYear() + format(now.getMonth() + 1) + format(now.getDate()) + format(now.getHours()) + format(now.getMinutes())
|
|
|
|
|
function format (num) {
|
|
|
|
|
return num < 10 ? '0' + num : '' + num
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = VersionPlugin
|