unplugin-auto-git-log 0.0.1 โ†’ 1.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
@@ -9,11 +9,10 @@ Unplugin for automatically generating Git information (repo, branch, commit, etc
9
9
  - ๐Ÿ“ฆ Automatically extract Git repository information
10
10
  - ๐ŸŽฏ Support multiple output formats:
11
11
  - JSON file
12
- - Window global variable
13
- - Environment variables (.env)
14
- - TypeScript type definitions
12
+ - Window global variable (via define replacement and HTML injection)
15
13
  - ๐Ÿ”ง Works with all major build tools (Vite, Webpack, Rollup, esbuild, Rspack, etc.)
16
14
  - โš™๏ธ Configurable fields and output options
15
+ - ๐Ÿš€ Compile-time injection for optimal performance
17
16
 
18
17
  ## Installation
19
18
 
@@ -119,7 +118,7 @@ By default, the plugin will:
119
118
  - Extract all available Git fields (repo, branch, commit, commitShort, author, authorEmail, commitTime, commitMessage, isDirty)
120
119
  - Generate a JSON file at your build output directory (e.g., `dist/git-log.json` for Vite)
121
120
  - Automatically detect the output directory from your build tool configuration
122
- - Run after build (`enforce: 'post'`)
121
+ - Run after build completion
123
122
 
124
123
  You can use the plugin without any configuration:
125
124
 
@@ -149,29 +148,16 @@ AutoGitLog({
149
148
  fileName: 'git-log.json', // Relative to build output directory
150
149
  },
151
150
 
152
- // Generate window global variable file (default: '__GIT_LOG__')
151
+ // Generate window global variable (default: '__GIT_LOG__')
152
+ // Uses define replacement for compile-time injection
153
153
  window: {
154
- varName: '__GIT_LOG__',
155
- console: true, // Log Git log to browser console
156
- },
157
-
158
- // Generate environment variables file (default: '.env.git')
159
- env: {
160
- prefix: '__GIT_',
161
- file: '.env.git',
162
- },
163
-
164
- // Generate TypeScript type definitions (default: 'git-log.d.ts' in output directory)
165
- types: {
166
- fileName: 'git-log.d.ts', // Relative to build output directory
154
+ varName: '__GIT_LOG__', // Global variable name
155
+ console: true, // Log Git log to browser console (default: false)
167
156
  },
168
157
  },
169
158
 
170
159
  // Working directory (optional, defaults to process.cwd())
171
160
  // cwd: './custom-path',
172
-
173
- // Plugin execution timing
174
- enforce: 'post', // 'pre' | 'post'
175
161
  })
176
162
  ```
177
163
 
@@ -204,15 +190,18 @@ AutoGitLog({
204
190
 
205
191
  The following Git information can be extracted:
206
192
 
207
- - `repo` - Repository URL
208
- - `branch` - Current branch name
193
+ - `repo` - Repository name (extracted from remote URL or directory name)
194
+ - `branch` - Current branch name (handles detached HEAD state)
209
195
  - `commit` - Full commit hash
210
196
  - `commitShort` - Short commit hash (7 characters)
211
197
  - `author` - Author name
212
198
  - `authorEmail` - Author email
213
- - `commitTime` - Commit timestamp
214
- - `commitMessage` - Commit message
199
+ - `commitTime` - Commit timestamp (ISO 8601 format)
200
+ - `commitMessage` - Commit message (first line, newlines removed)
201
+ - `tag` - Current tag if HEAD points to a tag
215
202
  - `isDirty` - Whether the working directory has uncommitted changes
203
+ - `remoteUrl` - Remote repository URL (e.g., `https://github.com/user/repo.git`)
204
+ - `root` - Git repository root directory path
216
205
 
217
206
  ## Output Examples
218
207
 
