fix(nitro): update nitro internal hook name (#218)

This commit is contained in:
Daniel Roe 2021-06-16 12:48:16 +01:00 committed by GitHub
parent 4095f96559
commit 77e489aae3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 8 additions and 8 deletions

View File

@ -58,7 +58,7 @@ export interface NuxtHooks {
'builder:watch': (event: WatchEvent, path: string) => HookResult
// @nuxt/nitro
'nitro:template': (template: { src: string, contents: string }) => HookResult
'nitro:document': (template: { src: string, contents: string }) => HookResult
// @nuxt/cli
'cli:buildError': (error: unknown) => HookResult

View File

@ -49,7 +49,7 @@ export async function build (nitroContext: NitroContext) {
const htmlTemplate = { src: htmlSrc, contents: '', dst: '', compiled: '' }
htmlTemplate.dst = htmlTemplate.src.replace(/.html$/, '.js').replace('app.', 'document.')
htmlTemplate.contents = await readFile(htmlTemplate.src, 'utf-8')
await nitroContext._internal.hooks.callHook('nitro:template:document', htmlTemplate)
await nitroContext._internal.hooks.callHook('nitro:document', htmlTemplate)
htmlTemplate.compiled = 'module.exports = ' + serializeTemplate(htmlTemplate.contents)
await writeFile(htmlTemplate.dst, htmlTemplate.compiled)

View File

@ -27,14 +27,14 @@ export default function nuxt2CompatModule () {
// Connect hooks
nuxt.addHooks(nitroContext.nuxtHooks)
nuxt.hook('close', () => nitroContext._internal.hooks.callHook('close'))
nitroContext._internal.hooks.hook('nitro:document', template => nuxt.callHook('nitro:document', template))
nuxt.addHooks(nitroDevContext.nuxtHooks)
nuxt.hook('close', () => nitroDevContext._internal.hooks.callHook('close'))
nitroDevContext._internal.hooks.hook('nitro:document', template => nuxt.callHook('nitro:document', template))
nitroDevContext._internal.hooks.hook('renderLoading',
(req, res) => nuxt.callHook('server:nuxt:renderLoading', req, res))
nitroContext._internal.hooks.hook('nitro:template', template => nuxt.callHook('nitro:template', template))
// Expose process.env.NITRO_PRESET
nuxt.options.env.NITRO_PRESET = nitroContext.preset
@ -54,7 +54,7 @@ export default function nuxt2CompatModule () {
})
// Add missing template variables (which normally renderer would create)
nitroContext._internal.hooks.hook('nitro:template:document', (htmlTemplate) => {
nitroContext._internal.hooks.hook('nitro:document', (htmlTemplate) => {
if (!htmlTemplate.contents.includes('BODY_SCRIPTS_PREPEND')) {
const fullTemplate = ['{{ BODY_SCRIPTS_PREPEND }}', '{{ APP }}', '{{ BODY_SCRIPTS }}'].join('\n ')
htmlTemplate.contents = htmlTemplate.contents.replace('{{ APP }}', fullTemplate)

View File

@ -61,7 +61,7 @@ if ('serviceWorker' in navigator) {
}
},
hooks: {
'nitro:template:document' (tmpl) {
'nitro:document' (tmpl) {
tmpl.compiled = tmpl.compiled.replace('</body>', script + '</body>')
},
async 'nitro:compiled' ({ output }: NitroContext) {

View File

@ -13,10 +13,12 @@ export function initNitro (nuxt: Nuxt) {
// @ts-ignore
nuxt.hooks.addHooks(nitroContext.nuxtHooks)
nuxt.hook('close', () => nitroContext._internal.hooks.callHook('close'))
nitroContext._internal.hooks.hook('nitro:document', template => nuxt.callHook('nitro:document', template))
// @ts-ignore
nuxt.hooks.addHooks(nitroDevContext.nuxtHooks)
nuxt.hook('close', () => nitroDevContext._internal.hooks.callHook('close'))
nitroDevContext._internal.hooks.hook('nitro:document', template => nuxt.callHook('nitro:document', template))
// Add nitro client plugin (to inject $fetch helper)
nuxt.hook('app:resolve', (app) => {
@ -26,8 +28,6 @@ export function initNitro (nuxt: Nuxt) {
// Expose process.env.NITRO_PRESET
nuxt.options.env.NITRO_PRESET = nitroContext.preset
nitroContext._internal.hooks.hook('nitro:template', template => nuxt.callHook('nitro:template', template))
// Wait for all modules to be ready
nuxt.hook('modules:done', async () => {
// Extend nitro with modules