refactor: simplify nuxt app interface

This commit is contained in:
pooya parsa 2020-08-19 15:06:27 +02:00
parent 978a997159
commit 929dfdc121
2 changed files with 26 additions and 32 deletions

View File

@ -2,45 +2,43 @@ import { resolve } from 'path'
import defu from 'defu'
import { Builder } from './builder'
import { NuxtRoute, resolvePagesRoutes } from './pages'
import { NormalizedConfiguration } from 'src/config'
export interface NuxtApp {
options: AppOptions
main?: string
main: string
routes: NuxtRoute[]
}
interface AppOptions {
srcDir?: string
dir?: NormalizedConfiguration['dir']
extensions?: NormalizedConfiguration['extensions']
dir: string
extensions: string[]
pages?: {
dir: string
}
}
// Scan project structure
export async function resolveApp (builder: Builder, options: AppOptions = {}): Promise<NuxtApp> {
export async function resolveApp (builder: Builder, options: Partial<NuxtApp> = {}): Promise<NuxtApp> {
const { nuxt } = builder
options = defu(options, {
srcDir: nuxt.options.srcDir,
dir: nuxt.options.dir,
extensions: nuxt.options.extensions
// Create base app object
const app: NuxtApp = defu(options, {
dir: nuxt.options.srcDir,
extensions: nuxt.options.extensions,
routes: [],
pages: {
dir: 'pages'
}
})
// Create base app object
const app: NuxtApp = {
options,
// Overwritten by the resolvers
main: '',
routes: []
// Resolve app.main
if (!app.main) {
app.main = nuxt.resolver.tryResolvePath('~/App') ||
nuxt.resolver.tryResolvePath('~/app') ||
resolve(nuxt.options.appDir, 'app.vue')
}
// Resolve App.vue
app.main = nuxt.resolver.tryResolvePath('~/App') ||
nuxt.resolver.tryResolvePath('~/app') ||
resolve(nuxt.options.appDir, 'app.vue')
// Resolve pages/
app.routes.push(...await resolvePagesRoutes(builder, app))
if (app.pages) {
app.routes.push(...await resolvePagesRoutes(builder, app))
}
return app
}

View File

@ -12,13 +12,9 @@ export interface NuxtRoute {
}
export async function resolvePagesRoutes (builder, app: NuxtApp) {
const { nuxt } = builder
const pagesDirName = app.options.dir.pages
const extensions = app.options.extensions
const pagesDir = resolve(app.options.srcDir, pagesDirName)
const pagesPattern = `${pagesDirName}/**/*.{${extensions.join(',')}}`
const files = await resolveFiles(builder, pagesPattern, app.options.srcDir)
const pagesDir = resolve(app.dir, app.pages!.dir)
const pagesPattern = `${app.pages!.dir}/**/*.{${app.extensions.join(',')}}`
const files = await resolveFiles(builder, pagesPattern, app.dir)
const routes: NuxtRoute[] = []