@@ -238,8 +227,8 @@ By default, the JSON file is generated at your build output directory (e.g., `di
238
227
 
239
228
  When window output is enabled, the plugin will:
240
229
 
241
- 1. Generate a JavaScript file (e.g., `dist/__GIT_LOG__.js`)
242
- 2. Automatically inject a `<script>` tag into your HTML (Vite only)
230
+ 1. **For Vite**: Automatically inject a `<script>` tag into your HTML `<head>` section
231
+ 2. **For other build tools**: Use define replacement to inject the variable at compile time
243
232
  3. Optionally log Git log to browser console (with `console: true`)
244
233
 
245
234
  You can then access the Git log anywhere in your code:
@@ -251,29 +240,23 @@ console.log(window.__GIT_LOG__.branch)
251
240
  console.log(window.__GIT_LOG__.commit)
252
241
  ```
253
242
 
254
- The generated file (`__GIT_LOG__.js`) contains:
243
+ **How it works:**
255
244
 
256
- ```js
257
- ;(function () {
245
+ - **Vite**: The plugin injects a `<script>` tag in the HTML that sets `window.__GIT_LOG__` before your code runs
246
+ - **Other build tools**: Uses define replacement - code references to `window.__GIT_LOG__` are replaced with the actual Git log object at compile time
247
+
248
+ **Example HTML injection (Vite):**
249
+
250
+ ```html
251
+ <script>
258
252
  if (typeof window !== 'undefined') {
259
- window.__GIT_LOG__ = { /* git log */ }
253
+ window.__GIT_LOG__ = {"repo":"...","branch":"main",...};
254
+ // console.log('[Git Log]', window.__GIT_LOG__); // if console: true
260
255
  }
261
- })()
256
+ </script>
262
257
  ```
263
258
 
264
- ### Environment Variables (`.env.git`)
265
-
266
- ```bash
267
- __GIT_REPO=https://github.com/user/repo.git
268
- __GIT_BRANCH=main
269
- __GIT_COMMIT=abc123def456...
270
- __GIT_COMMIT_SHORT=abc123d
271
- __GIT_AUTHOR=John Doe
272
- __GIT_AUTHOR_EMAIL=john@example.com
273
- __GIT_COMMIT_TIME=2025-01-08T12:00:00.000Z
274
- __GIT_COMMIT_MESSAGE=feat: add new feature
275
- __GIT_IS_DIRTY=false
276
- ```
259
+ **Note**: For non-Vite build tools, you need to reference `window.__GIT_LOG__` in your code for the define replacement to work.
277
260
 
278
261
  ## License
279
262
 
package/dist/api.d.mts CHANGED
@@ -1,2 +1,2 @@
1
- import { a as OutputOptions, c as generateEnvVars, d as generateTypeDefinitions, f as generateWindowVar, g as getGitLog, h as getAvailableFields, i as JsonOutputOptions, l as generateJson, m as GitLog, n as OptionsResolved, o as TypesOutputOptions, p as GitField, r as EnvOutputOptions, s as WindowOutputOptions, t as Options, u as generateOutputs } from "./options-B7zpW-6U.mjs";
2
- export { type EnvOutputOptions, type GitField, type GitLog, type JsonOutputOptions, type Options, type OptionsResolved, type OutputOptions, type TypesOutputOptions, type WindowOutputOptions, generateEnvVars, generateJson, generateOutputs, generateTypeDefinitions, generateWindowVar, getAvailableFields, getGitLog };
1
+ import { a as WindowOutputOptions, c as GitField, d as getGitLog, i as OutputOptions, l as GitLog, n as OptionsResolved, o as generateJson, r as JsonOutputOptions, s as generateOutputs, t as Options, u as getAvailableFields } from "./options-g2PItbEC.mjs";
2
+ export { type GitField, type GitLog, type JsonOutputOptions, type Options, type OptionsResolved, type OutputOptions, type WindowOutputOptions, generateJson, generateOutputs, getAvailableFields, getGitLog };
package/dist/api.mjs CHANGED
@@ -1,3 +1,3 @@
1
- import { a as generateWindowVar, i as generateTypeDefinitions, n as generateJson, o as getAvailableFields, r as generateOutputs, s as getGitLog, t as generateEnvVars } from "./outputs-CbT7SkvQ.mjs";
1
+ import { i as getGitLog, n as generateOutputs, r as getAvailableFields, t as generateJson } from "./outputs-Brlh_kpb.mjs";
2
2
 
3
- export { generateEnvVars, generateJson, generateOutputs, generateTypeDefinitions, generateWindowVar, getAvailableFields, getGitLog };
3
+ export { generateJson, generateOutputs, getAvailableFields, getGitLog };
package/dist/cli.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import { r as generateOutputs, s as getGitLog } from "./outputs-CbT7SkvQ.mjs";
2
+ import { i as getGitLog, n as generateOutputs } from "./outputs-Brlh_kpb.mjs";
3
3
  import { readFileSync } from "node:fs";
4
4
  import { resolve } from "node:path";
5
5
  import process from "node:process";
@@ -1,4 +1,3 @@
1
- import "./options-B7zpW-6U.mjs";
2
1
  import { AutoGitLog } from "./index.mjs";
3
2
 
4
3
  //#region src/esbuild.d.ts
package/dist/esbuild.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { t as AutoGitLog } from "./src-Bbcs1xDg.mjs";
1
+ import { t as AutoGitLog } from "./src-C7eWkLP7.mjs";
2
2
 
3
3
  //#region src/esbuild.ts
4
4
  /**
package/dist/farm.d.mts CHANGED
@@ -1,4 +1,3 @@
1
- import "./options-B7zpW-6U.mjs";
2
1
  import { AutoGitLog } from "./index.mjs";
3
2
 
4
3
  //#region src/farm.d.ts
package/dist/farm.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { t as AutoGitLog } from "./src-Bbcs1xDg.mjs";
1
+ import { t as AutoGitLog } from "./src-C7eWkLP7.mjs";
2
2
 
3
3
  //#region src/farm.ts
4
4
  /**
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { t as Options } from "./options-B7zpW-6U.mjs";
1
+ import { t as Options } from "./options-g2PItbEC.mjs";
2
2
  import { UnpluginInstance } from "unplugin";
3
3
 
4
4
  //#region src/index.d.ts
package/dist/index.mjs CHANGED
@@ -1,3 +1,3 @@
1
- import { t as AutoGitLog } from "./src-Bbcs1xDg.mjs";
1
+ import { t as AutoGitLog } from "./src-C7eWkLP7.mjs";
2
2
 
3
3
  export { AutoGitLog };
@@ -0,0 +1,70 @@
1
+ //#region src/core/git.d.ts
2
+ type GitField = "repo" | "branch" | "commit" | "commitShort" | "author" | "authorEmail" | "commitTime" | "commitMessage" | "tag" | "isDirty" | "remoteUrl" | "root";
3
+ type GitLog = Record<string, string | boolean>;
4
+ /**
5
+ * ่Žทๅ– Git ๆ—ฅๅฟ—ไฟกๆฏ
6
+ * @param fields ้œ€่ฆๆๅ–็š„ Git ๅญ—ๆฎตๅˆ—่กจ
7
+ * @param cwd ๅทฅไฝœ็›ฎๅฝ•๏ผŒ้ป˜่ฎคไธบๅฝ“ๅ‰็›ฎๅฝ•
8
+ * @returns Git ๆ—ฅๅฟ—ๅฏน่ฑก๏ผŒๅŒ…ๅซๆ‰€ๆœ‰่ฏทๆฑ‚็š„ๅญ—ๆฎต
9
+ */
10
+ declare function getGitLog(fields?: string[], cwd?: string): GitLog;
11
+ /**
12
+ * ่Žทๅ–ๆ‰€ๆœ‰ๅฏ็”จ็š„ Git ๅญ—ๆฎต
13
+ */
14
+ declare function getAvailableFields(): GitField[];
15
+ //#endregion
16
+ //#region src/core/outputs.d.ts
17
+ interface JsonOutputOptions {
18
+ fileName?: string;
19
+ }
20
+ /**
21
+ * Window ๅ…จๅฑ€ๅ˜้‡่พ“ๅ‡บ้€‰้กน
22
+ * ้€š่ฟ‡ define ๆ›ฟๆข๏ผˆ็ผ–่ฏ‘ๆ—ถ๏ผ‰ๅ’Œ HTML ๆณจๅ…ฅ๏ผˆVite๏ผ‰ๅฎž็Žฐ
23
+ */
24
+ interface WindowOutputOptions {
25
+ /** ๅ…จๅฑ€ๅ˜้‡ๅ็งฐ๏ผŒ้ป˜่ฎคไธบ '__GIT_LOG__' */
26
+ varName?: string;
27
+ /** ๆ˜ฏๅฆๅœจๆต่งˆๅ™จๆŽงๅˆถๅฐๆ‰“ๅฐ Git ๆ—ฅๅฟ—๏ผŒ้ป˜่ฎค๏ผšfalse */
28
+ console?: boolean;
29
+ }
30
+ interface OutputOptions {
31
+ json?: JsonOutputOptions;
32
+ window?: WindowOutputOptions;
33
+ }
34
+ /**
35
+ * ็”Ÿๆˆ JSON ๆ–‡ไปถๅˆฐ่พ“ๅ‡บ็›ฎๅฝ•
36
+ * @param gitLog Git ๆ—ฅๅฟ—ๅฏน่ฑก
37
+ * @param options JSON ่พ“ๅ‡บ้€‰้กน
38
+ * @param outputDir ่พ“ๅ‡บ็›ฎๅฝ•่ทฏๅพ„
39
+ */
40
+ declare function generateJson(gitLog: GitLog, options?: JsonOutputOptions, outputDir?: string): void;
41
+ /**
42
+ * ๆ นๆฎ้…็ฝฎ็”Ÿๆˆๆ‰€ๆœ‰่พ“ๅ‡บๆ–‡ไปถ
43
+ * ็›ฎๅ‰ๆ”ฏๆŒ JSON ๆ–‡ไปถ่พ“ๅ‡บ
44
+ * Window ๅ…จๅฑ€ๅ˜้‡้€š่ฟ‡ define ๆ›ฟๆขๅ’Œ HTML ๆณจๅ…ฅๅฎž็Žฐ๏ผŒไธ็”Ÿๆˆๆ–‡ไปถ
45
+ *
46
+ * @param gitLog Git ๆ—ฅๅฟ—ๅฏน่ฑก
47
+ * @param outputOptions ่พ“ๅ‡บ้€‰้กน้…็ฝฎ
48
+ * @param outputDir ่พ“ๅ‡บ็›ฎๅฝ•่ทฏๅพ„
49
+ */
50
+ declare function generateOutputs(gitLog: GitLog, outputOptions: OutputOptions, outputDir?: string): void;
51
+ //#endregion
52
+ //#region src/core/options.d.ts
53
+ /**
54
+ * ๆ’ไปถ้…็ฝฎ้€‰้กน
55
+ */
56
+ interface Options {
57
+ /** ๆ˜ฏๅฆๅฏ็”จๆ’ไปถ๏ผŒ้ป˜่ฎค๏ผštrue */
58
+ enable?: boolean;
59
+ /** ้œ€่ฆๆๅ–็š„ Git ๅญ—ๆฎตๅˆ—่กจ๏ผŒ้ป˜่ฎค๏ผšๆ‰€ๆœ‰ๅฏ็”จๅญ—ๆฎต */
60
+ fields?: GitField[] | string[];
61
+ /** ่พ“ๅ‡บ้€‰้กน้…็ฝฎ */
62
+ outputs?: OutputOptions;
63
+ /** ๅทฅไฝœ็›ฎๅฝ•๏ผŒ้ป˜่ฎคไธบๅฝ“ๅ‰็›ฎๅฝ• */
64
+ cwd?: string;
65
+ }
66
+ type OptionsResolved = Omit<Pick<Options, "enable" | "fields" | "outputs" | "cwd">, "outputs"> & {
67
+ outputs: OutputOptions;
68
+ };
69
+ //#endregion
70
+ export { WindowOutputOptions as a, GitField as c, getGitLog as d, OutputOptions as i, GitLog as l, OptionsResolved as n, generateJson as o, JsonOutputOptions as r, generateOutputs as s, Options as t, getAvailableFields as u };
@@ -27,7 +27,8 @@ function execGitCommand(command, cwd) {
27
27
  encoding: "utf8",
28
28
  stdio: "pipe"
29
29
  }).trim();
30
- } catch {
30
+ } catch (error) {
31
+ console.warn(`[unplugin-auto-git-log] Git command failed: ${command}`, error);
31
32
  return "";
32
33
  }
33
34
  }
@@ -38,11 +39,17 @@ function getGitRoot(cwd) {
38
39
  return execGitCommand("git rev-parse --show-toplevel", cwd);
39
40
  }
40
41
  /**
41
- * ่Žทๅ– Git ๆ—ฅๅฟ—
42
+ * ่Žทๅ– Git ๆ—ฅๅฟ—ไฟกๆฏ
43
+ * @param fields ้œ€่ฆๆๅ–็š„ Git ๅญ—ๆฎตๅˆ—่กจ
44
+ * @param cwd ๅทฅไฝœ็›ฎๅฝ•๏ผŒ้ป˜่ฎคไธบๅฝ“ๅ‰็›ฎๅฝ•
45
+ * @returns Git ๆ—ฅๅฟ—ๅฏน่ฑก๏ผŒๅŒ…ๅซๆ‰€ๆœ‰่ฏทๆฑ‚็š„ๅญ—ๆฎต
42
46
  */
43
47
  function getGitLog(fields = [], cwd) {
44
48
  const result = {};
45
- if (!isGitRepository(cwd)) return result;
49
+ if (!isGitRepository(cwd)) {
50
+ console.warn("[unplugin-auto-git-log] Not a Git repository or cannot access .git directory");
51
+ return result;
52
+ }
46
53
  const gitRoot = getGitRoot(cwd);
47
54
  for (const field of fields) switch (field) {
48
55
  case "repo": {
@@ -53,9 +60,15 @@ function getGitLog(fields = [], cwd) {
53
60
  } else result.repo = gitRoot ? basename(gitRoot) : "";
54
61
  break;
55
62
  }
56
- case "branch":
57
- result.branch = execGitCommand("git rev-parse --abbrev-ref HEAD", cwd) || "";
63
+ case "branch": {
64
+ const branch = execGitCommand("git rev-parse --abbrev-ref HEAD", cwd);
65
+ if (branch === "HEAD") {
66
+ const tag = execGitCommand("git describe --tags --exact-match HEAD 2>/dev/null", cwd);
67
+ if (tag) result.branch = tag;
68
+ else result.branch = execGitCommand("git rev-parse --short HEAD", cwd) || "HEAD";
69
+ } else result.branch = branch || "";
58
70
  break;
71
+ }
59
72
  case "commit":
60
73
  result.commit = execGitCommand("git rev-parse HEAD", cwd) || "";
61
74
  break;
@@ -69,10 +82,10 @@ function getGitLog(fields = [], cwd) {
69
82
  result.authorEmail = execGitCommand("git log -1 --pretty=format:\"%ae\"", cwd) || "";
70
83
  break;
71
84
  case "commitTime":
72
- result.commitTime = execGitCommand("git log -1 --pretty=format:\"%ci\"", cwd) || "";
85
+ result.commitTime = execGitCommand("git log -1 --pretty=format:\"%cI\"", cwd) || "";
73
86
  break;
74
87
  case "commitMessage":
75
- result.commitMessage = execGitCommand("git log -1 --pretty=format:\"%s\"", cwd) || "";
88
+ result.commitMessage = execGitCommand("git log -1 --pretty=format:\"%s\"", cwd).replace(/\n/g, " ").trim() || "";
76
89
  break;
77
90
  case "tag":
78
91
  result.tag = execGitCommand("git describe --tags --exact-match HEAD 2>/dev/null", cwd) || "";
@@ -116,12 +129,17 @@ function getAvailableFields() {
116
129
  //#region src/core/outputs.ts
117
130
  /**
118
131
  * ็”Ÿๆˆ JSON ๆ–‡ไปถๅ†…ๅฎน
132
+ * @param gitLog Git ๆ—ฅๅฟ—ๅฏน่ฑก
133
+ * @returns ๆ ผๅผๅŒ–็š„ JSON ๅญ—็ฌฆไธฒ
119
134
  */
120
135
  function generateJsonContent(gitLog) {
121
136
  return JSON.stringify(gitLog, null, 2);
122
137
  }
123
138
  /**
124
- * ็”Ÿๆˆ JSON ๆ–‡ไปถ
139
+ * ็”Ÿๆˆ JSON ๆ–‡ไปถๅˆฐ่พ“ๅ‡บ็›ฎๅฝ•
140
+ * @param gitLog Git ๆ—ฅๅฟ—ๅฏน่ฑก
141
+ * @param options JSON ่พ“ๅ‡บ้€‰้กน
142
+ * @param outputDir ่พ“ๅ‡บ็›ฎๅฝ•่ทฏๅพ„
125
143
  */
126
144
  function generateJson(gitLog, options = {}, outputDir) {
127
145
  const fileName = options.fileName || "git-log.json";
@@ -130,80 +148,17 @@ function generateJson(gitLog, options = {}, outputDir) {
130
148
  writeFileSync(fullPath, generateJsonContent(gitLog), "utf8");
131
149
  }
132
150
  /**
133
- * ็”Ÿๆˆ window ๅ…จๅฑ€ๅ˜้‡ไปฃ็ 
134
- */
135
- function generateWindowVarContent(gitLog, options = {}) {
136
- const varName = options.varName || "__GIT_LOG__";
137
- return `(function() {
138
- if (typeof window !== 'undefined') {
139
- window.${varName} = ${JSON.stringify(gitLog, null, 2)};
140
- ${options.console ? `console.log('[Git Log]', window.${varName});` : ""}
141
- }
142
- })();`;
143
- }
144
- /**
145
- * ็”Ÿๆˆ window ๅ…จๅฑ€ๅ˜้‡ๆ–‡ไปถ
146
- */
147
- function generateWindowVar(gitLog, options = {}, outputDir) {
148
- const fileName = `${options.varName || "__GIT_LOG__"}.js`;
149
- const fullPath = outputDir ? resolve(outputDir, fileName) : fileName;
150
- mkdirSync(dirname(fullPath), { recursive: true });
151
- writeFileSync(fullPath, generateWindowVarContent(gitLog, options), "utf8");
152
- return fileName;
153
- }
154
- /**
155
- * ็”Ÿๆˆ็Žฏๅขƒๅ˜้‡ๆ–‡ไปถๅ†…ๅฎน
156
- */
157
- function generateEnvVarsContent(gitLog, options = {}) {
158
- const prefix = options.prefix || "__GIT_";
159
- const lines = [];
160
- for (const [key, value] of Object.entries(gitLog)) {
161
- const envKey = `${prefix}${key.toUpperCase()}`;
162
- const escapedValue = (typeof value === "string" ? value : String(value)).replaceAll("\"", String.raw`\"`);
163
- lines.push(`${envKey}="${escapedValue}"`);
164
- }
165
- return lines.join("\n");
166
- }
167
- /**
168
- * ็”Ÿๆˆ็Žฏๅขƒๅ˜้‡ๆ–‡ไปถ
169
- */
170
- function generateEnvVars(gitLog, options = {}, outputDir) {
171
- const filePath = options.file || ".env.git";
172
- const fullPath = outputDir ? resolve(outputDir, filePath) : filePath;
173
- mkdirSync(dirname(fullPath), { recursive: true });
174
- writeFileSync(fullPath, generateEnvVarsContent(gitLog, options), "utf8");
175
- }
176
- /**
177
- * ็”Ÿๆˆ TypeScript ็ฑปๅž‹ๅฎšไน‰ๆ–‡ไปถๅ†…ๅฎน
178
- */
179
- function generateTypeDefinitionsContent(gitLog) {
180
- return `export interface GitLog {
181
- ${Object.entries(gitLog).map(([key, value]) => {
182
- return ` ${key}: ${typeof value === "boolean" ? "boolean" : "string"}`;
183
- }).join("\n")}
184
- }
185
- `;
186
- }
187
- /**
188
- * ็”Ÿๆˆ TypeScript ็ฑปๅž‹ๅฎšไน‰ๆ–‡ไปถ
189
- */
190
- function generateTypeDefinitions(gitLog, options = {}, outputDir) {
191
- const fileName = options.fileName || "git-log.d.ts";
192
- const fullPath = outputDir && !isAbsolute(fileName) ? resolve(outputDir, fileName) : fileName;
193
- mkdirSync(dirname(fullPath), { recursive: true });
194
- writeFileSync(fullPath, generateTypeDefinitionsContent(gitLog), "utf8");
195
- }
196
- /**
197
- * ๆ นๆฎ้…็ฝฎ็”Ÿๆˆๆ‰€ๆœ‰่พ“ๅ‡บ
151
+ * ๆ นๆฎ้…็ฝฎ็”Ÿๆˆๆ‰€ๆœ‰่พ“ๅ‡บๆ–‡ไปถ
152
+ * ็›ฎๅ‰ๆ”ฏๆŒ JSON ๆ–‡ไปถ่พ“ๅ‡บ
153
+ * Window ๅ…จๅฑ€ๅ˜้‡้€š่ฟ‡ define ๆ›ฟๆขๅ’Œ HTML ๆณจๅ…ฅๅฎž็Žฐ๏ผŒไธ็”Ÿๆˆๆ–‡ไปถ
154
+ *
155
+ * @param gitLog Git ๆ—ฅๅฟ—ๅฏน่ฑก
156
+ * @param outputOptions ่พ“ๅ‡บ้€‰้กน้…็ฝฎ
157
+ * @param outputDir ่พ“ๅ‡บ็›ฎๅฝ•่ทฏๅพ„
198
158
  */
