Skip to content

Commit

Permalink
chore: update type & docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhongxuYang committed Oct 21, 2024
1 parent f1537e9 commit 1df2852
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 56 deletions.
40 changes: 21 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export default defineConfig({
// name: 'test-app',
// version: '0.0.1',
// command: 'git describe --tags',
// outputFile: true,
// ifGitSHA: true,
ifShortSHA: true,
ifMeta: true,
Expand All @@ -71,6 +72,7 @@ export default defineNuxtConfig({
// name: 'test-app',
// version: '0.0.1',
// command: 'git describe --tags',
// outputFile: true,
// ifGitSHA: true,
ifShortSHA: true,
ifMeta: true,
Expand Down Expand Up @@ -102,6 +104,25 @@ Then you can use `vite-plugin-version-mark` ! 🎉

> The **version field** takes precedence: `command` > `ifShortSHA` > `ifGitSHA` > `version`
### `outputFile` Configuration Option

If you want to enable it, you can set it to `true`, and it will create a file with the path `.well-known/version` and the content of the current version number in the relative build directory (*dist* for `vite` and *.output/public* for `nuxt3`).

Alternatively, it can be set to a function that takes the version number as a parameter and returns an object. This allows you to define the content information generated, for example:

```ts
// vite.config.ts
vitePluginVersionMark({
// ...other options

outputFile: (version) => ({
path: 'custom/version.json',
content: `{"version":"${version}"}`,
})
}),
```

With this configuration, a file named `custom/version.json` will be generated, and its content will be `{"version":"${current version number}"}`.

## Other

Expand Down Expand Up @@ -141,25 +162,6 @@ Through the `git` command below, you can list all branches containing the specif
git branch -r --contains <COMMIT_SHA>
```

### outputFile Configuration Option

If you want to enable it, you can set it to `true`, and it will create a file with the path `.well-known/version` and the content of the current version number in the relative build directory (*dist* for `vite` and *.output/public* for `nuxt3`).

Alternatively, it can be set to a function that takes the version number as a parameter and returns an object. This allows you to define the content information generated, for example:

```ts
// vite.config.ts
vitePluginVersionMark({
// ...other options
outputFile: (version) => ({
path: 'custom/version.json',
content: `{"version":"${version}"}`,
})
}),
```

With this configuration, a file named `custom/version.json` will be generated, and its content will be `{"version":"${current version number}"}`.

## Star History

[![Star History Chart](https://api.star-history.com/svg?repos=ZhongxuYang/vite-plugin-version-mark&type=Date)](https://star-history.com/#ZhongxuYang/vite-plugin-version-mark&Date)
Expand Down
47 changes: 24 additions & 23 deletions README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ export default defineConfig({
// name: 'test-app',
// version: '0.0.1',
// command: 'git describe --tags',
ifGitSHA: true,
// outputFile: true,
// ifGitSHA: true,
ifShortSHA: true,
ifMeta: true,
ifLog: true,
ifGlobal: true,
// outputFile: true,
})
],
})
Expand All @@ -73,12 +73,12 @@ export default defineNuxtConfig({
// name: 'test-app',
// version: '0.0.1',
// command: 'git describe --tags',
ifGitSHA: true,
// outputFile: true,
// ifGitSHA: true,
ifShortSHA: true,
ifMeta: true,
ifLog: true,
ifGlobal: true,
// outputFile: true,
}]
],
})
Expand All @@ -105,6 +105,26 @@ export default defineNuxtConfig({

> **版本字段**的取值优先级为: `command` > `ifShortSHA` > `ifGitSHA` > `version`
### `outputFile` 配置项说明

如需启用可设置为 `true`,会在相对构建目录(`vite` 默认为 *dist*`nuxt3` 默认为 *.output/public*)下创建路径为 `.well-known/version` 的文件,内容为当前版本号。

也可以设置为一个函数,该函数接收版本号作为参数,并返回一个对象,以便自行定义生成的内容信息,例如:

```ts
// vite.config.ts
vitePluginVersionMark({
// ...other options

outputFile: (version) => ({
path: 'custom/version.json',
content: `{"version":"${version}"}`,
})
}),
```

如此配置便可以生成一个名为 `custom/version.json` 的文件,内容为 `{"version":"${当前版本号}"}`

## 其它

### 如何在您的vite插件中获取版本号?
Expand Down Expand Up @@ -143,25 +163,6 @@ export default defineConfig({
git branch -r --contains <COMMIT_SHA>
```

### `outputFile` 配置项说明

如需启用可设置为 `true`,会在相对构建目录(`vite` 默认为 *dist*`nuxt3` 默认为 *.output/public*)下创建路径为 `.well-known/version` 的文件,内容为当前版本号。

也可以设置为一个函数,该函数接收版本号作为参数,并返回一个对象,以便自行定义生成的内容信息,例如:

```ts
// vite.config.ts
vitePluginVersionMark({
// ...other options
outputFile: (version) => ({
path: 'custom/version.json',
content: `{"version":"${version}"}`,
})
}),
```

如此配置便可以生成一个名为 `custom/version.json` 的文件,内容为 `{"version":"${当前版本号}"}`

## Star History

[![Star History Chart](https://api.star-history.com/svg?repos=ZhongxuYang/vite-plugin-version-mark&type=Date)](https://star-history.com/#ZhongxuYang/vite-plugin-version-mark&Date)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
f1537e9
2 changes: 1 addition & 1 deletion playground/vite-lib/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src"]
"include": ["src", "../../src/plugins/vite"]
}
2 changes: 1 addition & 1 deletion playground/vite-webapp/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"],
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue", "../../src/plugins/vite.ts"],
"references": [{ "path": "./tsconfig.node.json" }]
}
2 changes: 1 addition & 1 deletion playground/vite-webapp/tsconfig.node.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true
},
"include": ["vite.config.ts"]
"include": ["vite.config.ts", "../../src/plugins/vite.ts"]
}
3 changes: 2 additions & 1 deletion playground/vite-webapp/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ export default defineConfig({
vue(),
vitePluginVersionMark({
// ifGitSHA: true,
ifShortSHA: true,
// command: 'git describe --tags',
ifShortSHA: true,
ifMeta: true,
ifLog: true,
ifGlobal: true,
ifExport: true,
outputFile: true,
}),
testPlugin(),
],
Expand Down
18 changes: 8 additions & 10 deletions src/plugins/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import childProcess from 'child_process'

interface OutputFile {
path: string;
content: string
}
type OutputFileFunction = (version: string) => OutputFile | OutputFile[]

interface VitePluginVersionMarkBaseInput {
name?: string
version?: string
ifMeta?: boolean
ifLog?: boolean
ifGlobal?: boolean
ifExport?: boolean
outputFile?: boolean | OutputFileFunction
}

interface VitePluginVersionMarkGitInput extends VitePluginVersionMarkBaseInput {
Expand All @@ -17,16 +24,7 @@ interface VitePluginVersionMarkCommandInput extends VitePluginVersionMarkBaseInp
command?: string
}

interface OutputFile {
path: string;
content: string
}
type OutputFileFunction = (version: string) => OutputFile | OutputFile[]
interface VitePluginVersionMarkFileInput extends VitePluginVersionMarkBaseInput {
outputFile?: boolean | OutputFileFunction
}

export type VitePluginVersionMarkInput = VitePluginVersionMarkGitInput & VitePluginVersionMarkCommandInput & VitePluginVersionMarkFileInput
export type VitePluginVersionMarkInput = VitePluginVersionMarkGitInput & VitePluginVersionMarkCommandInput

export type VitePluginVersionMarkConfig = {
fileList: { path: string, content: string }[]
Expand Down

0 comments on commit 1df2852

Please sign in to comment.