vite-plugin-version-info 1.1.0 โ†’ 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -8,7 +8,6 @@ A Vite plugin that injects version metadata into your app at build time. Useful
8
8
 
9
9
  - Injects version string and commit date from Git and `package.json`
10
10
  - Fully customizable version format via pattern tokens
11
- - Locale-aware and timezone-aware commit date and time formatting
12
11
  - Detects missing license and appends customizable suffix
13
12
  - Adds branch suffix based on `NODE_ENV`
14
13
  - Injects global constants at build time โ€” no runtime overhead or bundle size impact
@@ -34,10 +33,9 @@ export default defineConfig({
34
33
  plugins: [
35
34
  /* other plugins */
36
35
  versionInfo({
37
- locale: 'en-GB',
38
36
  commitHashLength: 8,
39
- noLicenseSuffix: '-proprietary',
40
- pattern: '{version}-{commitHash}{noLicenseSuffix}',
37
+ noLicenseSuffix: '(proprietary)',
38
+ pattern: '{version}-{commitHash} {noLicenseSuffix}',
41
39
  }),
42
40
  ],
43
41
  });
@@ -59,26 +57,34 @@ export default defineConfig({
59
57
  If using typescript, define the exposed constants in a separate `src/globals.d.ts` file:
60
58
 
61
59
  ```ts
62
- declare const __APP_VERSION__: string;
63
- declare const __COMMIT_DATE__: string;
60
+ declare const __VERSION_INFO__: string;
61
+ declare const __VERSION_INFO_COMMIT_DATE__: string;
62
+ declare const __VERSION_INFO_COMMIT_HASH__: string;
63
+ declare const __VERSION_INFO_VERSION__: string;
64
+ declare const __VERSION_INFO_BRANCH__: string;
64
65
  ```
65
66
 
66
67
  Then use the global constants injected by the plugin anywhere in your app:
67
68
 
68
69
  ```ts
69
- console.log(`Build version: ${__APP_VERSION__} - committed on ${__COMMIT_DATE__}`);
70
+ const commitDate = new Date(__VERSION_INFO_COMMIT_DATE__).toLocaleString('en-US', {
71
+ month: 'short',
72
+ day: '2-digit',
73
+ year: 'numeric',
74
+ } satisfies Intl.DateTimeFormatOptions);
75
+ console.log(`Build version: ${__APP_VERSION__} - committed on ${commitDate}`);
70
76
  ```
71
77
 
72
78
  Example output:
73
79
 
74
80
  ```
75
- Build version: 1.2.3-4afb2a1e-proprietary - committed on July 7, 2025
81
+ Build version: 1.2.3-4afb2a1e (proprietary) - committed on Mar 22, 2026
76
82
  ```
77
83
 
78
84
  Or, using defaults:
79
85
 
80
86
  ```
81
- 1.2.3-nonfree develop-4afb2a1e - commited on July 7, 2025
87
+ 1.2.3+develop.4afb2a1e (non-free) - committed on Mar 22, 2025
82
88
  ```
83
89
 
84
90
  ### ๐Ÿงช Injected Globals
@@ -86,31 +92,32 @@ Or, using defaults:
86
92
  These globals are injected at build time and can be accessed anywhere in your UI code or logic.
87
93
 
88
94
  | Global | Type | Description |
89
- | ----------------- | -------- | --------------------------------------- |
90
- | `__APP_VERSION__` | `string` | Fully formatted version string |
91
- | `__COMMIT_DATE__` | `string` | Formatted date of the latest Git commit |
95
+ |-------------------|----------|-----------------------------------------|
96
+ | `__VERSION_INFO__` | `string` | Fully formatted version string |
97
+ | `__VERSION_INFO_COMMIT_DATE__` | `string` | ISO timestamp of the latest Git commit |
98
+ | `__VERSION_INFO_COMMIT_HASH__` | `string` | Git commit hash |
99
+ | `__VERSION_INFO_VERSION__` | `string` | bare version |
100
+ | `__VERSION_INFO_BRANCH__` | `string` | Git branch name |
92
101
 
93
102
  ### ๐Ÿ› ๏ธ Options
94
103
 
95
- | Option | Type | Default | Description |
96
- | ------------------ | ---------------------------- | ------------------------------------------------------------------------- | --------------------------------------------------------------- |
97
- | `locale` | `string` | `'en-US'` | Locale used to format the commit date |
98
- | `commitHashLength` | `number` | `8` | Number of characters to use from the Git commit hash |
99
- | `intlOptions` | `Intl.DateTimeFormatOptions` | `{ dateStyle: 'long' }` | Intl.DateTimeFormat options to customize commit date formatting |
100
- | `noLicenseSuffix` | `string` | `'-nonfree'` | Suffix appended to version string if no license is found |
101
- | `pattern` | `string` | `'{version}{noLicenseSuffix} {currentBranch}-{commitHash}{branchSuffix}'` | Template string to format the final version string |
104
+ | Option | Type | Default | Description |
105
+ |--------------------|----------|---------------------------------------------------------------------|----------------------------------------------------------|
106
+ | `commitHashLength` | `number` | `8` | Number of characters to use from the Git commit hash |
107
+ | `noLicenseSuffix` | `string` | `'non-free'` | Suffix appended to version string if no license is found |
108
+ | `pattern` | `string` | `'{version}{suffix}+{branch}.{commitHash} {noLicenseSuffix}'` | Template string to format the final version string |
102
109
 
103
110
  ### ๐Ÿงฉ Pattern Tokens
104
111
 
105
112
  You can customize the `versionString` using a template string with the following tokens. These will be replaced at build time:
106
113
 
107
114
  | Token | Description | Example |
108
- | ------------------- | ----------------------------------------------- | -------------------------------------- |
115
+ |---------------------|-------------------------------------------------|----------------------------------------|non-free
109
116
  | `{version}` | Version from your `package.json` | `1.2.3` |
110
- | `{noLicenseSuffix}` | Appended if `license` field is missing | `-nonfree`, `-unlicensed`, or empty |
111
- | `{currentBranch}` | Current Git branch name | `main`, `develop`, `feature/login-fix` |
117
+ | `{noLicenseSuffix}` | Appended if `license` field is missing | e.g. `non-free`, `MIT` or empty |
118
+ | `{branch}` | Current Git branch name | `main`, `develop`, `feature/login-fix` |
112
119
  | `{commitHash}` | Git commit hash truncated to `commitHashLength` | `4afb2a1e` |
113
- | `{branchSuffix}` | `-dev` if `NODE_ENV` starts with `"dev"` | `-dev` or empty |
120
+ | `{suffix}` | `-dev` if `NODE_ENV` starts with `"dev"` | `-dev` or empty |
114
121
 
115
122
  #### ๐Ÿ”ง Example Output
116
123
 
@@ -118,7 +125,7 @@ Given the following plugin options:
118
125
 
119
126
  ```ts
120
127
  {
121
- pattern: '{version}{noLicenseSuffix}-{currentBranch}-{commitHash}{branchSuffix}',
128
+ pattern: '{version}+{branch}.{commitHash}',
122
129
  commitHashLength: 8,
123
130
  }
124
131
  ```
@@ -134,7 +141,7 @@ And your environment:
134
141
  Then the result will be:
135
142
 
136
143
  ```ts
137
- versionString = '1.2.3-nonfree-develop-4afb2a1e-dev';
144
+ versionString = '1.2.3-dev+develop.4afb2a1e';
138
145
  ```
139
146
 
140
147
  You can fully control the format by editing the pattern string.
package/dist/index.cjs CHANGED
@@ -28,12 +28,6 @@ function _interopNamespaceDefault(e) {
28
28
 
29
29
  var child_process__namespace = /*#__PURE__*/_interopNamespaceDefault(child_process);
30
30
 
31
- function formatDate(date, locale, options = {
32
- dateStyle: "long",
33
- timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone
34
- }) {
35
- return new Intl.DateTimeFormat(locale, options).format(date);
36
- }
37
31
  function formatVersionString(pattern, context) {
38
32
  return pattern.replace(/\{(\w+)\}/g, (_, key) => {
39
33
  return context[key] !== void 0 ? context[key] : `{${key}}`;
@@ -42,16 +36,16 @@ function formatVersionString(pattern, context) {
42
36
  function getBranchSuffix() {
43
37
  return process.env.NODE_ENV?.startsWith("dev") ? "-dev" : "";
44
38
  }
45
- function getCommitDate({ locale, intlOptions }) {
39
+ function getCommitDate() {
46
40
  const output = runCommand('bash -c "git show -s --format=%cd --date=iso-strict|cat"');
47
41
  let date = void 0;
48
42
  try {
49
43
  date = new Date(output);
50
44
  } catch (e) {
51
45
  console.error("Could not convert command output to Date:", output);
52
- return "";
46
+ return void 0;
53
47
  }
54
- return formatDate(date, locale, intlOptions);
48
+ return date;
55
49
  }
56
50
  function getCurrentBranchName() {
57
51
  return runCommand("git rev-parse --abbrev-ref HEAD");
@@ -59,22 +53,20 @@ function getCurrentBranchName() {
59
53
  function getCommitHash(short) {
60
54
  return runCommand(`git rev-parse${short ? " --short" : ""} HEAD`);
61
55
  }
62
- function getVersionString({ commitHashLength, noLicenseSuffix, pattern }, { commitHash, gitBranch }) {
56
+ function createVersionContext({ commitHashLength, noLicenseSuffix }, { commitHash, gitBranch }) {
63
57
  const { version, license } = readPackageJson();
64
- const currentBranch = gitBranch ?? getCurrentBranchName();
65
- const licenseSuffix = license ? "" : noLicenseSuffix;
66
- const slicedCommitHash = sliceCommitHash(
67
- commitHash ?? getCommitHash(commitHashLength < 8),
68
- commitHashLength
69
- );
70
- const branchSuffix = getBranchSuffix();
71
- const context = {
58
+ return {
72
59
  version,
73
- noLicenseSuffix: licenseSuffix,
74
- currentBranch,
75
- commitHash: slicedCommitHash,
76
- branchSuffix
60
+ noLicenseSuffix: license ? "" : noLicenseSuffix,
61
+ branch: gitBranch ?? getCurrentBranchName(),
62
+ commitHash: sliceCommitHash(
63
+ commitHash ?? getCommitHash(commitHashLength < 8),
64
+ commitHashLength
65
+ ),
66
+ suffix: getBranchSuffix()
77
67
  };
68
+ }
69
+ function getVersionString(pattern, context) {
78
70
  return formatVersionString(pattern, context);
79
71
  }
80
72
  function runCommand(command) {
@@ -89,33 +81,30 @@ function sliceCommitHash(hash, length) {
89
81
  return hash.slice(0, length);
90
82
  }
91
83
 
92
- function plugin({
93
- locale,
94
- commitHashLength,
95
- intlOptions,
96
- noLicenseSuffix,
97
- pattern
98
- } = {}) {
84
+ function plugin(versionInfo = {}) {
85
+ const { commitHashLength, noLicenseSuffix, pattern } = versionInfo;
86
+ const versionContext = createVersionContext(
87
+ {
88
+ commitHashLength: commitHashLength ?? 8,
89
+ noLicenseSuffix: noLicenseSuffix ?? "non-free"
90
+ },
91
+ versionInfo
92
+ );
99
93
  return {
100
94
  name: "vite-plugin-version-info",
101
- config(pluginParameters) {
95
+ config(_) {
102
96
  return {
103
97
  define: {
104
- __APP_VERSION__: JSON.stringify(
105
- getVersionString({
106
- commitHashLength: commitHashLength ?? 8,
107
- noLicenseSuffix: noLicenseSuffix ?? "-nonfree",
108
- pattern: pattern ?? "{version}{noLicenseSuffix} {currentBranch}-{commitHash}{branchSuffix}"
109
- }, pluginParameters)
98
+ __VERSION_INFO__: JSON.stringify(
99
+ getVersionString(
100
+ pattern ?? "{version}{suffix}+{branch}.{commitHash} {noLicenseSuffix}",
101
+ versionContext
102
+ )
110
103
  ),
111
- __COMMIT_DATE__: JSON.stringify(
112
- pluginParameters.commitDate ?? getCommitDate({
113
- locale: locale ?? "en-US",
114
- intlOptions: intlOptions ?? {
115
- dateStyle: "long"
116
- }
117
- })
118
- )
104
+ __VERSION_INFO_COMMIT_DATE__: JSON.stringify(getCommitDate()),
105
+ __VERSION_INFO_COMMIT_HASH__: JSON.stringify(versionContext.commitHash),
106
+ __VERSION_INFO_VERSION__: JSON.stringify(versionContext.version),
107
+ __VERSION_INFO_BRANCH__: JSON.stringify(versionContext.branch)
119
108
  }
120
109
  };
121
110
  }
package/dist/index.d.ts CHANGED
@@ -1,22 +1,9 @@
1
- /**
2
- * Options for formatting the Git commit date.
3
- */
4
- type CommitDateOptions = {
5
- /**
6
- * Locale for the formatted date (e.g., 'en-GB', 'en-US').
7
- */
8
- locale?: string;
9
- /**
10
- * Intl.DateTimeFormat options for customizing date output.
11
- */
12
- intlOptions?: Intl.DateTimeFormatOptions;
13
- };
14
1
  /**
15
2
  * Options for customizing the version string.
16
3
  */
17
4
  type VersionStringOptions = {
18
5
  /**
19
- * Suffix to append when no license is present (default: "-nonfree").
6
+ * Suffix to append when no license is present (default: "non-free").
20
7
  */
21
8
  noLicenseSuffix?: string;
22
9
  /**
@@ -31,9 +18,9 @@ type VersionStringOptions = {
31
18
  /**
32
19
  * Combined plugin options for version and commit date customization.
33
20
  */
34
- type VersionInfoPluginOptions = CommitDateOptions & VersionStringOptions;
21
+ type VersionInfoPluginOptions = VersionStringOptions & PrecomputedVersionInfo;
35
22
 
36
- type VersionInfoParameters = {
23
+ type PrecomputedVersionInfo = {
37
24
  commitHash?: string;
38
25
  gitBranch?: string;
39
26
  commitDate?: string;
@@ -53,12 +40,15 @@ type VersionInfoParameters = {
53
40
  * @param options - Plugin options to customize formatting and metadata injection
54
41
  * @returns A Vite-compatible plugin object
55
42
  */
56
- declare function plugin({ locale, commitHashLength, intlOptions, noLicenseSuffix, pattern, }?: VersionInfoPluginOptions): {
43
+ declare function plugin(versionInfo?: VersionInfoPluginOptions): {
57
44
  name: string;
58
- config(pluginParameters: VersionInfoParameters): {
45
+ config(_: PrecomputedVersionInfo): {
59
46
  define: {
60
- __APP_VERSION__: string;
61
- __COMMIT_DATE__: string;
47
+ __VERSION_INFO__: string;
48
+ __VERSION_INFO_COMMIT_DATE__: string;
49
+ __VERSION_INFO_COMMIT_HASH__: string;
50
+ __VERSION_INFO_VERSION__: string;
51
+ __VERSION_INFO_BRANCH__: string;
62
52
  };
63
53
  };
64
54
  };
package/dist/index.mjs CHANGED
@@ -4,12 +4,6 @@ import { join } from 'path';
4
4
  import process from 'process';
5
5
  import { fileURLToPath } from 'url';
6
6
 
7
- function formatDate(date, locale, options = {
8
- dateStyle: "long",
9
- timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone
10
- }) {
11
- return new Intl.DateTimeFormat(locale, options).format(date);
12
- }
13
7
  function formatVersionString(pattern, context) {
14
8
  return pattern.replace(/\{(\w+)\}/g, (_, key) => {
15
9
  return context[key] !== void 0 ? context[key] : `{${key}}`;
@@ -18,16 +12,16 @@ function formatVersionString(pattern, context) {
18
12
  function getBranchSuffix() {
19
13
  return process.env.NODE_ENV?.startsWith("dev") ? "-dev" : "";
20
14
  }
21
- function getCommitDate({ locale, intlOptions }) {
15
+ function getCommitDate() {
22
16
  const output = runCommand('bash -c "git show -s --format=%cd --date=iso-strict|cat"');
23
17
  let date = void 0;
24
18
  try {
25
19
  date = new Date(output);
26
20
  } catch (e) {
27
21
  console.error("Could not convert command output to Date:", output);
28
- return "";
22
+ return void 0;
29
23
  }
30
- return formatDate(date, locale, intlOptions);
24
+ return date;
31
25
  }
32
26
  function getCurrentBranchName() {
33
27
  return runCommand("git rev-parse --abbrev-ref HEAD");
@@ -35,22 +29,20 @@ function getCurrentBranchName() {
35
29
  function getCommitHash(short) {
36
30
  return runCommand(`git rev-parse${short ? " --short" : ""} HEAD`);
37
31
  }
38
- function getVersionString({ commitHashLength, noLicenseSuffix, pattern }, { commitHash, gitBranch }) {
32
+ function createVersionContext({ commitHashLength, noLicenseSuffix }, { commitHash, gitBranch }) {
39
33
  const { version, license } = readPackageJson();
40
- const currentBranch = gitBranch ?? getCurrentBranchName();
41
- const licenseSuffix = license ? "" : noLicenseSuffix;
42
- const slicedCommitHash = sliceCommitHash(
43
- commitHash ?? getCommitHash(commitHashLength < 8),
44
- commitHashLength
45
- );
46
- const branchSuffix = getBranchSuffix();
47
- const context = {
34
+ return {
48
35
  version,
49
- noLicenseSuffix: licenseSuffix,
50
- currentBranch,
51
- commitHash: slicedCommitHash,
52
- branchSuffix
36
+ noLicenseSuffix: license ? "" : noLicenseSuffix,
37
+ branch: gitBranch ?? getCurrentBranchName(),
38
+ commitHash: sliceCommitHash(
39
+ commitHash ?? getCommitHash(commitHashLength < 8),
40
+ commitHashLength
41
+ ),
42
+ suffix: getBranchSuffix()
53
43
  };
44
+ }
45
+ function getVersionString(pattern, context) {
54
46
  return formatVersionString(pattern, context);
55
47
  }
56
48
  function runCommand(command) {
@@ -65,33 +57,30 @@ function sliceCommitHash(hash, length) {
65
57
  return hash.slice(0, length);
66
58
  }
67
59
 
68
- function plugin({
69
- locale,
70
- commitHashLength,
71
- intlOptions,
72
- noLicenseSuffix,
73
- pattern
74
- } = {}) {
60
+ function plugin(versionInfo = {}) {
61
+ const { commitHashLength, noLicenseSuffix, pattern } = versionInfo;
62
+ const versionContext = createVersionContext(
63
+ {
64
+ commitHashLength: commitHashLength ?? 8,
65
+ noLicenseSuffix: noLicenseSuffix ?? "non-free"
66
+ },
67
+ versionInfo
68
+ );
75
69
  return {
76
70
  name: "vite-plugin-version-info",
77
- config(pluginParameters) {
71
+ config(_) {
78
72
  return {
79
73
  define: {
80
- __APP_VERSION__: JSON.stringify(
81
- getVersionString({
82
- commitHashLength: commitHashLength ?? 8,
83
- noLicenseSuffix: noLicenseSuffix ?? "-nonfree",
84
- pattern: pattern ?? "{version}{noLicenseSuffix} {currentBranch}-{commitHash}{branchSuffix}"
85
- }, pluginParameters)
74
+ __VERSION_INFO__: JSON.stringify(
75
+ getVersionString(
76
+ pattern ?? "{version}{suffix}+{branch}.{commitHash} {noLicenseSuffix}",
77
+ versionContext
78
+ )
86
79
  ),
87
- __COMMIT_DATE__: JSON.stringify(
88
- pluginParameters.commitDate ?? getCommitDate({
89
- locale: locale ?? "en-US",
90
- intlOptions: intlOptions ?? {
91
- dateStyle: "long"
92
- }
93
- })
94
- )
80
+ __VERSION_INFO_COMMIT_DATE__: JSON.stringify(getCommitDate()),
81
+ __VERSION_INFO_COMMIT_HASH__: JSON.stringify(versionContext.commitHash),
82
+ __VERSION_INFO_VERSION__: JSON.stringify(versionContext.version),
83
+ __VERSION_INFO_BRANCH__: JSON.stringify(versionContext.branch)
95
84
  }
96
85
  };
97
86
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vite-plugin-version-info",
3
3
  "description": "A Vite plugin that injects software version metadata as build-time globals for easy display in your UI or console.",
4
- "version": "1.1.0",
4
+ "version": "2.0.0",
5
5
  "license": "MIT",
6
6
  "keywords": [
7
7
  "vite",
@@ -25,18 +25,18 @@
25
25
  "readme": "",
26
26
  "type": "module",
27
27
  "peerDependencies": {
28
- "vite": "^2.7.0 || ^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0"
28
+ "vite": "^2.7.0 || ^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0"
29
29
  },
30
30
  "devDependencies": {
31
- "@rollup/plugin-typescript": "^12.1.4",
32
- "@types/node": "^24.0.10",
33
- "prettier": "^3.6.2",
34
- "rollup": "^4.44.2",
35
- "rollup-plugin-dts": "^6.2.1",
31
+ "@rollup/plugin-typescript": "^12.3.0",
32
+ "@types/node": "^25.5.0",
33
+ "prettier": "^3.8.1",
34
+ "rollup": "^4.60.0",
35
+ "rollup-plugin-dts": "^6.4.1",
36
36
  "rollup-plugin-esbuild": "^6.2.1",
37
37
  "tslib": "^2.8.1",
38
- "typescript": "^5.8.3",
39
- "vite": "^6.3.5"
38
+ "typescript": "^5.9.3",
39
+ "vite": "^8.0.1"
40
40
  },
41
41
  "main": "dist/index.cjs",
42
42
  "types": "dist/index.d.ts",