199
159
  function generateOutputs(gitLog, outputOptions, outputDir) {
200
- let windowVarFileName;
201
160
  if (outputOptions.json) generateJson(gitLog, outputOptions.json, outputDir);
202
- if (outputOptions.window) windowVarFileName = generateWindowVar(gitLog, outputOptions.window, outputDir);
203
- if (outputOptions.env) generateEnvVars(gitLog, outputOptions.env, outputDir);
204
- if (outputOptions.types) generateTypeDefinitions(gitLog, outputOptions.types, outputDir);
205
- return windowVarFileName;
206
161
  }
207
162
 
208
163
  //#endregion
209
- export { generateWindowVar as a, generateTypeDefinitions as i, generateJson as n, getAvailableFields as o, generateOutputs as r, getGitLog as s, generateEnvVars as t };
164
+ export { getGitLog as i, generateOutputs as n, getAvailableFields as r, generateJson as t };
@@ -1,4 +1,3 @@
1
- import "./options-B7zpW-6U.mjs";
2
1
  import { AutoGitLog } from "./index.mjs";
3
2
 
4
3
  //#region src/rolldown.d.ts
package/dist/rolldown.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { t as AutoGitLog } from "./src-Bbcs1xDg.mjs";
1
+ import { t as AutoGitLog } from "./src-C7eWkLP7.mjs";
2
2
 
