chore(bridge): Improve typing of defineNuxtPlugin function (#1995)

This commit is contained in:
Tobias Diez 2021-11-18 12:08:58 +01:00 committed by GitHub
parent a08b435f51
commit 18924da869
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 33 additions and 31 deletions

View File

@ -58,10 +58,12 @@ export const setNuxtAppInstance = (nuxt: NuxtAppCompat | null) => {
currentNuxtAppInstance = nuxt
}
export const defineNuxtPlugin = plugin => (ctx: Context) => {
setNuxtAppInstance(ctx.$_nuxtApp)
plugin(ctx.$_nuxtApp)
setNuxtAppInstance(null)
export function defineNuxtPlugin (plugin: (nuxtApp: NuxtAppCompat) => void): (ctx: Context) => void {
return (ctx) => {
setNuxtAppInstance(ctx.$_nuxtApp)
plugin(ctx.$_nuxtApp)
setNuxtAppInstance(null)
}
}
export const useNuxtApp = () => {

View File

@ -1,13 +1,13 @@
import { defineNuxtPlugin } from '#app'
export default defineNuxtPlugin((nuxt) => {
nuxt._setupFns = []
export default defineNuxtPlugin((nuxtApp) => {
nuxtApp._setupFns = []
const _originalSetup = nuxt.nuxt2Context.app.setup
const _originalSetup = nuxtApp.nuxt2Context.app.setup
nuxt.nuxt2Context.app.setup = function (...args) {
nuxtApp.nuxt2Context.app.setup = function (...args) {
const result = _originalSetup instanceof Function ? _originalSetup(...args) : {}
for (const fn of nuxt._setupFns) {
for (const fn of nuxtApp._setupFns) {
Object.assign(result, fn.call(this, ...args))
}
return result

View File

@ -26,12 +26,12 @@ const vueMetaRenderer = (nuxt) => {
}
}
export default defineNuxtPlugin((nuxt) => {
export default defineNuxtPlugin((nuxtApp) => {
const metaRenderers = [vueMetaRenderer]
nuxt.callHook('meta:register', metaRenderers)
nuxtApp.callHook('meta:register', metaRenderers)
nuxt.ssrContext.renderMeta = async () => {
nuxtApp.ssrContext.renderMeta = async () => {
const metadata = {
htmlAttrs: '',
headAttrs: '',
@ -41,7 +41,7 @@ export default defineNuxtPlugin((nuxt) => {
bodyScripts: ''
}
for await (const renderer of metaRenderers) {
const result = await renderer(nuxt)
const result = await renderer(nuxtApp)
for (const key in result) {
metadata[key] += result[key]
}

View File

@ -3,12 +3,12 @@ import { createMetaManager } from 'vue-meta'
import type { MetaObject } from '../types'
import { defineNuxtPlugin } from '#app'
export default defineNuxtPlugin((nuxt) => {
export default defineNuxtPlugin((nuxtApp) => {
const manager = createMetaManager(process.server)
nuxt.vueApp.use(manager)
nuxtApp.vueApp.use(manager)
nuxt._useMeta = (meta: MetaObject) => manager.addMeta(meta)
nuxtApp._useMeta = (meta: MetaObject) => manager.addMeta(meta)
if (process.client) {
const teleportTarget = document.createElement('div')
@ -19,19 +19,19 @@ export default defineNuxtPlugin((nuxt) => {
}
if (process.server) {
nuxt.ssrContext.renderMeta = async () => {
nuxtApp.ssrContext.renderMeta = async () => {
const { renderMetaToString } = await import('vue-meta/ssr')
nuxt.ssrContext.teleports = nuxt.ssrContext.teleports || {}
nuxtApp.ssrContext.teleports = nuxtApp.ssrContext.teleports || {}
await renderMetaToString(nuxt.app, nuxt.ssrContext)
await renderMetaToString(nuxtApp.app, nuxtApp.ssrContext)
return {
htmlAttrs: nuxt.ssrContext.teleports.htmlAttrs || '',
headAttrs: nuxt.ssrContext.teleports.headAttrs || '',
bodyAttrs: nuxt.ssrContext.teleports.bodyAttrs || '',
headTags: nuxt.ssrContext.teleports.head || '',
bodyPrepend: nuxt.ssrContext.teleports['body-prepend'] || '',
bodyScripts: nuxt.ssrContext.teleports.body || ''
htmlAttrs: nuxtApp.ssrContext.teleports.htmlAttrs || '',
headAttrs: nuxtApp.ssrContext.teleports.headAttrs || '',
bodyAttrs: nuxtApp.ssrContext.teleports.bodyAttrs || '',
headTags: nuxtApp.ssrContext.teleports.head || '',
bodyPrepend: nuxtApp.ssrContext.teleports['body-prepend'] || '',
bodyScripts: nuxtApp.ssrContext.teleports.body || ''
}
}
}

View File

@ -3,12 +3,12 @@ import { ref, watchEffect, onBeforeUnmount, getCurrentInstance } from 'vue'
import type { MetaObject } from '..'
import { defineNuxtPlugin } from '#app'
export default defineNuxtPlugin((nuxt) => {
export default defineNuxtPlugin((nuxtApp) => {
const head = createHead()
nuxt.vueApp.use(head)
nuxtApp.vueApp.use(head)
nuxt._useMeta = (meta: MetaObject) => {
nuxtApp._useMeta = (meta: MetaObject) => {
const headObj = ref(meta as any)
head.addHeadObjs(headObj)
@ -28,6 +28,6 @@ export default defineNuxtPlugin((nuxt) => {
}
if (process.server) {
nuxt.ssrContext.renderMeta = () => renderHeadToString(head)
nuxtApp.ssrContext.renderMeta = () => renderHeadToString(head)
}
})

View File

@ -2,10 +2,10 @@ import { onGlobalSetup, ref } from '@nuxtjs/composition-api'
import { defineNuxtPlugin } from '#app'
export default defineNuxtPlugin((nuxt) => {
export default defineNuxtPlugin((nuxtApp) => {
const globalsetup = ref('🚧')
onGlobalSetup(() => {
globalsetup.value = '✅'
})
nuxt.provide('globalsetup', globalsetup)
nuxtApp.provide('globalsetup', globalsetup)
})