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 currentNuxtAppInstance = nuxt
} }
export const defineNuxtPlugin = plugin => (ctx: Context) => { export function defineNuxtPlugin (plugin: (nuxtApp: NuxtAppCompat) => void): (ctx: Context) => void {
return (ctx) => {
setNuxtAppInstance(ctx.$_nuxtApp) setNuxtAppInstance(ctx.$_nuxtApp)
plugin(ctx.$_nuxtApp) plugin(ctx.$_nuxtApp)
setNuxtAppInstance(null) setNuxtAppInstance(null)
}
} }
export const useNuxtApp = () => { export const useNuxtApp = () => {

View File

@ -1,13 +1,13 @@
import { defineNuxtPlugin } from '#app' import { defineNuxtPlugin } from '#app'
export default defineNuxtPlugin((nuxt) => { export default defineNuxtPlugin((nuxtApp) => {
nuxt._setupFns = [] 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) : {} 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)) Object.assign(result, fn.call(this, ...args))
} }
return result return result

View File

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

View File

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

View File

@ -3,12 +3,12 @@ import { ref, watchEffect, onBeforeUnmount, getCurrentInstance } from 'vue'
import type { MetaObject } from '..' import type { MetaObject } from '..'
import { defineNuxtPlugin } from '#app' import { defineNuxtPlugin } from '#app'
export default defineNuxtPlugin((nuxt) => { export default defineNuxtPlugin((nuxtApp) => {
const head = createHead() 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) const headObj = ref(meta as any)
head.addHeadObjs(headObj) head.addHeadObjs(headObj)
@ -28,6 +28,6 @@ export default defineNuxtPlugin((nuxt) => {
} }
if (process.server) { 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' import { defineNuxtPlugin } from '#app'
export default defineNuxtPlugin((nuxt) => { export default defineNuxtPlugin((nuxtApp) => {
const globalsetup = ref('🚧') const globalsetup = ref('🚧')
onGlobalSetup(() => { onGlobalSetup(() => {
globalsetup.value = '✅' globalsetup.value = '✅'
}) })
nuxt.provide('globalsetup', globalsetup) nuxtApp.provide('globalsetup', globalsetup)
}) })