Nuxt/packages/nuxt/src/app/entry.ts
renovate[bot] 0b1cdcc70d
chore(deps): update devdependency @nuxtjs/eslint-config-typescript to v12 (#9086)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Daniel Roe <daniel@roe.dev>
2023-01-14 01:13:48 +00:00

85 lines
2.3 KiB
TypeScript

// We set __webpack_public_path via this import with webpack builder
import { createSSRApp, createApp, nextTick } from 'vue'
import { $fetch } from 'ofetch'
// @ts-ignore
import { baseURL } from '#build/paths.mjs'
import type { CreateOptions } from '#app'
import { createNuxtApp, applyPlugins, normalizePlugins } from '#app'
import '#build/css'
// @ts-ignore
import _plugins from '#build/plugins'
// @ts-ignore
import RootComponent from '#build/root-component.mjs'
// @ts-ignore
import { appRootId } from '#build/nuxt.config.mjs'
if (!globalThis.$fetch) {
// @ts-ignore
globalThis.$fetch = $fetch.create({
baseURL: baseURL()
})
}
let entry: Function
const plugins = normalizePlugins(_plugins)
if (process.server) {
entry = async function createNuxtAppServer (ssrContext: CreateOptions['ssrContext']) {
const vueApp = createApp(RootComponent)
const nuxt = createNuxtApp({ vueApp, ssrContext })
try {
await applyPlugins(nuxt, plugins)
await nuxt.hooks.callHook('app:created', vueApp)
} catch (err) {
await nuxt.callHook('app:error', err)
nuxt.payload.error = (nuxt.payload.error || err) as any
}
return vueApp
}
}
if (process.client) {
// TODO: temporary webpack 5 HMR fix
// https://github.com/webpack-contrib/webpack-hot-middleware/issues/390
// @ts-ignore
if (process.dev && import.meta.webpackHot) {
// @ts-ignore
import.meta.webpackHot.accept()
}
entry = async function initApp () {
const isSSR = Boolean(window.__NUXT__?.serverRendered)
const vueApp = isSSR ? createSSRApp(RootComponent) : createApp(RootComponent)
const nuxt = createNuxtApp({ vueApp })
try {
await applyPlugins(nuxt, plugins)
} catch (err) {
await nuxt.callHook('app:error', err)
nuxt.payload.error = (nuxt.payload.error || err) as any
}
try {
await nuxt.hooks.callHook('app:created', vueApp)
await nuxt.hooks.callHook('app:beforeMount', vueApp)
vueApp.mount('#' + appRootId)
await nuxt.hooks.callHook('app:mounted', vueApp)
await nextTick()
} catch (err) {
await nuxt.callHook('app:error', err)
nuxt.payload.error = (nuxt.payload.error || err) as any
}
}
entry().catch((error: unknown) => {
console.error('Error while mounting app:', error)
})
}
export default (ctx?: CreateOptions['ssrContext']) => entry(ctx)