Skip to content

Commit a3fdeb4

Browse files
committed
feat: lazy generate meta
1 parent d09ac00 commit a3fdeb4

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,19 @@ export default defineConfig({
286286
})
287287
```
288288

289+
You can also generate meta data lazily.
290+
291+
```ts
292+
// vite.config.ts
293+
export default defineConfig({
294+
plugins: [
295+
BuildInfo({
296+
meta: async () => ({ message: 'This is set from vite.config.ts' })
297+
})
298+
]
299+
})
300+
```
301+
289302
Then you can import them in your Vite app.
290303

291304
```ts

src/core/index.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,14 @@ export const UnpluginInfo = createUnplugin<Options | undefined>((option) => {
9797
`export const name = ${ci.name !== null ? `\`${ci.name}\`` : 'null'}`
9898
].join('\n');
9999
} else if (id === ModuleName.BuildMeta) {
100-
const body = Object.entries(option?.meta ?? {}).map(
100+
const get = () => {
101+
if (!option?.meta) return {};
102+
if (typeof option.meta === 'function') {
103+
return option.meta();
104+
}
105+
return option.meta;
106+
};
107+
const body = Object.entries(await get()).map(
101108
([key, value]) => `export const ${key} = ${JSON.stringify(value, null, 2)};`
102109
);
103110
return body.join('\n');

src/core/types.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { SimpleGit } from 'simple-git';
22

3+
type Metadata = Record<string | number, any>;
4+
35
export interface Options {
46
/**
57
* Git repo root path
@@ -26,7 +28,7 @@ export interface Options {
2628
*
2729
* Notice: meta data will be serialized to JSON format
2830
*/
29-
meta?: Record<string | number | symbol, any>;
31+
meta?: Metadata | (() => Metadata | Promise<Metadata>);
3032

3133
/**
3234
* Custom virtual module prefix

0 commit comments

Comments
 (0)