中文 | English
自动插入版本号到你的 Vite/Nuxt 项目中.
pnpm add -D vite-plugin-version-mark
# 或者 yarn add -D vite-plugin-version-mark
# 或者 npm install -D vite-plugin-version-mark
// vite.config.ts
import {defineConfig} from 'vite'
import {vitePluginVersionMark} from 'vite-plugin-version-mark'
export default defineConfig({
plugins: [
vitePluginVersionMark({
// name: 'test-app',
// version: '0.0.1',
// command: 'git describe --tags',
ifGitSHA: true,
ifShortSHA: true,
ifMeta: true,
ifLog: true,
ifGlobal: true,
// outputFile: true,
})
],
})
// nuxt.config.ts
export default defineNuxtConfig({
modules: [
['vite-plugin-version-mark/nuxt', {
// name: 'test-app',
// version: '0.0.1',
// command: 'git describe --tags',
ifGitSHA: true,
ifShortSHA: true,
ifMeta: true,
ifLog: true,
ifGlobal: true,
// outputFile: true,
}]
],
})
至此,你就在开始使用 vite-plugin-version-mark
啦! 🎉
vite-plugin-version-mark
可以在Console
中打印版本号,也可以在全局定义变量供您使用, 同时支持在<meta>
标签中显示版本号。
属性 | 描述 | 类型 | 默认值 | 支持版本 |
---|---|---|---|---|
name | 应用名 | string |
在 package.json 中定义的 name 属性 |
0.0.1+ |
version | 应用版本 | string |
在 package.json 中定义的 version 属性 |
0.0.1+ |
ifGitSHA | 使用git commitSHA作为版本号 | boolean |
false | 0.0.1+ |
ifShortSHA | 使用git的短commitSHA作为版本号 | boolean |
false | 0.0.1+ |
command | 提供自定义指令,以便自定义版本号的获取方式 例如使用git tag作为版本号: git describe --tags |
string |
git rev-parse --short HEAD | 0.0.8+ |
ifLog | 在控制台打印版本信息 | boolean |
true | 0.0.1+ |
ifGlobal | 在window上定义变量 `__${APPNAME}_VERSION__` 对于TypeScript使用者, 请确保您在 env.d.ts 或者 vite-env.d.ts 文件中定义该变量,以便通过类型检查。 |
boolean |
true | 0.0.4+ |
ifMeta | 在 <head> 中添加 <meta name="application-name" content="{APPNAME_VERSION}: {version}"> |
boolean |
true | 0.0.1+ |
ifExport | 在入口文件导出版本字段。这在您使用vite构建 library mode 时或许会用到。通过 import { {APPNAME}_VERSION} from <your_library_name> |
boolean |
false | 0.0.11+ |
outputFile | 构建时根据版本生成一个静态文件,具体配置详见下方的 outputFile 配置项说明 |
boolean /function |
false | 0.1.1+ |
版本字段的取值优先级为:
command
>ifShortSHA
>ifGitSHA
>version
// vite.config.ts
import {defineConfig} from 'vite'
import type {Plugin} from 'vite'
import {vitePluginVersionMark} from 'vite-plugin-version-mark'
const yourPlugin: () => Plugin = () => ({
name: 'test-plugin',
config (config) {
// get version in vitePlugin if you open `ifGlobal`
console.log(config.define)
}
})
export default defineConfig({
plugins: [
vue(),
vitePluginVersionMark({
ifGlobal: true,
}),
yourPlugin(),
],
})
通过下方的 git
指令,可以列出所有包含指定 提交SHA
的分支。
git branch -r --contains <COMMIT_SHA>
查看 CHANGELOG
如需启用可设置为true,会在相对构建目录(vite默认为dist,nuxt3默认为.output/public)下创建路径为“.well-known/version”的文件,内容为当前版本号。
也可以设置为一个函数,该函数接收版本号作为参数,并返回一个对象,以便自行定义生成的内容信息,例如:
// vite.config.ts
vitePluginVersionMark({
// ...other options
outputFile: (version) => ({
path: 'custom/version.json',
content: `{"version":"${version}"}`,
})
}),
如此配置便可以生成一个名为“custom/version.json”的文件,内容为 {"version":"${当前版本号}"}
。