This repository has been archived by the owner on Apr 6, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
nuxt.ts
185 lines (151 loc) · 4.36 KB
/
nuxt.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { getCurrentInstance } from 'vue'
import type { App, VNode } from 'vue'
import { createHooks, Hookable } from 'hookable'
import { defineGetter } from './utils'
import { legacyPlugin, LegacyContext } from './legacy'
type NuxtMeta = {
htmlAttrs?: string
headAttrs?: string
bodyAttrs?: string
headTags?: string
bodyPrepend?: string
bodyScripts?: string
}
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
}
export interface NuxtApp {
app: App
globalName: string
hooks: Hookable<RuntimeNuxtHooks>
hook: NuxtApp['hooks']['hook']
callHook: NuxtApp['hooks']['callHook']
[key: string]: any
_asyncDataPromises?: Record<string, Promise<any>>
_legacyContext?: LegacyContext
ssrContext?: Record<string, any> & {
renderMeta?: () => Promise<NuxtMeta> | NuxtMeta
}
payload: {
serverRendered?: true
data?: Record<string, any>
rendered?: Function
[key: string]: any
}
provide: (name: string, value: any) => void
}
export const NuxtPluginIndicator = '__nuxt_plugin'
export interface Plugin {
(nuxt: NuxtApp): Promise<void> | void
[NuxtPluginIndicator]?: true
}
export interface LegacyPlugin {
(context: LegacyContext, provide: NuxtApp['provide']): Promise<void> | void
}
export interface CreateOptions {
app: NuxtApp['app']
ssrContext?: NuxtApp['ssrContext']
globalName?: NuxtApp['globalName']
}
export function createNuxt (options: CreateOptions) {
const nuxt: NuxtApp = {
provide: undefined,
globalName: 'nuxt',
state: {},
payload: {},
isHydrating: process.client,
...options
} as any as NuxtApp
nuxt.hooks = createHooks<RuntimeNuxtHooks>()
nuxt.hook = nuxt.hooks.hook
nuxt.callHook = nuxt.hooks.callHook
nuxt.provide = (name: string, value: any) => {
const $name = '$' + name
defineGetter(nuxt, $name, value)
defineGetter(nuxt.app.config.globalProperties, $name, value)
}
// Inject $nuxt
defineGetter(nuxt.app, '$nuxt', nuxt)
defineGetter(nuxt.app.config.globalProperties, '$nuxt', nuxt)
// Expose nuxt to the renderContext
if (nuxt.ssrContext) {
nuxt.ssrContext.nuxt = nuxt
}
if (process.server) {
nuxt.payload = {
serverRendered: true
}
nuxt.ssrContext = nuxt.ssrContext || {}
// Expose to server renderer to create window.__NUXT__
nuxt.ssrContext.payload = nuxt.payload
}
if (process.client) {
nuxt.payload = window.__NUXT__ || {}
}
return nuxt
}
export function applyPlugin (nuxt: NuxtApp, plugin: Plugin) {
if (typeof plugin !== 'function') { return }
return callWithNuxt(nuxt, () => plugin(nuxt))
}
export async function applyPlugins (nuxt: NuxtApp, plugins: Plugin[]) {
for (const plugin of plugins) {
await applyPlugin(nuxt, plugin)
}
}
export function normalizePlugins (_plugins: Array<Plugin | LegacyPlugin>) {
let needsLegacyContext = false
const plugins = _plugins.map((plugin) => {
if (isLegacyPlugin(plugin)) {
needsLegacyContext = true
return (nuxt: NuxtApp) => plugin(nuxt._legacyContext!, nuxt.provide)
}
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]
}
let currentNuxtInstance: NuxtApp | null
export const setNuxtInstance = (nuxt: NuxtApp | null) => {
currentNuxtInstance = nuxt
}
/**
* Ensures that the setup function passed in has access to the Nuxt instance via `useNuxt`.
*
* @param nuxt A Nuxt instance
* @param setup The function to call
*/
export async function callWithNuxt (nuxt: NuxtApp, setup: () => any) {
setNuxtInstance(nuxt)
const p = setup()
setNuxtInstance(null)
await p
}
/**
* Returns the current Nuxt instance.
*/
export function useNuxtApp (): NuxtApp {
const vm = getCurrentInstance()
if (!vm) {
if (!currentNuxtInstance) {
throw new Error('nuxt instance unavailable')
}
return currentNuxtInstance
}
return vm.appContext.app.$nuxt
}