Nuxt/packages/nuxt3/src/core/nuxt.ts

124 lines
3.5 KiB
TypeScript
Raw Normal View History

import { resolve } from 'pathe'
import { createHooks } from 'hookable'
import type { Nuxt, NuxtOptions, NuxtConfig, ModuleContainer, NuxtHooks } from '@nuxt/schema'
import { loadNuxtConfig, LoadNuxtOptions, nuxtCtx, installModule, addComponent, addVitePlugin, addWebpackPlugin } from '@nuxt/kit'
import pagesModule from '../pages/module'
import metaModule from '../meta/module'
import componentsModule from '../components/module'
import autoImportsModule from '../auto-imports/module'
import { distDir, pkgDir } from '../dirs'
import { version } from '../../package.json'
import { ImportProtectionPlugin, vueAppPatterns } from './plugins/import-protection'
2021-01-22 20:57:09 +00:00
import { initNitro } from './nitro'
import { addModuleTranspiles } from './modules'
2020-07-02 13:02:35 +00:00
export function createNuxt (options: NuxtOptions): Nuxt {
const hooks = createHooks<NuxtHooks>()
2021-04-15 19:17:44 +00:00
const nuxt: Nuxt = {
_version: version,
options,
hooks,
callHook: hooks.callHook,
addHooks: hooks.addHooks,
2021-04-15 19:17:44 +00:00
hook: hooks.hook,
ready: () => initNuxt(nuxt),
close: () => Promise.resolve(hooks.callHook('close', nuxt)),
vfs: {}
}
2021-04-15 19:17:44 +00:00
return nuxt
2020-08-02 15:50:35 +00:00
}
async function initNuxt (nuxt: Nuxt) {
// Register user hooks
nuxt.hooks.addHooks(nuxt.options.hooks)
2020-07-02 13:02:35 +00:00
// Set nuxt instance for useNuxt
nuxtCtx.set(nuxt)
nuxt.hook('close', () => nuxtCtx.unset())
// Init nitro
await initNitro(nuxt)
2020-07-02 13:02:35 +00:00
// Add nuxt3 types
nuxt.hook('prepare:types', (opts) => {
opts.references.push({ types: 'nuxt3' })
opts.references.push({ path: resolve(nuxt.options.buildDir, 'plugins.d.ts') })
// Add vue shim
if (nuxt.options.typescript.shim) {
opts.references.push({ path: resolve(nuxt.options.buildDir, 'vue-shim.d.ts') })
}
})
// Add import protection
const config = {
rootDir: nuxt.options.rootDir,
patterns: vueAppPatterns(nuxt)
}
addVitePlugin(ImportProtectionPlugin.vite(config))
addWebpackPlugin(ImportProtectionPlugin.webpack(config))
// Init user modules
2021-04-15 19:17:44 +00:00
await nuxt.callHook('modules:before', { nuxt } as ModuleContainer)
const modulesToInstall = [
...nuxt.options.buildModules,
...nuxt.options.modules,
...nuxt.options._modules
]
2020-07-02 13:02:35 +00:00
// Add <NuxtWelcome>
addComponent({
name: 'NuxtWelcome',
filePath: resolve(nuxt.options.appDir, 'components/nuxt-welcome.vue')
})
// Add <ClientOnly>
addComponent({
name: 'ClientOnly',
filePath: resolve(nuxt.options.appDir, 'components/client-only')
})
for (const m of modulesToInstall) {
if (Array.isArray(m)) {
await installModule(m[0], m[1], nuxt)
} else {
await installModule(m, {}, nuxt)
}
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
await addModuleTranspiles()
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 = await loadNuxtConfig(opts)
// Temporary until finding better placement for each
options.appDir = options.alias['#app'] = resolve(distDir, 'app')
options._majorVersion = 3
options._modules.push(pagesModule, metaModule, componentsModule, autoImportsModule)
options.modulesDir.push(resolve(pkgDir, 'node_modules'))
options.alias['vue-demi'] = resolve(options.appDir, 'compat/vue-demi')
options.alias['@vue/composition-api'] = resolve(options.appDir, 'compat/capi')
const nuxt = createNuxt(options)
2021-04-15 19:17:44 +00:00
if (opts.ready !== false) {
await nuxt.ready()
}
return nuxt
}
export function defineNuxtConfig (config: NuxtConfig): NuxtConfig {
return config
}
// For a convenience import together with `defineNuxtConfig`
export type { NuxtConfig }