Nuxt/packages/nuxt3/src/builder/app.ts

73 lines
1.7 KiB
TypeScript
Raw Normal View History

import { resolve } from 'path'
2020-08-19 12:38:18 +00:00
import defu from 'defu'
2020-08-18 17:08:06 +00:00
import { Builder } from './builder'
2020-08-18 18:34:08 +00:00
import { NuxtRoute, resolvePagesRoutes } from './pages'
import { NuxtPlugin, resolvePlugins } from './plugins'
export interface NuxtApp {
2020-08-19 13:06:27 +00:00
main: string
routes: NuxtRoute[]
2020-08-19 13:06:27 +00:00
dir: string
extensions: string[]
plugins: NuxtPlugin[]
templates: Record<string, string>
2020-08-19 13:06:27 +00:00
pages?: {
dir: string
}
2020-08-19 12:38:18 +00:00
}
// Scan project structure
export async function createApp (
builder: Builder,
options: Partial<NuxtApp> = {}
): Promise<NuxtApp> {
2020-08-18 17:08:06 +00:00
const { nuxt } = builder
2020-08-19 13:06:27 +00:00
// Create base app object
const app: NuxtApp = defu(options, {
dir: nuxt.options.srcDir,
extensions: nuxt.options.extensions,
routes: [],
plugins: [],
templates: {},
2020-08-19 13:06:27 +00:00
pages: {
dir: 'pages'
}
2020-08-19 12:38:18 +00:00
})
2020-08-19 13:06:27 +00:00
// Resolve app.main
if (!app.main) {
app.main =
nuxt.resolver.tryResolvePath('~/App') ||
2020-08-19 15:28:04 +00:00
nuxt.resolver.tryResolvePath('~/app')
}
2020-08-18 17:08:06 +00:00
2020-08-18 18:34:08 +00:00
// Resolve pages/
2020-08-19 13:06:27 +00:00
if (app.pages) {
app.routes.push(...(await resolvePagesRoutes(builder, app)))
}
if (app.routes.length) {
// Add 404 page is not added
const page404 = app.routes.find(route => route.name === '404')
if (!page404) {
app.routes.push({
name: '404',
path: '/:catchAll(.*)*',
file: resolve(nuxt.options.appDir, 'pages/404.vue'),
children: []
})
}
2021-02-02 16:42:48 +00:00
}
2020-08-18 17:08:06 +00:00
2020-08-19 15:28:04 +00:00
// Fallback app.main
if (!app.main && app.routes.length) {
app.main = resolve(nuxt.options.appDir, 'app.pages.vue')
} else if (!app.main) {
app.main = resolve(nuxt.options.appDir, 'app.tutorial.vue')
}
// Resolve plugins/
app.plugins = await resolvePlugins(builder, app)
return app
}