mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 21:55:11 +00:00
perf(kit): use pure readdir
to auto-load nuxt layers
This commit is contained in:
parent
3fb906ac29
commit
c45131b46f
@ -1,11 +1,13 @@
|
|||||||
|
import { existsSync } from 'node:fs'
|
||||||
|
import { readdir } from 'node:fs/promises'
|
||||||
import type { JSValue } from 'untyped'
|
import type { JSValue } from 'untyped'
|
||||||
import { applyDefaults } from 'untyped'
|
import { applyDefaults } from 'untyped'
|
||||||
import type { ConfigLayer, ConfigLayerMeta, LoadConfigOptions } from 'c12'
|
import type { ConfigLayer, ConfigLayerMeta, LoadConfigOptions } from 'c12'
|
||||||
import { loadConfig } from 'c12'
|
import { loadConfig } from 'c12'
|
||||||
import type { NuxtConfig, NuxtOptions } from '@nuxt/schema'
|
import type { NuxtConfig, NuxtOptions } from '@nuxt/schema'
|
||||||
import { NuxtConfigSchema } from '@nuxt/schema'
|
import { NuxtConfigSchema } from '@nuxt/schema'
|
||||||
import { globby } from 'globby'
|
|
||||||
import defu from 'defu'
|
import defu from 'defu'
|
||||||
|
import { join } from 'pathe'
|
||||||
|
|
||||||
export interface LoadNuxtConfigOptions extends Omit<LoadConfigOptions<NuxtConfig>, 'overrides'> {
|
export interface LoadNuxtConfigOptions extends Omit<LoadConfigOptions<NuxtConfig>, 'overrides'> {
|
||||||
overrides?: Exclude<LoadConfigOptions<NuxtConfig>['overrides'], Promise<any> | Function>
|
overrides?: Exclude<LoadConfigOptions<NuxtConfig>['overrides'], Promise<any> | Function>
|
||||||
@ -21,12 +23,16 @@ for (const key of layerSchemaKeys) {
|
|||||||
|
|
||||||
export async function loadNuxtConfig (opts: LoadNuxtConfigOptions): Promise<NuxtOptions> {
|
export async function loadNuxtConfig (opts: LoadNuxtConfigOptions): Promise<NuxtOptions> {
|
||||||
// Automatically detect and import layers from `~~/layers/` directory
|
// Automatically detect and import layers from `~~/layers/` directory
|
||||||
opts.overrides = defu(opts.overrides, {
|
const _extends: string[] = []
|
||||||
_extends: await globby('layers/*', {
|
const layersDir = join(opts.cwd || process.cwd(), 'layers')
|
||||||
onlyDirectories: true,
|
if (existsSync(layersDir)) {
|
||||||
cwd: opts.cwd || process.cwd(),
|
for (const file of await readdir(layersDir, { withFileTypes: true })) {
|
||||||
}),
|
if (file.isDirectory()) {
|
||||||
});
|
_extends.push(join(layersDir, file.name))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
opts.overrides = defu(opts.overrides, { _extends });
|
||||||
(globalThis as any).defineNuxtConfig = (c: any) => c
|
(globalThis as any).defineNuxtConfig = (c: any) => c
|
||||||
const result = await loadConfig<NuxtConfig>({
|
const result = await loadConfig<NuxtConfig>({
|
||||||
name: 'nuxt',
|
name: 'nuxt',
|
||||||
|
Loading…
Reference in New Issue
Block a user