mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 08:33:53 +00:00
feat(docs): describe updateTemplates
This commit is contained in:
parent
daf3a8ae2d
commit
c0f701d41a
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user