Skip to content

Commit

Permalink
fix: type generation hook usage and timing
Browse files Browse the repository at this point in the history
  • Loading branch information
BobbieGoede committed Oct 3, 2024
1 parent 5a082d2 commit 5687e38
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/type-generation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,25 @@ export function enableVueI18nTypeGeneration(
*
* So far these hooks have been the most reliable way to fetch when the endpoint is ready to be used
*/
nuxt.hook('vite:serverCreated', () => {
useNitro().hooks.hook('compiled', async () => {
try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
res = await (await fetch(nuxt.options.devServer.url + 'api/merged')).json()
void updateTemplates({ filter: template => template.filename === 'types/i18n-messages.d.ts' })
} catch {
// console.log('fetch failed')
nuxt.hooks.hookOnce('vite:serverCreated', () => {
// eslint-disable-next-line @typescript-eslint/no-misused-promises
useNitro().hooks.afterEach(async e => {
if (e.name === 'dev:reload') {
try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
res = await (await fetch(nuxt.options.devServer.url + 'api/merged', { cache: 'no-cache' })).json()
await updateTemplates({ filter: template => template.filename === 'types/i18n-messages.d.ts' })
} catch {
// console.log('fetch failed')
}
}
})
})

addTypeTemplate({
filename: 'types/i18n-messages.d.ts',
getContents: () => {
// console.log(res)
if (res == null) return ''

return `// generated by @nuxtjs/i18n
Expand Down Expand Up @@ -109,7 +113,9 @@ export {}`
nuxt.hook('builder:watch', async (_, path) => {
const paths = nuxt.options._i18n.locales.flatMap(x => x.files.map(f => f.path))
if (!paths.includes(path) && !vueI18nConfigPaths.some(x => x.absolute.includes(path))) return

// @ts-ignore
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
res = await (await fetch(nuxt.options.devServer.url + 'api/merged', { cache: 'no-cache' })).json()
await updateTemplates({ filter: template => template.filename === 'types/i18n-messages.d.ts' })
})
}

0 comments on commit 5687e38

Please sign in to comment.