mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 05:35:13 +00:00
feat: add vue-app types (#12)
* feat: preliminary vue-app types * style: fix various lint issues * style: further fixing (not caught by eslint) * chore: add alias for nuxt-app Co-authored-by: pooya parsa <pyapar@gmail.com>
This commit is contained in:
parent
e7dd27fa2a
commit
a74b48c648
10
packages/nuxt3/src/vue-app/declarations/node.d.ts
vendored
Normal file
10
packages/nuxt3/src/vue-app/declarations/node.d.ts
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
declare module NodeJS {
|
||||
interface Process {
|
||||
browser: boolean
|
||||
client: boolean
|
||||
mode: 'spa' | 'universal'
|
||||
modern: boolean
|
||||
server: boolean
|
||||
static: boolean
|
||||
}
|
||||
}
|
6
packages/nuxt3/src/vue-app/declarations/nuxt-build.d.ts
vendored
Normal file
6
packages/nuxt3/src/vue-app/declarations/nuxt-build.d.ts
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
declare module 'nuxt-build/routes' {
|
||||
import { RouteRecordRaw } from 'vue-router'
|
||||
|
||||
const _default: RouteRecordRaw[]
|
||||
export default _default
|
||||
}
|
3
packages/nuxt3/src/vue-app/declarations/window.d.ts
vendored
Normal file
3
packages/nuxt3/src/vue-app/declarations/window.d.ts
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
interface Window {
|
||||
__NUXT__?: Record<string, any>
|
||||
}
|
@ -1,28 +1,54 @@
|
||||
import type { IncomingMessage, ServerResponse } from 'http'
|
||||
import Hookable from 'hookable'
|
||||
import type { App } from 'vue'
|
||||
|
||||
import type { Plugin } from './types'
|
||||
import { defineGetter } from './utils'
|
||||
|
||||
class Nuxt extends Hookable {
|
||||
constructor ({ app, ssrContext, globalName }) {
|
||||
export class Nuxt extends Hookable {
|
||||
app: App<Element>
|
||||
ssrContext?: Record<string, any>
|
||||
globalName: string
|
||||
context: {
|
||||
req?: IncomingMessage
|
||||
res?: ServerResponse
|
||||
}
|
||||
|
||||
constructor ({ app, ssrContext, globalName }: { app: Nuxt['app'], ssrContext?: Nuxt['ssrContext'], globalName: Nuxt['globalName'] }) {
|
||||
super()
|
||||
this.app = app
|
||||
this.ssrContext = ssrContext
|
||||
this.globalName = globalName
|
||||
}
|
||||
|
||||
provide (name, value) {
|
||||
provide (name: string, value: any) {
|
||||
const $name = '$' + name
|
||||
defineGetter(this.app, $name, value)
|
||||
defineGetter(this.app.config.globalProperties, $name, value)
|
||||
}
|
||||
}
|
||||
|
||||
export async function init ({ app, plugins, ssrContext, globalName = 'nuxt' }) {
|
||||
interface InitOptions {
|
||||
app: Nuxt['app']
|
||||
plugins?: Plugin[]
|
||||
ssrContext?: Nuxt['ssrContext']
|
||||
globalName?: Nuxt['globalName']
|
||||
}
|
||||
|
||||
export async function init ({ app, plugins, ssrContext, globalName = 'nuxt' }: InitOptions) {
|
||||
const nuxt = new Nuxt({ app, ssrContext, globalName })
|
||||
nuxt.provide('nuxt', nuxt)
|
||||
|
||||
const inject = nuxt.provide.bind(nuxt)
|
||||
const inject: Nuxt['provide'] = nuxt.provide.bind(nuxt)
|
||||
|
||||
for (const plugin of plugins) {
|
||||
await plugin(nuxt, inject)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
declare module 'vue' {
|
||||
interface App {
|
||||
$nuxt: Nuxt
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { createSSRApp } from 'vue'
|
||||
import plugins from './plugins.client'
|
||||
import { init } from 'nuxt-app'
|
||||
import plugins from './plugins.client'
|
||||
import App from '<%= appPath %>'
|
||||
|
||||
async function initApp () {
|
||||
|
@ -1,5 +1,5 @@
|
||||
import sharedPlugins from './plugins'
|
||||
import preload from 'nuxt-app/plugins/preload'
|
||||
import sharedPlugins from './plugins'
|
||||
|
||||
export default [
|
||||
...sharedPlugins,
|
||||
|
@ -1,3 +1,5 @@
|
||||
import type { Plugin } from 'nuxt/vue-app/types'
|
||||
|
||||
// import { h, defineComponent } from 'vue'
|
||||
import { RouterLink } from 'vue-router'
|
||||
|
||||
@ -5,7 +7,9 @@ import { RouterLink } from 'vue-router'
|
||||
// extends: Link
|
||||
// })
|
||||
|
||||
export default function components ({ app }) {
|
||||
const components: Plugin = function ({ app }) {
|
||||
app.component('NuxtLink', RouterLink)
|
||||
app.component('NLink', RouterLink) // TODO: deprecate
|
||||
}
|
||||
|
||||
export default components
|
||||
|
@ -1,8 +1,11 @@
|
||||
export default function legacy ({ app }) {
|
||||
import type { App } from 'vue'
|
||||
import type { Plugin } from 'nuxt/vue-app/types'
|
||||
|
||||
const legacy: Plugin = function ({ app }) {
|
||||
app.$nuxt.context = {}
|
||||
|
||||
if (process.client) {
|
||||
const legacyApp = { ...app }
|
||||
const legacyApp: App<Element> & { $root?: App<Element> } = { ...app }
|
||||
legacyApp.$root = legacyApp
|
||||
window[app.$nuxt.globalName] = legacyApp
|
||||
}
|
||||
@ -13,3 +16,5 @@ export default function legacy ({ app }) {
|
||||
app.$nuxt.context.res = ssrContext.res
|
||||
}
|
||||
}
|
||||
|
||||
export default legacy
|
||||
|
@ -1,4 +1,6 @@
|
||||
export default function preload ({ app }) {
|
||||
import type { Plugin } from 'nuxt/vue-app/types'
|
||||
|
||||
const preload: Plugin = function ({ app }) {
|
||||
app.mixin({
|
||||
beforeCreate () {
|
||||
const { _registeredComponents } = this.$nuxt.ssrContext
|
||||
@ -7,3 +9,5 @@ export default function preload ({ app }) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export default preload
|
||||
|
@ -1,9 +1,11 @@
|
||||
import { ref } from 'vue'
|
||||
import { createRouter, createWebHistory, createMemoryHistory } from 'vue-router'
|
||||
|
||||
import type { Plugin } from 'nuxt/vue-app/types'
|
||||
|
||||
import routes from 'nuxt-build/routes'
|
||||
|
||||
export default function router ({ app }) {
|
||||
const router: Plugin = function ({ app }) {
|
||||
const routerHistory = process.client
|
||||
? createWebHistory()
|
||||
: createMemoryHistory()
|
||||
@ -35,3 +37,5 @@ export default function router ({ app }) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export default router
|
||||
|
@ -1,4 +1,6 @@
|
||||
export default function state ({ app }) {
|
||||
import type { Plugin } from 'nuxt/vue-app/types'
|
||||
|
||||
const state: Plugin = function ({ app }) {
|
||||
if (process.server) {
|
||||
app.$nuxt.state = {
|
||||
serverRendered: true
|
||||
@ -11,3 +13,5 @@ export default function state ({ app }) {
|
||||
app.$nuxt.state = window.__NUXT__ || {}
|
||||
}
|
||||
}
|
||||
|
||||
export default state
|
||||
|
5
packages/nuxt3/src/vue-app/types.ts
Normal file
5
packages/nuxt3/src/vue-app/types.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import type { Nuxt } from './nuxt';
|
||||
|
||||
export interface Plugin {
|
||||
(nuxt: Nuxt, inject?: Nuxt['provide']): Promise<void> | void
|
||||
}
|
@ -1,3 +1,3 @@
|
||||
export function defineGetter (obj, key, val) {
|
||||
export function defineGetter<K extends string | number | symbol, V> (obj: Record<K, V>, key: K, val: V) {
|
||||
Object.defineProperty(obj, key, { get: () => val })
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user