3
3
  //#region src/rolldown.ts
4
4
  /**
package/dist/rollup.d.mts CHANGED
@@ -1,4 +1,3 @@
1
- import "./options-B7zpW-6U.mjs";
2
1
  import { AutoGitLog } from "./index.mjs";
3
2
 
4
3
  //#region src/rollup.d.ts
package/dist/rollup.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { t as AutoGitLog } from "./src-Bbcs1xDg.mjs";
1
+ import { t as AutoGitLog } from "./src-C7eWkLP7.mjs";
2
2
 
3
3
  //#region src/rollup.ts
4
4
  /**
package/dist/rspack.d.mts CHANGED
@@ -1,4 +1,3 @@
1
- import "./options-B7zpW-6U.mjs";
2
1
  import { AutoGitLog } from "./index.mjs";
3
2
 
4
3
  //#region src/rspack.d.ts
package/dist/rspack.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { t as AutoGitLog } from "./src-Bbcs1xDg.mjs";
1
+ import { t as AutoGitLog } from "./src-C7eWkLP7.mjs";
2
2
 
3
3
  //#region src/rspack.ts
4
4
  /**
@@ -0,0 +1,103 @@
1
+ import { i as getGitLog, n as generateOutputs } from "./outputs-Brlh_kpb.mjs";
2
+ import { resolve } from "node:path";
3
+ import process from "node:process";
4
+ import { createUnplugin } from "unplugin";
5
+
6
+ //#region src/core/options.ts
7
+ /**
8
+ * ้ป˜่ฎคๆๅ–็š„ Git ๅญ—ๆฎตๅˆ—่กจ
9
+ */
10
+ const DEFAULT_FIELDS = [
11
+ "repo",
12
+ "branch",
13
+ "commit",
14
+ "commitShort",
15
+ "author",
16
+ "authorEmail",
17
+ "commitTime",
18
+ "commitMessage",
19
+ "isDirty"
20
+ ];
21
+ /**
22
+ * ่งฃๆžๅนถ่ง„่ŒƒๅŒ–ๆ’ไปถ้€‰้กน
23
+ * @param options ็”จๆˆทๆไพ›็š„้€‰้กน
24
+ * @returns ่งฃๆžๅŽ็š„้€‰้กนๅฏน่ฑก
25
+ */
26
+ function resolveOptions(options = {}) {
27
+ return {
28
+ enable: options.enable !== void 0 ? options.enable : true,
29
+ fields: options.fields || DEFAULT_FIELDS,
30
+ outputs: options.outputs || {},
31
+ cwd: options.cwd
32
+ };
33
+ }
34
+
35
+ //#endregion
36
+ //#region src/index.ts
37
+ /**
38
+ * ไปŽๆž„ๅปบๅทฅๅ…ท้…็ฝฎไธญ่Žทๅ–่พ“ๅ‡บ็›ฎๅฝ•
39
+ * ๆ”ฏๆŒ Viteใ€Webpackใ€Rollup/Rolldown ็ญ‰ๆž„ๅปบๅทฅๅ…ท
40
+ */
41
+ function getOutputDir(config, cwd) {
42
+ const projectRoot = cwd || process.cwd();
43
+ if ("vite" in config || config.vite) {
44
+ const outDir = (config.vite || config).build?.outDir;
45
+ if (outDir) return resolve(projectRoot, outDir);
46
+ }
47
+ if ("webpack" in config || config.output?.path) {
48
+ const outputPath = config.output?.path;
49
+ if (outputPath) return resolve(projectRoot, outputPath);
50
+ }
51
+ if (config.output?.dir) {
52
+ const outputDir = config.output.dir;
53
+ return resolve(projectRoot, outputDir);
54
+ }
55
+ return resolve(projectRoot, "dist");
56
+ }
57
+ /**
58
+ * ๆ นๆฎ้…็ฝฎ็”Ÿๆˆ Git ๆ—ฅๅฟ—่พ“ๅ‡บๆ–‡ไปถ
59
+ * ็›ฎๅ‰ๆ”ฏๆŒ JSON ๆ–‡ไปถ่พ“ๅ‡บ
60
+ */
61
+ function generateGitLogFiles(options, outputDir) {
62
+ if (!options.enable) return;
63
+ const gitLog = getGitLog(options.fields, options.cwd);
64
+ if (options.outputs && (options.outputs.json || options.outputs.window)) generateOutputs(gitLog, options.outputs, outputDir);
65
+ else generateOutputs(gitLog, { json: {} }, outputDir);
66
+ }
67
+ const AutoGitLog = createUnplugin((rawOptions = {}) => {
68
+ const options = resolveOptions(rawOptions);
69
+ let outputDir;
70
+ let gitLog = null;
71
+ const varName = options.outputs?.window?.varName || "__GIT_LOG__";
72
+ const shouldConsole = options.enable && options.outputs?.window?.console === true;
73
+ const name = "unplugin-auto-git-log";
74
+ if (options.enable && options.outputs?.window) gitLog = getGitLog(options.fields, options.cwd);
75
+ return {
76
+ name,
77
+ enforce: "post",
78
+ define: options.enable && options.outputs?.window && gitLog ? { [`window.${varName}`]: JSON.stringify(gitLog) } : {},
79
+ vite: { transformIndexHtml(html) {
80
+ if (!options.enable || !options.outputs?.window || !gitLog) return html;
81
+ const scriptCode = `<script>
82
+ if (typeof window !== 'undefined') {
83
+ window.${varName} = ${JSON.stringify(gitLog)};${shouldConsole ? `\n console.log('[Git Log]', window.${varName});` : ""}
84
+ }
85
+ <\/script>`;
86
+ return html.replace("</head>", ` ${scriptCode}\n</head>`);
87
+ } },
88
+ configResolved(config) {
89
+ outputDir = getOutputDir(config, options.cwd);
90
+ },
91
+ buildEnd() {
92
+ if (!options.enable) return;
93
+ generateGitLogFiles(options, outputDir || "dist");
94
+ },
95
+ writeBundle() {
96
+ if (!options.enable) return;
97
+ generateGitLogFiles(options, outputDir || "dist");
98
+ }
99
+ };
100
+ });
101
+
102
+ //#endregion
103
+ export { AutoGitLog as t };
package/dist/vite.d.mts CHANGED
@@ -1,4 +1,3 @@
1
- import "./options-B7zpW-6U.mjs";
2
1
  import { AutoGitLog } from "./index.mjs";
