Skip to content

Commit

Permalink
feat: update ifGlobal
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhongxuYang committed Jul 29, 2023
1 parent aed3222 commit e34e6ba
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 29 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## v0.0.9

* The ifGlobal setting mode is switched to vite.define, so that the node.js environment can read (by [@censujiang](https://github.com/ZhongxuYang/vite-plugin-version-mark/pull/4))

## v0.0.8

* Support provide a custom command to retrieve the version (by [@kgutwin](https://github.com/kgutwin))
Expand Down
41 changes: 31 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Then you can use `vite-plugin-version-mark` ! 🎉

## Config

`vite-plugin-version-mark` can be print application version in the `Console` or add `<meta>` in HTML element.
> `vite-plugin-version-mark` can be print application version in the `Console`, defined `global` or add `<meta>` in HTML element.
| name | description | type | default | supported |
| --- | --- | --- | --- | --- |
Expand All @@ -86,19 +86,40 @@ Then you can use `vite-plugin-version-mark` ! 🎉
| ifGitSHA | use git commit SHA as the version | `boolean` | false | `0.0.1+` |
| ifShortSHA | use git commit short SHA as the version | `boolean` | true | `0.0.1+` |
| ifLog | print info in the Console | `boolean` | true | `0.0.1+` |
| ifGlobal | set a variable named *\`\_\_${APPNAME}\_VERSION\_\_\`* in the window | `boolean` | true | `0.0.4+` |
| ifGlobal | set a variable named *\`\_\_${APPNAME}\_VERSION\_\_\`* in the window<br/>[For TypeScript users, make sure to add the type declarations in the env.d.ts or vite-env.d.ts file to get type checks and Intellisense.](https://vitejs.dev/config/shared-options.html#define) | `boolean` | true | `0.0.4+` |
| ifMeta | add \<meta name="application-name" content="{APPNAME_VERSION}: {version}"> in the \<head> | `boolean` | true | `0.0.1+` |
| command | provide a custom command to retrieve the version <br/>For example: `git describe --tags` | `string` | git rev-parse --short HEAD | `0.0.8+` |


<!-- - `name` - application name (`name in package.json` by default)
- `version` - application version (`version in package.json` by default)
- `ifGitSHA` - use git commit SHA as the version (`false` by default)
- `ifShortSHA` - use git commit short SHA (`true` by default)
- `ifMeta` - add \<meta name="application-name" content="{APPNAME_VERSION}: {version}"> in the \<head> (`true` by default)
- `ifLog` - print info in the Console (`true` by default)
- `ifGlobal` - set a variable named *\`\_\_${APPNAME}\_VERSION\_\_\`* in the window. (`true` by default)
- `command` - provide a custom command to retrieve the version. For example: `git describe --tags` (`git rev-parse --short HEAD` by default) -->
## Other

### How to get the version in your vitePlugin?
```ts
// 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(),
],
})

```

View [CHANGELOG](./CHANGELOG.md)

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{
"name": "vite-plugin-version-mark",
"version": "0.0.8",
"version": "0.0.9",
"description": "Automatically insert the version or git_commit_sha in your Vite/Nuxt project.",
"keywords": [
"git",
"commit",
"log",
"version",
"plugin",
"vite",
"plugin",
"vite-plugin",
"nuxt"
],
"author": "ZhongxuYang <himatthew@foxmail.com>",
Expand Down
13 changes: 10 additions & 3 deletions src/plugins/core/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@ interface VitePluginVersionMarkCommandInput extends VitePluginVersionMarkBaseInp
command?: string
}


export type VitePluginVersionMarkInput = VitePluginVersionMarkGitInput & VitePluginVersionMarkCommandInput
export type VitePluginVersionMarkConfig = {
ifMeta: boolean
ifLog: boolean
ifGlobal: boolean
printVersion: string
printName: string
printInfo: string
}

const getGitSHA = (ifShortSHA: boolean, command: string | undefined) => {
const {exec} = childProcess
Expand All @@ -42,7 +49,7 @@ const getGitSHA = (ifShortSHA: boolean, command: string | undefined) => {
})
}

export const analyticOptions = async (options: VitePluginVersionMarkInput) => {
export const analyticOptions: (options: VitePluginVersionMarkInput) => Promise<VitePluginVersionMarkConfig> = async (options) => {
const {
name = process.env['npm_package_name'],
version = process.env['npm_package_version'],
Expand All @@ -54,7 +61,7 @@ export const analyticOptions = async (options: VitePluginVersionMarkInput) => {
command = undefined,
} = options

const printVersion = ifGitSHA ? await getGitSHA(ifShortSHA, command) : version
const printVersion = (ifGitSHA ? await getGitSHA(ifShortSHA, command) : version) as string
const printName = `${name?.replace(/((?!\w).)/g, '_')?.toLocaleUpperCase?.()}_VERSION`
const printInfo = `${printName}: ${printVersion}`

Expand Down
5 changes: 2 additions & 3 deletions src/plugins/nuxt3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const nuxtVersionMark = defineNuxtModule<ModuleOptions>({

nuxt.options.app.head.meta = nuxt.options.app.head.meta || []
nuxt.options.app.head.script = nuxt.options.app.head.script || []
nuxt.options.vite.define = nuxt.options.vite.define || {}

ifMeta && nuxt.options.app.head.meta.push({
name: 'application-name',
Expand All @@ -28,9 +29,7 @@ const nuxtVersionMark = defineNuxtModule<ModuleOptions>({
ifLog && nuxt.options.app.head.script.push({
children: `console.log("${printInfo}")`
})
ifGlobal && nuxt.options.app.head.script.push({
children: `__${printName}__ = "${printVersion}"`
})
ifGlobal && (nuxt.options.vite.define[ `__${printName}__`] = JSON.stringify(printVersion))
}
})

Expand Down
37 changes: 26 additions & 11 deletions src/plugins/vite.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,39 @@
import {VitePluginVersionMarkInput, analyticOptions} from './core/main'
import {analyticOptions} from './core/main'
import type {VitePluginVersionMarkInput, VitePluginVersionMarkConfig} from './core/main'
import type {Plugin, IndexHtmlTransformResult} from 'vite'

export const vitePluginVersionMark: (options?: VitePluginVersionMarkInput) => Plugin = (options = {}) => {
let versionMarkConfig: VitePluginVersionMarkConfig
const getVersionMarkConfig = async () => {
if (!versionMarkConfig) versionMarkConfig = await analyticOptions(options)
return versionMarkConfig
}
return {
name: 'vite-plugin-version-mark',

async transformIndexHtml() {
async config () {
const {
ifMeta,
ifLog,
ifGlobal,
printVersion,
printName,
printVersion,
} = await getVersionMarkConfig()

if (ifGlobal) {
const keyName = `__${printName}__`
return {
define: {
[keyName]: JSON.stringify(printVersion)
},
}
}
},

async transformIndexHtml() {
const {
ifLog,
ifMeta,
printInfo,
} = await analyticOptions(options)
} = await getVersionMarkConfig()

const els: IndexHtmlTransformResult = []
ifMeta && els.push({
Expand All @@ -29,11 +49,6 @@ export const vitePluginVersionMark: (options?: VitePluginVersionMarkInput) => Pl
injectTo: 'body',
children: `console.log("${printInfo}")`
})
ifGlobal && els.push({
tag: 'script',
injectTo: 'body',
children: `__${printName}__ = "${printVersion}"`
})

return els
},
Expand Down

0 comments on commit e34e6ba

Please sign in to comment.