Skip to content

Commit 8eb6475

Browse files
authored
feat: new hook context (#181)
1 parent b34d6ea commit 8eb6475

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

docs/reference/config.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,29 @@ More options, see [MDX Compile Options](https://mdxjs.com/packages/mdx/#compileo
269269

270270
## `prepare`
271271

272-
- Type: `(result: Result<Collections>) => Promisable<void | false>`
272+
- Type: `(data: Result<Collections>, context: Context) => Promisable<void | false>`
273273

274274
Data prepare hook, executed before write to file. You can apply additional processing to the output data, such as modify them, add missing data, handle relationships, or write them to files. return false to prevent the default output to a file if you wanted.
275275

276+
```js
277+
export default defineConfig({
278+
collections: { posts, tags },
279+
prepare: (data, context) => {
280+
// modify data
281+
data.posts.push({ ... })
282+
data.tags.push({ ... })
283+
284+
// context
285+
const { config } = context
286+
// config is resolved from `velite.config.js` with default values
287+
288+
// return false to prevent the default output to a file
289+
}
290+
})
291+
```
292+
276293
## `complete`
277294

278-
- Type: `(result: Result<Collections>) => Promisable<void>`
295+
- Type: `(data: Result<Collections>, context: Context) => Promisable<void>`
279296

280297
Build success hook, executed after the build is complete. You can do anything after the build is complete, such as print some tips or deploy the output files.

src/build.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,13 @@ const resolve = async (config: Config, changed?: string): Promise<Record<string,
134134
})
135135
)
136136

137+
const context = { config }
138+
137139
let shouldOutput = true
138140
// apply prepare hook
139141
if (typeof prepare === 'function') {
140142
const begin = performance.now()
141-
shouldOutput = (await prepare(result)) ?? true
143+
shouldOutput = (await prepare(result, context)) ?? true
142144
logger.log(`executed 'prepare' callback got ${shouldOutput}`, begin)
143145
}
144146

@@ -155,7 +157,7 @@ const resolve = async (config: Config, changed?: string): Promise<Record<string,
155157
// call complete hook
156158
if (typeof complete === 'function') {
157159
const begin = performance.now()
158-
await complete(result)
160+
await complete(result, context)
159161
logger.log(`executed 'complete' callback`, begin)
160162
}
161163

src/types.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,16 @@ export type CollectionType<T extends Collections, P extends keyof T> = T[P]['sin
198198
*/
199199
export type Result<T extends Collections> = { [P in keyof T]: CollectionType<T, P> }
200200

201+
/**
202+
* Hook context
203+
*/
204+
export type Context = {
205+
/**
206+
* Resolved config
207+
*/
208+
config: Config
209+
}
210+
201211
/**
202212
* This interface for plugins extra user config
203213
* @example
@@ -252,14 +262,14 @@ export interface UserConfig<T extends Collections = Collections> extends Partial
252262
* return false to prevent the default output to a file if you wanted
253263
* @param data loaded data
254264
*/
255-
prepare?: (data: Result<T>) => Promisable<void | false>
265+
prepare?: (data: Result<T>, context: Context) => Promisable<void | false>
256266
/**
257267
* Build success hook
258268
* @description
259269
* You can do anything after the build is complete, such as print some tips or deploy the output files.
260270
* @param data loaded data
261271
*/
262-
complete?: (data: Result<T>) => Promisable<void>
272+
complete?: (data: Result<T>, context: Context) => Promisable<void>
263273
}
264274

265275
/**

0 commit comments

Comments
 (0)