mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 08:33:53 +00:00
chore(bridge): Improve typing of defineNuxtPlugin function (#1995)
This commit is contained in:
parent
a08b435f51
commit
18924da869
@ -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 {
|
||||||
setNuxtAppInstance(ctx.$_nuxtApp)
|
return (ctx) => {
|
||||||
plugin(ctx.$_nuxtApp)
|
setNuxtAppInstance(ctx.$_nuxtApp)
|
||||||
setNuxtAppInstance(null)
|
plugin(ctx.$_nuxtApp)
|
||||||
|
setNuxtAppInstance(null)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useNuxtApp = () => {
|
export const useNuxtApp = () => {
|
||||||
|
@ -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
|
||||||
|
@ -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]
|
||||||
}
|
}
|
||||||
|
@ -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 || ''
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
4
test/fixtures/bridge/plugins/setup.js
vendored
4
test/fixtures/bridge/plugins/setup.js
vendored
@ -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)
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user