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

View File

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