feat(docs): describe `updateTemplates`

This commit is contained in:
Andrey Yolkin 2023-09-03 19:36:59 +03:00
parent daf3a8ae2d
commit c0f701d41a
No known key found for this signature in database
GPG Key ID: 4A2899263001EA49
1 changed files with 63 additions and 1 deletions

View File

@ -1960,7 +1960,69 @@ A template object or a string with the path to the template. If a string is prov
A function that will be called with the `options` object. It should return a string or a promise that resolves to a string. If `src` is provided, this function will be ignored.
### `updateTemplates({ filter?: ResolvedNuxtTemplate => boolean })`
### `updateTemplates`
Regenerate templates that match the filter. If no filter is provided, all templates will be regenerated.
#### Type
```ts
async function updateTemplates (options: UpdateTemplatesOptions): void
interface UpdateTemplatesOptions {
filter?: (template: ResolvedNuxtTemplate) => boolean
}
interface ResolvedNuxtTemplate {
src: string
filename: string
dst: string
options: Record<string, any>
getContents: (data: Record<string, any>) => string | Promise<string>
write: boolean
filename: string
dst: string
}
```
#### Parameters
##### `options`
**Type**: `UpdateTemplatesOptions`
**Default**: `{}`
Options to pass to the template. This object can have the following property:
- `filter` (optional)
**Type**: `(template: ResolvedNuxtTemplate) => boolean`
A function that will be called with the `template` object. It should return a boolean indicating whether the template should be regenerated. If `filter` is not provided, all templates will be regenerated.
#### Example
```ts
// https://github.com/nuxt/nuxt
import { defineNuxtModule, updateTemplates } from '@nuxt/kit'
export default defineNuxtModule({
setup(options, nuxt) {
// watch and rebuild routes template list when one of the pages changes
nuxt.hook('builder:watch', async (event, relativePath) => {
if (event === 'change') { return }
const path = resolve(nuxt.options.srcDir, relativePath)
if (updateTemplatePaths.some(dir => path.startsWith(dir))) {
await updateTemplates({
filter: template => template.filename === 'routes.mjs'
})
}
})
}
})
```
## Nitro