2021-10-06 12:37:45 +00:00
|
|
|
/* eslint-disable no-use-before-define */
|
2021-10-02 20:30:20 +00:00
|
|
|
import { getCurrentInstance, reactive } from 'vue'
|
2021-08-26 18:57:36 +00:00
|
|
|
import type { App, VNode } from 'vue'
|
2021-08-27 12:51:40 +00:00
|
|
|
import { createHooks, Hookable } from 'hookable'
|
2021-04-09 13:48:39 +00:00
|
|
|
import { defineGetter } from './utils'
|
2021-06-18 17:16:51 +00:00
|
|
|
import { legacyPlugin, LegacyContext } from './legacy'
|
2021-01-18 12:46:19 +00:00
|
|
|
|
2021-07-15 11:28:04 +00:00
|
|
|
type NuxtMeta = {
|
|
|
|
htmlAttrs?: string
|
|
|
|
headAttrs?: string
|
|
|
|
bodyAttrs?: string
|
|
|
|
headTags?: string
|
|
|
|
bodyPrepend?: string
|
|
|
|
bodyScripts?: string
|
|
|
|
}
|
|
|
|
|
2021-08-26 18:57:36 +00:00
|
|
|
type HookResult = Promise<void> | void
|
|
|
|
export interface RuntimeNuxtHooks {
|
|
|
|
'app:created': (app: App<Element>) => HookResult
|
|
|
|
'app:beforeMount': (app: App<Element>) => HookResult
|
|
|
|
'app:mounted': (app: App<Element>) => HookResult
|
|
|
|
'app:rendered': () => HookResult
|
|
|
|
'page:start': (Component?: VNode) => HookResult
|
|
|
|
'page:finish': (Component?: VNode) => HookResult
|
2021-10-06 12:37:45 +00:00
|
|
|
'meta:register': (metaRenderers: Array<(nuxt: NuxtApp) => NuxtMeta | Promise<NuxtMeta>>) => HookResult
|
2021-08-26 18:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-08-27 13:30:53 +00:00
|
|
|
export interface NuxtApp {
|
2021-10-02 22:39:02 +00:00
|
|
|
app: App<Element>
|
2021-01-18 12:46:19 +00:00
|
|
|
globalName: string
|
|
|
|
|
2021-08-27 12:51:40 +00:00
|
|
|
hooks: Hookable<RuntimeNuxtHooks>
|
2021-08-27 13:30:53 +00:00
|
|
|
hook: NuxtApp['hooks']['hook']
|
|
|
|
callHook: NuxtApp['hooks']['callHook']
|
2021-01-18 12:46:19 +00:00
|
|
|
|
|
|
|
[key: string]: any
|
|
|
|
|
2021-04-03 10:03:20 +00:00
|
|
|
_asyncDataPromises?: Record<string, Promise<any>>
|
2021-06-18 17:16:51 +00:00
|
|
|
_legacyContext?: LegacyContext
|
2021-04-03 10:03:20 +00:00
|
|
|
|
2021-07-15 11:28:04 +00:00
|
|
|
ssrContext?: Record<string, any> & {
|
2021-07-19 13:27:20 +00:00
|
|
|
renderMeta?: () => Promise<NuxtMeta> | NuxtMeta
|
2021-07-15 11:28:04 +00:00
|
|
|
}
|
2021-01-18 12:46:19 +00:00
|
|
|
payload: {
|
2021-03-17 09:17:18 +00:00
|
|
|
serverRendered?: true
|
|
|
|
data?: Record<string, any>
|
2021-01-18 12:46:19 +00:00
|
|
|
rendered?: Function
|
|
|
|
[key: string]: any
|
|
|
|
}
|
|
|
|
|
|
|
|
provide: (name: string, value: any) => void
|
|
|
|
}
|
|
|
|
|
2021-06-18 17:16:51 +00:00
|
|
|
export const NuxtPluginIndicator = '__nuxt_plugin'
|
2021-01-18 12:46:19 +00:00
|
|
|
export interface Plugin {
|
2021-08-27 13:30:53 +00:00
|
|
|
(nuxt: NuxtApp): Promise<void> | void
|
2021-06-18 17:16:51 +00:00
|
|
|
[NuxtPluginIndicator]?: true
|
|
|
|
}
|
|
|
|
export interface LegacyPlugin {
|
2021-08-27 13:30:53 +00:00
|
|
|
(context: LegacyContext, provide: NuxtApp['provide']): Promise<void> | void
|
2021-01-18 12:46:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface CreateOptions {
|
2021-08-27 13:30:53 +00:00
|
|
|
app: NuxtApp['app']
|
|
|
|
ssrContext?: NuxtApp['ssrContext']
|
|
|
|
globalName?: NuxtApp['globalName']
|
2021-01-18 12:46:19 +00:00
|
|
|
}
|
|
|
|
|
2021-10-02 18:40:10 +00:00
|
|
|
export function createNuxtApp (options: CreateOptions) {
|
2021-08-27 13:30:53 +00:00
|
|
|
const nuxt: NuxtApp = {
|
2021-01-18 12:46:19 +00:00
|
|
|
provide: undefined,
|
|
|
|
globalName: 'nuxt',
|
2021-10-08 14:21:55 +00:00
|
|
|
payload: reactive(process.server ? { serverRendered: true, data: {} } : (window.__NUXT__ || { data: {} })),
|
2021-01-18 12:46:19 +00:00
|
|
|
isHydrating: process.client,
|
2021-10-08 14:21:55 +00:00
|
|
|
_asyncDataPromises: {},
|
2021-01-18 12:46:19 +00:00
|
|
|
...options
|
2021-08-27 13:30:53 +00:00
|
|
|
} as any as NuxtApp
|
2021-01-18 12:46:19 +00:00
|
|
|
|
2021-08-27 12:51:40 +00:00
|
|
|
nuxt.hooks = createHooks<RuntimeNuxtHooks>()
|
2021-01-18 12:46:19 +00:00
|
|
|
nuxt.hook = nuxt.hooks.hook
|
|
|
|
nuxt.callHook = nuxt.hooks.callHook
|
|
|
|
|
|
|
|
nuxt.provide = (name: string, value: any) => {
|
|
|
|
const $name = '$' + name
|
2021-02-03 18:14:30 +00:00
|
|
|
defineGetter(nuxt, $name, value)
|
2021-01-18 12:46:19 +00:00
|
|
|
defineGetter(nuxt.app.config.globalProperties, $name, value)
|
|
|
|
}
|
|
|
|
|
2021-02-03 18:14:30 +00:00
|
|
|
// Inject $nuxt
|
|
|
|
defineGetter(nuxt.app, '$nuxt', nuxt)
|
|
|
|
defineGetter(nuxt.app.config.globalProperties, '$nuxt', nuxt)
|
2021-01-18 12:46:19 +00:00
|
|
|
|
|
|
|
// Expose nuxt to the renderContext
|
|
|
|
if (nuxt.ssrContext) {
|
|
|
|
nuxt.ssrContext.nuxt = nuxt
|
|
|
|
}
|
|
|
|
|
|
|
|
if (process.server) {
|
|
|
|
// Expose to server renderer to create window.__NUXT__
|
2021-10-08 14:21:55 +00:00
|
|
|
nuxt.ssrContext = nuxt.ssrContext || {}
|
2021-01-18 12:46:19 +00:00
|
|
|
nuxt.ssrContext.payload = nuxt.payload
|
|
|
|
}
|
|
|
|
|
2021-10-02 20:30:20 +00:00
|
|
|
// Expose runtime config
|
|
|
|
if (process.server) {
|
|
|
|
nuxt.provide('config', options.ssrContext.runtimeConfig.private)
|
|
|
|
nuxt.payload.config = options.ssrContext.runtimeConfig.public
|
|
|
|
} else {
|
|
|
|
nuxt.provide('config', reactive(nuxt.payload.config))
|
|
|
|
}
|
|
|
|
|
2021-01-18 12:46:19 +00:00
|
|
|
return nuxt
|
|
|
|
}
|
|
|
|
|
2021-08-27 13:30:53 +00:00
|
|
|
export function applyPlugin (nuxt: NuxtApp, plugin: Plugin) {
|
2021-02-19 01:08:45 +00:00
|
|
|
if (typeof plugin !== 'function') { return }
|
2021-06-18 17:16:51 +00:00
|
|
|
return callWithNuxt(nuxt, () => plugin(nuxt))
|
2021-01-18 12:46:19 +00:00
|
|
|
}
|
|
|
|
|
2021-08-27 13:30:53 +00:00
|
|
|
export async function applyPlugins (nuxt: NuxtApp, plugins: Plugin[]) {
|
2021-01-18 12:46:19 +00:00
|
|
|
for (const plugin of plugins) {
|
|
|
|
await applyPlugin(nuxt, plugin)
|
|
|
|
}
|
|
|
|
}
|
2021-04-09 13:48:39 +00:00
|
|
|
|
2021-06-18 17:16:51 +00:00
|
|
|
export function normalizePlugins (_plugins: Array<Plugin | LegacyPlugin>) {
|
|
|
|
let needsLegacyContext = false
|
|
|
|
|
|
|
|
const plugins = _plugins.map((plugin) => {
|
|
|
|
if (isLegacyPlugin(plugin)) {
|
|
|
|
needsLegacyContext = true
|
2021-08-27 13:30:53 +00:00
|
|
|
return (nuxt: NuxtApp) => plugin(nuxt._legacyContext!, nuxt.provide)
|
2021-06-18 17:16:51 +00:00
|
|
|
}
|
|
|
|
return plugin
|
|
|
|
})
|
|
|
|
|
|
|
|
if (needsLegacyContext) {
|
|
|
|
plugins.unshift(legacyPlugin)
|
|
|
|
}
|
|
|
|
|
|
|
|
return plugins as Plugin[]
|
|
|
|
}
|
|
|
|
|
|
|
|
export function defineNuxtPlugin (plugin: Plugin) {
|
|
|
|
plugin[NuxtPluginIndicator] = true
|
|
|
|
return plugin
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isLegacyPlugin (plugin: unknown): plugin is LegacyPlugin {
|
|
|
|
return !plugin[NuxtPluginIndicator]
|
|
|
|
}
|
|
|
|
|
2021-08-27 13:30:53 +00:00
|
|
|
let currentNuxtInstance: NuxtApp | null
|
2021-04-09 13:48:39 +00:00
|
|
|
|
2021-08-27 13:30:53 +00:00
|
|
|
export const setNuxtInstance = (nuxt: NuxtApp | null) => {
|
2021-04-09 13:48:39 +00:00
|
|
|
currentNuxtInstance = nuxt
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensures that the setup function passed in has access to the Nuxt instance via `useNuxt`.
|
2021-04-15 18:49:29 +00:00
|
|
|
*
|
2021-04-09 13:48:39 +00:00
|
|
|
* @param nuxt A Nuxt instance
|
|
|
|
* @param setup The function to call
|
|
|
|
*/
|
2021-08-27 13:30:53 +00:00
|
|
|
export async function callWithNuxt (nuxt: NuxtApp, setup: () => any) {
|
2021-04-09 13:48:39 +00:00
|
|
|
setNuxtInstance(nuxt)
|
|
|
|
const p = setup()
|
|
|
|
setNuxtInstance(null)
|
|
|
|
await p
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current Nuxt instance.
|
|
|
|
*/
|
2021-08-27 13:30:53 +00:00
|
|
|
export function useNuxtApp (): NuxtApp {
|
2021-04-09 13:48:39 +00:00
|
|
|
const vm = getCurrentInstance()
|
|
|
|
|
|
|
|
if (!vm) {
|
|
|
|
if (!currentNuxtInstance) {
|
|
|
|
throw new Error('nuxt instance unavailable')
|
|
|
|
}
|
|
|
|
return currentNuxtInstance
|
|
|
|
}
|
|
|
|
|
|
|
|
return vm.appContext.app.$nuxt
|
|
|
|
}
|
2021-10-02 20:30:20 +00:00
|
|
|
|
|
|
|
export function useRuntimeConfig (): Record<string, any> {
|
|
|
|
return useNuxtApp().$config
|
|
|
|
}
|