vite-plugin-version-info 1.0.0 → 1.1.1
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 +7 -0
- package/dist/index.cjs +18 -21
- package/dist/index.d.ts +8 -2
- package/dist/index.mjs +18 -21
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -56,6 +56,13 @@ export default defineConfig({
|
|
|
56
56
|
});
|
|
57
57
|
```
|
|
58
58
|
|
|
59
|
+
If using typescript, define the exposed constants in a separate `src/globals.d.ts` file:
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
declare const __APP_VERSION__: string;
|
|
63
|
+
declare const __COMMIT_DATE__: string;
|
|
64
|
+
```
|
|
65
|
+
|
|
59
66
|
Then use the global constants injected by the plugin anywhere in your app:
|
|
60
67
|
|
|
61
68
|
```ts
|
package/dist/index.cjs
CHANGED
|
@@ -59,21 +59,20 @@ function getCurrentBranchName() {
|
|
|
59
59
|
function getCommitHash(short) {
|
|
60
60
|
return runCommand(`git rev-parse${short ? " --short" : ""} HEAD`);
|
|
61
61
|
}
|
|
62
|
-
function getVersionString({
|
|
63
|
-
commitHashLength,
|
|
64
|
-
noLicenseSuffix,
|
|
65
|
-
pattern
|
|
66
|
-
}) {
|
|
62
|
+
function getVersionString({ commitHashLength, noLicenseSuffix, pattern }, { commitHash, gitBranch }) {
|
|
67
63
|
const { version, license } = readPackageJson();
|
|
68
|
-
const currentBranch = getCurrentBranchName();
|
|
64
|
+
const currentBranch = gitBranch ?? getCurrentBranchName();
|
|
69
65
|
const licenseSuffix = license ? "" : noLicenseSuffix;
|
|
70
|
-
const
|
|
66
|
+
const slicedCommitHash = sliceCommitHash(
|
|
67
|
+
commitHash ?? getCommitHash(commitHashLength < 8),
|
|
68
|
+
commitHashLength
|
|
69
|
+
);
|
|
71
70
|
const branchSuffix = getBranchSuffix();
|
|
72
71
|
const context = {
|
|
73
72
|
version,
|
|
74
73
|
noLicenseSuffix: licenseSuffix,
|
|
75
74
|
currentBranch,
|
|
76
|
-
commitHash,
|
|
75
|
+
commitHash: slicedCommitHash,
|
|
77
76
|
branchSuffix
|
|
78
77
|
};
|
|
79
78
|
return formatVersionString(pattern, context);
|
|
@@ -90,27 +89,25 @@ function sliceCommitHash(hash, length) {
|
|
|
90
89
|
return hash.slice(0, length);
|
|
91
90
|
}
|
|
92
91
|
|
|
93
|
-
function plugin({
|
|
94
|
-
locale,
|
|
95
|
-
commitHashLength,
|
|
96
|
-
intlOptions,
|
|
97
|
-
noLicenseSuffix,
|
|
98
|
-
pattern
|
|
99
|
-
} = {}) {
|
|
92
|
+
function plugin(versionInfo = {}) {
|
|
93
|
+
const { locale, commitHashLength, intlOptions, noLicenseSuffix, pattern } = versionInfo;
|
|
100
94
|
return {
|
|
101
95
|
name: "vite-plugin-version-info",
|
|
102
96
|
config() {
|
|
103
97
|
return {
|
|
104
98
|
define: {
|
|
105
99
|
__APP_VERSION__: JSON.stringify(
|
|
106
|
-
getVersionString(
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
100
|
+
getVersionString(
|
|
101
|
+
{
|
|
102
|
+
commitHashLength: commitHashLength ?? 8,
|
|
103
|
+
noLicenseSuffix: noLicenseSuffix ?? "-nonfree",
|
|
104
|
+
pattern: pattern ?? "{version}{noLicenseSuffix} {currentBranch}-{commitHash}{branchSuffix}"
|
|
105
|
+
},
|
|
106
|
+
versionInfo
|
|
107
|
+
)
|
|
111
108
|
),
|
|
112
109
|
__COMMIT_DATE__: JSON.stringify(
|
|
113
|
-
getCommitDate({
|
|
110
|
+
versionInfo.commitDate ?? getCommitDate({
|
|
114
111
|
locale: locale ?? "en-US",
|
|
115
112
|
intlOptions: intlOptions ?? {
|
|
116
113
|
dateStyle: "long"
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
type PrecomputedVersionInfo = {
|
|
2
|
+
commitHash?: string;
|
|
3
|
+
gitBranch?: string;
|
|
4
|
+
commitDate?: string;
|
|
5
|
+
};
|
|
6
|
+
|
|
1
7
|
/**
|
|
2
8
|
* Options for formatting the Git commit date.
|
|
3
9
|
*/
|
|
@@ -31,7 +37,7 @@ type VersionStringOptions = {
|
|
|
31
37
|
/**
|
|
32
38
|
* Combined plugin options for version and commit date customization.
|
|
33
39
|
*/
|
|
34
|
-
type VersionInfoPluginOptions = CommitDateOptions & VersionStringOptions;
|
|
40
|
+
type VersionInfoPluginOptions = CommitDateOptions & VersionStringOptions & PrecomputedVersionInfo;
|
|
35
41
|
|
|
36
42
|
/**
|
|
37
43
|
* Vite plugin that injects Git version info and commit metadata into your frontend app.
|
|
@@ -47,7 +53,7 @@ type VersionInfoPluginOptions = CommitDateOptions & VersionStringOptions;
|
|
|
47
53
|
* @param options - Plugin options to customize formatting and metadata injection
|
|
48
54
|
* @returns A Vite-compatible plugin object
|
|
49
55
|
*/
|
|
50
|
-
declare function plugin(
|
|
56
|
+
declare function plugin(versionInfo?: VersionInfoPluginOptions): {
|
|
51
57
|
name: string;
|
|
52
58
|
config(): {
|
|
53
59
|
define: {
|
package/dist/index.mjs
CHANGED
|
@@ -35,21 +35,20 @@ function getCurrentBranchName() {
|
|
|
35
35
|
function getCommitHash(short) {
|
|
36
36
|
return runCommand(`git rev-parse${short ? " --short" : ""} HEAD`);
|
|
37
37
|
}
|
|
38
|
-
function getVersionString({
|
|
39
|
-
commitHashLength,
|
|
40
|
-
noLicenseSuffix,
|
|
41
|
-
pattern
|
|
42
|
-
}) {
|
|
38
|
+
function getVersionString({ commitHashLength, noLicenseSuffix, pattern }, { commitHash, gitBranch }) {
|
|
43
39
|
const { version, license } = readPackageJson();
|
|
44
|
-
const currentBranch = getCurrentBranchName();
|
|
40
|
+
const currentBranch = gitBranch ?? getCurrentBranchName();
|
|
45
41
|
const licenseSuffix = license ? "" : noLicenseSuffix;
|
|
46
|
-
const
|
|
42
|
+
const slicedCommitHash = sliceCommitHash(
|
|
43
|
+
commitHash ?? getCommitHash(commitHashLength < 8),
|
|
44
|
+
commitHashLength
|
|
45
|
+
);
|
|
47
46
|
const branchSuffix = getBranchSuffix();
|
|
48
47
|
const context = {
|
|
49
48
|
version,
|
|
50
49
|
noLicenseSuffix: licenseSuffix,
|
|
51
50
|
currentBranch,
|
|
52
|
-
commitHash,
|
|
51
|
+
commitHash: slicedCommitHash,
|
|
53
52
|
branchSuffix
|
|
54
53
|
};
|
|
55
54
|
return formatVersionString(pattern, context);
|
|
@@ -66,27 +65,25 @@ function sliceCommitHash(hash, length) {
|
|
|
66
65
|
return hash.slice(0, length);
|
|
67
66
|
}
|
|
68
67
|
|
|
69
|
-
function plugin({
|
|
70
|
-
locale,
|
|
71
|
-
commitHashLength,
|
|
72
|
-
intlOptions,
|
|
73
|
-
noLicenseSuffix,
|
|
74
|
-
pattern
|
|
75
|
-
} = {}) {
|
|
68
|
+
function plugin(versionInfo = {}) {
|
|
69
|
+
const { locale, commitHashLength, intlOptions, noLicenseSuffix, pattern } = versionInfo;
|
|
76
70
|
return {
|
|
77
71
|
name: "vite-plugin-version-info",
|
|
78
72
|
config() {
|
|
79
73
|
return {
|
|
80
74
|
define: {
|
|
81
75
|
__APP_VERSION__: JSON.stringify(
|
|
82
|
-
getVersionString(
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
76
|
+
getVersionString(
|
|
77
|
+
{
|
|
78
|
+
commitHashLength: commitHashLength ?? 8,
|
|
79
|
+
noLicenseSuffix: noLicenseSuffix ?? "-nonfree",
|
|
80
|
+
pattern: pattern ?? "{version}{noLicenseSuffix} {currentBranch}-{commitHash}{branchSuffix}"
|
|
81
|
+
},
|
|
82
|
+
versionInfo
|
|
83
|
+
)
|
|
87
84
|
),
|
|
88
85
|
__COMMIT_DATE__: JSON.stringify(
|
|
89
|
-
getCommitDate({
|
|
86
|
+
versionInfo.commitDate ?? getCommitDate({
|
|
90
87
|
locale: locale ?? "en-US",
|
|
91
88
|
intlOptions: intlOptions ?? {
|
|
92
89
|
dateStyle: "long"
|
package/package.json
CHANGED