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

47 lines
1.1 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'
2020-08-19 12:38:18 +00:00
import { NormalizedConfiguration } from 'src/config'
export interface NuxtApp {
2020-08-19 12:38:18 +00:00
options: AppOptions
main?: string
routes: NuxtRoute[]
}
2020-08-19 12:38:18 +00:00
interface AppOptions {
srcDir?: string
dir?: NormalizedConfiguration['dir']
extensions?: NormalizedConfiguration['extensions']
}
// Scan project structure
2020-08-19 12:38:18 +00:00
export async function resolveApp (builder: Builder, options: AppOptions = {}): Promise<NuxtApp> {
2020-08-18 17:08:06 +00:00
const { nuxt } = builder
2020-08-19 12:38:18 +00:00
options = defu(options, {
srcDir: nuxt.options.srcDir,
dir: nuxt.options.dir,
extensions: nuxt.options.extensions
})
2020-08-18 18:34:08 +00:00
// Create base app object
const app: NuxtApp = {
2020-08-19 12:38:18 +00:00
options,
// Overwritten by the resolvers
2020-08-18 18:34:08 +00:00
main: '',
routes: []
}
2020-08-18 17:08:06 +00:00
2020-08-18 18:34:08 +00:00
// Resolve App.vue
app.main = nuxt.resolver.tryResolvePath('~/App') ||
nuxt.resolver.tryResolvePath('~/app') ||
resolve(nuxt.options.appDir, 'app.vue')
2020-08-18 17:08:06 +00:00
2020-08-18 18:34:08 +00:00
// Resolve pages/
app.routes.push(...await resolvePagesRoutes(builder, app))
2020-08-18 17:08:06 +00:00
2020-08-18 18:34:08 +00:00
return app
2020-08-18 17:08:06 +00:00
}