2020-08-17 15:25:06 +00:00
|
|
|
import { resolve } from 'path'
|
2020-08-19 12:38:18 +00:00
|
|
|
import defu from 'defu'
|
2021-04-02 11:47:01 +00:00
|
|
|
import { tryResolvePath } from '@nuxt/kit'
|
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'
|
2021-02-19 01:08:45 +00:00
|
|
|
import { NuxtPlugin, resolvePlugins } from './plugins'
|
2021-04-02 11:47:01 +00:00
|
|
|
|
2020-08-17 15:25:06 +00:00
|
|
|
export interface NuxtApp {
|
2021-02-22 10:04:46 +00:00
|
|
|
main?: string
|
2020-08-17 15:25:06 +00:00
|
|
|
routes: NuxtRoute[]
|
2020-08-19 13:06:27 +00:00
|
|
|
dir: string
|
|
|
|
extensions: string[]
|
2021-02-19 01:08:45 +00:00
|
|
|
plugins: NuxtPlugin[]
|
2021-01-18 12:22:38 +00:00
|
|
|
templates: Record<string, string>
|
2020-08-19 13:06:27 +00:00
|
|
|
pages?: {
|
|
|
|
dir: string
|
|
|
|
}
|
2020-08-19 12:38:18 +00:00
|
|
|
}
|
|
|
|
|
2020-08-17 15:25:06 +00:00
|
|
|
// Scan project structure
|
2021-01-18 12:22:38 +00:00
|
|
|
export async function createApp (
|
|
|
|
builder: Builder,
|
|
|
|
options: Partial<NuxtApp> = {}
|
|
|
|
): Promise<NuxtApp> {
|
2020-08-18 17:08:06 +00:00
|
|
|
const { nuxt } = builder
|
2020-08-17 15:25:06 +00:00
|
|
|
|
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: [],
|
2021-02-19 01:08:45 +00:00
|
|
|
plugins: [],
|
2021-01-18 12:22:38 +00:00
|
|
|
templates: {},
|
2020-08-19 13:06:27 +00:00
|
|
|
pages: {
|
|
|
|
dir: 'pages'
|
|
|
|
}
|
2021-04-04 22:15:34 +00:00
|
|
|
} as NuxtApp)
|
2020-08-19 12:38:18 +00:00
|
|
|
|
2020-08-19 13:06:27 +00:00
|
|
|
// Resolve app.main
|
2021-04-02 11:47:01 +00:00
|
|
|
const resolveOptions = {
|
|
|
|
base: nuxt.options.srcDir,
|
|
|
|
alias: nuxt.options.alias,
|
|
|
|
extensions: nuxt.options.extensions
|
|
|
|
}
|
|
|
|
|
2020-08-19 13:06:27 +00:00
|
|
|
if (!app.main) {
|
2021-04-02 11:47:01 +00:00
|
|
|
app.main = tryResolvePath('~/App', resolveOptions) ||
|
|
|
|
tryResolvePath('~/app', resolveOptions)
|
2020-08-17 15:25:06 +00:00
|
|
|
}
|
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) {
|
2021-01-18 12:22:38 +00:00
|
|
|
app.routes.push(...(await resolvePagesRoutes(builder, app)))
|
|
|
|
}
|
2021-02-17 14:29:15 +00:00
|
|
|
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')
|
|
|
|
}
|
|
|
|
|
2021-02-19 01:08:45 +00:00
|
|
|
// Resolve plugins/
|
|
|
|
app.plugins = await resolvePlugins(builder, app)
|
2021-01-18 12:22:38 +00:00
|
|
|
|
2021-02-19 01:08:45 +00:00
|
|
|
return app
|
2021-01-18 12:22:38 +00:00
|
|
|
}
|