chore: improve resolve app options

This commit is contained in:
Sébastien Chopin 2020-08-19 14:38:18 +02:00
parent f6355ca65d
commit 978a997159
3 changed files with 23 additions and 10 deletions

View File

@ -1,20 +1,35 @@
import { resolve } from 'path' import { resolve } from 'path'
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 {
srcDir: string options: AppOptions
main?: string main?: string
routes: NuxtRoute[] routes: NuxtRoute[]
} }
interface AppOptions {
srcDir?: string
dir?: NormalizedConfiguration['dir']
extensions?: NormalizedConfiguration['extensions']
}
// Scan project structure // Scan project structure
export async function resolveApp (builder: Builder, srcDir: string): Promise<NuxtApp> { export async function resolveApp (builder: Builder, options: AppOptions = {}): Promise<NuxtApp> {
const { nuxt } = builder const { nuxt } = builder
options = defu(options, {
srcDir: nuxt.options.srcDir,
dir: nuxt.options.dir,
extensions: nuxt.options.extensions
})
// Create base app object // Create base app object
const app: NuxtApp = { const app: NuxtApp = {
srcDir, options,
// Overwritten by the resolvers
main: '', main: '',
routes: [] routes: []
} }

View File

@ -70,7 +70,7 @@ export async function generate (builder: Builder) {
const { nuxt } = builder const { nuxt } = builder
await fsExtra.mkdirp(nuxt.options.buildDir) await fsExtra.mkdirp(nuxt.options.buildDir)
builder.app = await resolveApp(builder, nuxt.options.srcDir) builder.app = await resolveApp(builder)
const templatesDir = join(builder.nuxt.options.appDir, '_templates') const templatesDir = join(builder.nuxt.options.appDir, '_templates')
const appTemplates = await scanTemplates(templatesDir, templateData(builder)) const appTemplates = await scanTemplates(templatesDir, templateData(builder))

View File

@ -13,14 +13,12 @@ export interface NuxtRoute {
export async function resolvePagesRoutes (builder, app: NuxtApp) { export async function resolvePagesRoutes (builder, app: NuxtApp) {
const { nuxt } = builder const { nuxt } = builder
const pagesDirName = app.options.dir.pages
const extensions = app.options.extensions
// TODO: these variables should be overriable by app not global const pagesDir = resolve(app.options.srcDir, pagesDirName)
const pagesDirName = nuxt.options.dir.pages
const extensions = nuxt.options.extensions
const pagesDir = resolve(app.srcDir, pagesDirName)
const pagesPattern = `${pagesDirName}/**/*.{${extensions.join(',')}}` const pagesPattern = `${pagesDirName}/**/*.{${extensions.join(',')}}`
const files = await resolveFiles(builder, pagesPattern, app.srcDir) const files = await resolveFiles(builder, pagesPattern, app.options.srcDir)
const routes: NuxtRoute[] = [] const routes: NuxtRoute[] = []