2021-07-12 10:54:24 +00:00
|
|
|
import { normalize } from 'upath'
|
2020-07-30 23:40:16 +00:00
|
|
|
import Hookable from 'hookable'
|
2021-06-24 14:06:16 +00:00
|
|
|
import { loadNuxtConfig, LoadNuxtOptions, Nuxt, NuxtOptions, nuxtCtx, installModule, ModuleContainer } from '@nuxt/kit'
|
2021-01-22 20:57:09 +00:00
|
|
|
import { initNitro } from './nitro'
|
2020-07-02 13:02:35 +00:00
|
|
|
|
2021-04-02 11:47:01 +00:00
|
|
|
export function createNuxt (options: NuxtOptions): Nuxt {
|
2021-04-12 22:12:30 +00:00
|
|
|
const hooks = new Hookable() as any as Nuxt['hooks']
|
2021-04-02 11:47:01 +00:00
|
|
|
|
2021-04-15 19:17:44 +00:00
|
|
|
const nuxt: Nuxt = {
|
2021-04-02 11:47:01 +00:00
|
|
|
options,
|
|
|
|
hooks,
|
|
|
|
callHook: hooks.callHook,
|
2021-04-15 19:17:44 +00:00
|
|
|
hook: hooks.hook,
|
|
|
|
ready: () => initNuxt(nuxt),
|
2021-07-15 10:18:34 +00:00
|
|
|
close: () => Promise.resolve(hooks.callHook('close', nuxt)),
|
|
|
|
vfs: {}
|
2020-08-17 19:12:34 +00:00
|
|
|
}
|
2021-04-15 19:17:44 +00:00
|
|
|
|
|
|
|
return nuxt
|
2020-08-02 15:50:35 +00:00
|
|
|
}
|
|
|
|
|
2021-04-02 11:47:01 +00:00
|
|
|
async function initNuxt (nuxt: Nuxt) {
|
|
|
|
// Register user hooks
|
|
|
|
nuxt.hooks.addHooks(nuxt.options.hooks)
|
2020-07-02 13:02:35 +00:00
|
|
|
|
2021-06-24 14:06:16 +00:00
|
|
|
// Set nuxt instance for useNuxt
|
|
|
|
nuxtCtx.set(nuxt)
|
|
|
|
nuxt.hook('close', () => nuxtCtx.unset())
|
|
|
|
|
2021-04-02 11:47:01 +00:00
|
|
|
// Init nitro
|
|
|
|
await initNitro(nuxt)
|
2020-07-02 13:02:35 +00:00
|
|
|
|
2021-04-02 11:47:01 +00:00
|
|
|
// Init user modules
|
2021-04-15 19:17:44 +00:00
|
|
|
await nuxt.callHook('modules:before', { nuxt } as ModuleContainer)
|
2021-04-02 11:47:01 +00:00
|
|
|
const modulesToInstall = [
|
|
|
|
...nuxt.options.buildModules,
|
|
|
|
...nuxt.options.modules,
|
|
|
|
...nuxt.options._modules
|
|
|
|
]
|
2020-07-02 13:02:35 +00:00
|
|
|
|
2021-04-02 11:47:01 +00:00
|
|
|
for (const m of modulesToInstall) {
|
|
|
|
await installModule(nuxt, m)
|
2020-07-02 13:02:35 +00:00
|
|
|
}
|
|
|
|
|
2021-04-15 19:17:44 +00:00
|
|
|
await nuxt.callHook('modules:done', { nuxt } as ModuleContainer)
|
2020-07-02 13:02:35 +00:00
|
|
|
|
2021-04-02 11:47:01 +00:00
|
|
|
await nuxt.callHook('ready', nuxt)
|
|
|
|
}
|
2020-07-02 13:02:35 +00:00
|
|
|
|
2021-04-15 19:17:44 +00:00
|
|
|
export async function loadNuxt (opts: LoadNuxtOptions): Promise<Nuxt> {
|
|
|
|
const options = loadNuxtConfig(opts)
|
2021-03-28 21:12:16 +00:00
|
|
|
|
|
|
|
// Temp
|
2021-04-03 15:08:42 +00:00
|
|
|
const { appDir } = await import('@nuxt/app/meta')
|
|
|
|
options.appDir = appDir
|
2021-03-28 21:12:16 +00:00
|
|
|
options._majorVersion = 3
|
2021-07-12 10:54:24 +00:00
|
|
|
options.buildModules.push(normalize(require.resolve('@nuxt/pages/module')))
|
2021-07-15 11:28:04 +00:00
|
|
|
options.buildModules.push(normalize(require.resolve('@nuxt/meta/module')))
|
2021-07-12 10:54:24 +00:00
|
|
|
options.buildModules.push(normalize(require.resolve('@nuxt/component-discovery/module')))
|
2021-03-28 21:12:16 +00:00
|
|
|
|
2021-04-02 11:47:01 +00:00
|
|
|
const nuxt = createNuxt(options)
|
|
|
|
|
2021-04-15 19:17:44 +00:00
|
|
|
if (opts.ready !== false) {
|
|
|
|
await nuxt.ready()
|
|
|
|
}
|
2021-04-02 11:47:01 +00:00
|
|
|
|
2021-03-28 21:12:16 +00:00
|
|
|
return nuxt
|
|
|
|
}
|