diff --git a/README.md b/README.md index 9027843..66b81a6 100644 --- a/README.md +++ b/README.md @@ -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, @@ -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, @@ -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 @@ -141,25 +162,6 @@ Through the `git` command below, you can list all branches containing the specif git branch -r --contains ``` -### 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) diff --git a/README_ZH.md b/README_ZH.md index 6544df2..f5086bd 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -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, }) ], }) @@ -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, }] ], }) @@ -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插件中获取版本号? @@ -143,25 +163,6 @@ export default defineConfig({ git branch -r --contains ``` -### `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) diff --git a/playground/vite-lib/dummy-non-existing-folder/.well-known/version b/playground/vite-lib/dummy-non-existing-folder/.well-known/version new file mode 100644 index 0000000..f4f0ddc --- /dev/null +++ b/playground/vite-lib/dummy-non-existing-folder/.well-known/version @@ -0,0 +1 @@ +f1537e9 \ No newline at end of file diff --git a/playground/vite-lib/tsconfig.json b/playground/vite-lib/tsconfig.json index 8b42f77..6440f32 100644 --- a/playground/vite-lib/tsconfig.json +++ b/playground/vite-lib/tsconfig.json @@ -19,5 +19,5 @@ "noUnusedParameters": true, "noFallthroughCasesInSwitch": true }, - "include": ["src"] + "include": ["src", "../../src/plugins/vite"] } diff --git a/playground/vite-webapp/tsconfig.json b/playground/vite-webapp/tsconfig.json index f82888f..2220030 100644 --- a/playground/vite-webapp/tsconfig.json +++ b/playground/vite-webapp/tsconfig.json @@ -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" }] } diff --git a/playground/vite-webapp/tsconfig.node.json b/playground/vite-webapp/tsconfig.node.json index 42872c5..f754156 100644 --- a/playground/vite-webapp/tsconfig.node.json +++ b/playground/vite-webapp/tsconfig.node.json @@ -6,5 +6,5 @@ "moduleResolution": "bundler", "allowSyntheticDefaultImports": true }, - "include": ["vite.config.ts"] + "include": ["vite.config.ts", "../../src/plugins/vite.ts"] } diff --git a/playground/vite-webapp/vite.config.ts b/playground/vite-webapp/vite.config.ts index 47ddf75..e0c5c0a 100644 --- a/playground/vite-webapp/vite.config.ts +++ b/playground/vite-webapp/vite.config.ts @@ -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(), ], diff --git a/src/plugins/core/index.ts b/src/plugins/core/index.ts index cf67436..f539b86 100644 --- a/src/plugins/core/index.ts +++ b/src/plugins/core/index.ts @@ -1,5 +1,11 @@ import childProcess from 'child_process' +interface OutputFile { + path: string; + content: string +} +type OutputFileFunction = (version: string) => OutputFile | OutputFile[] + interface VitePluginVersionMarkBaseInput { name?: string version?: string @@ -7,6 +13,7 @@ interface VitePluginVersionMarkBaseInput { ifLog?: boolean ifGlobal?: boolean ifExport?: boolean + outputFile?: boolean | OutputFileFunction } interface VitePluginVersionMarkGitInput extends VitePluginVersionMarkBaseInput { @@ -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 }[]