3
2
 
4
3
  //#region src/vite.d.ts
package/dist/vite.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { t as AutoGitLog } from "./src-Bbcs1xDg.mjs";
1
+ import { t as AutoGitLog } from "./src-C7eWkLP7.mjs";
2
2
 
3
3
  //#region src/vite.ts
4
4
  /**
@@ -1,4 +1,3 @@
1
- import "./options-B7zpW-6U.mjs";
2
1
  import { AutoGitLog } from "./index.mjs";
3
2
 
4
3
  //#region src/webpack.d.ts
package/dist/webpack.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { t as AutoGitLog } from "./src-Bbcs1xDg.mjs";
1
+ import { t as AutoGitLog } from "./src-C7eWkLP7.mjs";
2
2
 
3
3
  //#region src/webpack.ts
4
4
  /**
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "unplugin-auto-git-log",
3
3
  "type": "module",
4
- "version": "0.0.1",
4
+ "version": "1.0.0",
5
5
  "packageManager": "pnpm@10.27.0",
6
6
  "description": "Unplugin for automatically generating Git information (repo, branch, commit, etc.) in multiple output formats.",
7
7
  "author": "Drswith",
@@ -1,72 +0,0 @@
1
- import { FilterPattern } from "unplugin";
2
-
3
- //#region src/core/git.d.ts
4
- type GitField = "repo" | "branch" | "commit" | "commitShort" | "author" | "authorEmail" | "commitTime" | "commitMessage" | "tag" | "isDirty" | "remoteUrl" | "root";
5
- type GitLog = Record<string, string | boolean>;
6
- /**
7
- * ่Žทๅ– Git ๆ—ฅๅฟ—
8
- */
9
- declare function getGitLog(fields?: string[], cwd?: string): GitLog;
10
- /**
11
- * ่Žทๅ–ๆ‰€ๆœ‰ๅฏ็”จ็š„ Git ๅญ—ๆฎต
12
- */
13
- declare function getAvailableFields(): GitField[];
14
- //#endregion
15
- //#region src/core/outputs.d.ts
16
- interface JsonOutputOptions {
17
- fileName?: string;
18
- }
19
- interface WindowOutputOptions {
20
- varName?: string;
21
- console?: boolean;
22
- }
23
- interface EnvOutputOptions {
24
- prefix?: string;
25
- file?: string;
26
- }
27
- interface TypesOutputOptions {
28
- fileName?: string;
29
- }
30
- interface OutputOptions {
31
- json?: JsonOutputOptions;
32
- window?: WindowOutputOptions;
33
- env?: EnvOutputOptions;
34
- types?: TypesOutputOptions;
35
- }
36
- /**
37
- * ็”Ÿๆˆ JSON ๆ–‡ไปถ
38
- */
39
- declare function generateJson(gitLog: GitLog, options?: JsonOutputOptions, outputDir?: string): void;
40
- /**
41
- * ็”Ÿๆˆ window ๅ…จๅฑ€ๅ˜้‡ๆ–‡ไปถ
42
- */
43
- declare function generateWindowVar(gitLog: GitLog, options?: WindowOutputOptions, outputDir?: string): string;
44
- /**
45
- * ็”Ÿๆˆ็Žฏๅขƒๅ˜้‡ๆ–‡ไปถ
46
- */
47
- declare function generateEnvVars(gitLog: GitLog, options?: EnvOutputOptions, outputDir?: string): void;
48
- /**
49
- * ็”Ÿๆˆ TypeScript ็ฑปๅž‹ๅฎšไน‰ๆ–‡ไปถ
50
- */
51
- declare function generateTypeDefinitions(gitLog: GitLog, options?: TypesOutputOptions, outputDir?: string): void;
52
- /**
53
- * ๆ นๆฎ้…็ฝฎ็”Ÿๆˆๆ‰€ๆœ‰่พ“ๅ‡บ
54
- */
55
- declare function generateOutputs(gitLog: GitLog, outputOptions: OutputOptions, outputDir?: string): string | undefined;
56
- //#endregion
57
- //#region src/core/options.d.ts
58
- interface Options {
59
- include?: FilterPattern;
60
- exclude?: FilterPattern;
61
- enforce?: "pre" | "post" | undefined;
62
- enable?: boolean;
63
- fields?: GitField[] | string[];
64
- outputs?: OutputOptions;
65
- cwd?: string;
66
- }
67
- type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U;
68
- type OptionsResolved = Overwrite<Required<Pick<Options, "include" | "exclude">>, Omit<Pick<Options, "enforce" | "enable" | "fields" | "outputs" | "cwd">, "outputs"> & {
69
- outputs: OutputOptions;
70
- }>;
71
- //#endregion
72
- export { OutputOptions as a, generateEnvVars as c, generateTypeDefinitions as d, generateWindowVar as f, getGitLog as g, getAvailableFields as h, JsonOutputOptions as i, generateJson as l, GitLog as m, OptionsResolved as n, TypesOutputOptions as o, GitField as p, EnvOutputOptions as r, WindowOutputOptions as s, Options as t, generateOutputs as u };
@@ -1,65 +0,0 @@
1
- import { r as generateOutputs, s as getGitLog } from "./outputs-CbT7SkvQ.mjs";
2
- import { createUnplugin } from "unplugin";
3
-
4
- //#region src/core/options.ts
5
- const DEFAULT_FIELDS = [
6
- "repo",
7
- "branch",
8
- "commit",
9
- "commitShort",
10
- "author",
11
- "authorEmail",
12
- "commitTime",
13
- "commitMessage",
14
- "isDirty"
15
- ];
16
- function resolveOptions(options = {}) {
17
- return {
18
- include: options.include || [/\.[cm]?[jt]sx?$/],
19
- exclude: options.exclude || [/node_modules/],
20
- enforce: "enforce" in options ? options.enforce : "post",
21
- enable: options.enable !== void 0 ? options.enable : true,
22
- fields: options.fields || DEFAULT_FIELDS,
23
- outputs: options.outputs || {},
24
- cwd: options.cwd
25
- };
26
- }
27
-
28
- //#endregion
29
- //#region src/index.ts
30
- const AutoGitLog = createUnplugin((rawOptions = {}) => {
31
- const options = resolveOptions(rawOptions);
32
- let outputDir;
33
- let windowVarFileName;
34
- return {
35
- name: "unplugin-auto-git-log",
36
- enforce: options.enforce,
37
- configResolved(config) {
38
- if ("vite" in config) {
39
- const viteConfig = config;
40
- outputDir = viteConfig.build?.outDir || viteConfig.vite?.config?.build?.outDir;
41
- } else if ("webpack" in config) outputDir = config.output?.path;
42
- else if ("output" in config && typeof config.output === "object") outputDir = config.output?.dir;
43
- if (!outputDir) outputDir = "dist";
44
- },
45
- buildEnd() {
46
- if (!options.enable) return;
47
- const gitLog = getGitLog(options.fields, options.cwd);
48
- if (options.outputs && (options.outputs.json || options.outputs.window || options.outputs.env || options.outputs.types)) windowVarFileName = generateOutputs(gitLog, options.outputs, outputDir);
49
- else generateOutputs(gitLog, { json: {} }, outputDir);
50
- },
51
- vite: { transformIndexHtml(html) {
52
- if (options.outputs?.window && windowVarFileName) return {
53
- html,
54
- tags: [{
55
- tag: "script",
56
- attrs: { src: `/${windowVarFileName}` },
57
- injectTo: "head"
58
- }]
59
- };
60
- } }
61
- };
62
- });
63
-
64
- //#endregion
65
- export { AutoGitLog as t };