2022-03-22 15:51:26 +00:00
|
|
|
// We set __webpack_public_path via this import with webpack builder
|
2023-04-07 16:02:47 +00:00
|
|
|
import { createApp, createSSRApp, nextTick } from 'vue'
|
2023-04-14 12:53:21 +00:00
|
|
|
|
2023-10-25 00:31:25 +00:00
|
|
|
// These files must be imported first as they have side effects
|
|
|
|
import '#build/paths.mjs'
|
|
|
|
import '#build/fetch.mjs'
|
2023-04-14 12:53:21 +00:00
|
|
|
|
2023-10-30 21:05:02 +00:00
|
|
|
import { applyPlugins, createNuxtApp } from './nuxt'
|
|
|
|
import type { CreateOptions } from './nuxt'
|
2023-04-14 12:53:21 +00:00
|
|
|
|
2022-02-25 12:42:34 +00:00
|
|
|
import '#build/css'
|
2023-04-14 12:53:21 +00:00
|
|
|
// @ts-expect-error virtual file
|
2023-06-19 23:00:03 +00:00
|
|
|
import plugins from '#build/plugins'
|
2023-04-14 12:53:21 +00:00
|
|
|
// @ts-expect-error virtual file
|
2022-02-25 12:42:34 +00:00
|
|
|
import RootComponent from '#build/root-component.mjs'
|
2023-04-14 12:53:21 +00:00
|
|
|
// @ts-expect-error virtual file
|
2023-08-12 07:19:36 +00:00
|
|
|
import { vueAppRootContainer } from '#build/nuxt.config.mjs'
|
2021-05-20 11:42:41 +00:00
|
|
|
|
2022-02-25 12:42:34 +00:00
|
|
|
let entry: Function
|
|
|
|
|
2023-08-07 22:03:40 +00:00
|
|
|
if (import.meta.server) {
|
2022-06-08 19:37:50 +00:00
|
|
|
entry = async function createNuxtAppServer (ssrContext: CreateOptions['ssrContext']) {
|
2022-02-25 12:42:34 +00:00
|
|
|
const vueApp = createApp(RootComponent)
|
|
|
|
|
|
|
|
const nuxt = createNuxtApp({ vueApp, ssrContext })
|
|
|
|
|
2022-03-11 08:22:16 +00:00
|
|
|
try {
|
|
|
|
await applyPlugins(nuxt, plugins)
|
|
|
|
await nuxt.hooks.callHook('app:created', vueApp)
|
|
|
|
} catch (err) {
|
2023-03-17 19:01:16 +00:00
|
|
|
await nuxt.hooks.callHook('app:error', err)
|
2022-08-12 17:47:58 +00:00
|
|
|
nuxt.payload.error = (nuxt.payload.error || err) as any
|
2022-03-11 08:22:16 +00:00
|
|
|
}
|
2023-06-07 12:18:50 +00:00
|
|
|
if (ssrContext?._renderResponse) { throw new Error('skipping render') }
|
2022-02-25 12:42:34 +00:00
|
|
|
|
|
|
|
return vueApp
|
|
|
|
}
|
|
|
|
}
|
2021-05-20 11:42:41 +00:00
|
|
|
|
2023-08-07 22:03:40 +00:00
|
|
|
if (import.meta.client) {
|
2022-02-25 12:42:34 +00:00
|
|
|
// TODO: temporary webpack 5 HMR fix
|
|
|
|
// https://github.com/webpack-contrib/webpack-hot-middleware/issues/390
|
2023-08-07 22:03:40 +00:00
|
|
|
if (import.meta.dev && import.meta.webpackHot) {
|
2022-02-25 12:42:34 +00:00
|
|
|
import.meta.webpackHot.accept()
|
|
|
|
}
|
|
|
|
|
2023-05-09 10:22:38 +00:00
|
|
|
// eslint-disable-next-line
|
|
|
|
let vueAppPromise: Promise<any>
|
|
|
|
|
2022-02-25 12:42:34 +00:00
|
|
|
entry = async function initApp () {
|
2023-05-09 10:22:38 +00:00
|
|
|
if (vueAppPromise) { return vueAppPromise }
|
2023-04-07 10:34:35 +00:00
|
|
|
const isSSR = Boolean(
|
|
|
|
window.__NUXT__?.serverRendered ||
|
|
|
|
document.getElementById('__NUXT_DATA__')?.dataset.ssr === 'true'
|
|
|
|
)
|
2022-02-25 12:42:34 +00:00
|
|
|
const vueApp = isSSR ? createSSRApp(RootComponent) : createApp(RootComponent)
|
|
|
|
|
|
|
|
const nuxt = createNuxtApp({ vueApp })
|
|
|
|
|
2022-03-11 08:22:16 +00:00
|
|
|
try {
|
|
|
|
await applyPlugins(nuxt, plugins)
|
|
|
|
} catch (err) {
|
|
|
|
await nuxt.callHook('app:error', err)
|
2022-08-12 17:47:58 +00:00
|
|
|
nuxt.payload.error = (nuxt.payload.error || err) as any
|
2022-03-11 08:22:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await nuxt.hooks.callHook('app:created', vueApp)
|
|
|
|
await nuxt.hooks.callHook('app:beforeMount', vueApp)
|
2023-08-12 07:19:36 +00:00
|
|
|
vueApp.mount(vueAppRootContainer)
|
2022-03-11 08:22:16 +00:00
|
|
|
await nuxt.hooks.callHook('app:mounted', vueApp)
|
|
|
|
await nextTick()
|
|
|
|
} catch (err) {
|
|
|
|
await nuxt.callHook('app:error', err)
|
2022-08-12 17:47:58 +00:00
|
|
|
nuxt.payload.error = (nuxt.payload.error || err) as any
|
2022-03-11 08:22:16 +00:00
|
|
|
}
|
2023-05-09 10:22:38 +00:00
|
|
|
|
|
|
|
return vueApp
|
2022-02-25 12:42:34 +00:00
|
|
|
}
|
|
|
|
|
2023-05-09 10:22:38 +00:00
|
|
|
vueAppPromise = entry().catch((error: unknown) => {
|
2023-01-14 01:13:48 +00:00
|
|
|
console.error('Error while mounting app:', error)
|
2022-02-25 12:42:34 +00:00
|
|
|
})
|
2021-05-20 11:42:41 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 12:42:34 +00:00
|
|
|
export default (ctx?: CreateOptions['ssrContext']) => entry(ctx)
|