Skip to content

Commit

Permalink
feat(nuxt): auto-register layers in layers/ directory (#27221)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed May 15, 2024
1 parent b96b62e commit 06be4cc
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
11 changes: 10 additions & 1 deletion packages/kit/src/loader/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,28 @@ import type { ConfigLayer, ConfigLayerMeta, LoadConfigOptions } from 'c12'
import { loadConfig } from 'c12'
import type { NuxtConfig, NuxtOptions } from '@nuxt/schema'
import { NuxtConfigSchema } from '@nuxt/schema'
import { globby } from 'globby'
import defu from 'defu'

export interface LoadNuxtConfigOptions extends LoadConfigOptions<NuxtConfig> {}

const layerSchemaKeys = ['future', 'srcDir', 'rootDir', 'dir']
const layerSchema = Object.fromEntries(Object.entries(NuxtConfigSchema).filter(([key]) => layerSchemaKeys.includes(key)))

export async function loadNuxtConfig (opts: LoadNuxtConfigOptions): Promise<NuxtOptions> {
// Automatically detect and import layers from `~~/layers/` directory
opts.overrides = defu(opts.overrides, {
_extends: await globby('layers/*', {
onlyDirectories: true,
cwd: opts.cwd || process.cwd(),
}),
});
(globalThis as any).defineNuxtConfig = (c: any) => c
const result = await loadConfig<NuxtConfig>({
name: 'nuxt',
configFile: 'nuxt.config',
rcFile: '.nuxtrc',
extend: { extendKey: ['theme', 'extends'] },
extend: { extendKey: ['theme', 'extends', '_extends'] },
dotenv: true,
globalRc: true,
...opts,
Expand Down
13 changes: 12 additions & 1 deletion packages/nuxt/src/core/nuxt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { readPackageJSON, resolvePackageJSON } from 'pkg-types'

import escapeRE from 'escape-string-regexp'
import fse from 'fs-extra'
import { withoutLeadingSlash } from 'ufo'
import { withTrailingSlash, withoutLeadingSlash } from 'ufo'

import defu from 'defu'
import pagesModule from '../pages/module'
Expand Down Expand Up @@ -71,6 +71,17 @@ async function initNuxt (nuxt: Nuxt) {
}
}

// Restart Nuxt when layer directories are added or removed
const layersDir = withTrailingSlash(resolve(nuxt.options.rootDir, 'layers'))
nuxt.hook('builder:watch', (event, relativePath) => {
const path = resolve(nuxt.options.srcDir, relativePath)
if (event === 'addDir' || event === 'unlinkDir') {
if (path.startsWith(layersDir)) {
return nuxt.callHook('restart', { hard: true })
}
}
})

// Set nuxt instance for useNuxt
nuxtCtx.set(nuxt)
nuxt.hook('close', () => nuxtCtx.unset())
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/basic/layers/bar/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default defineNuxtConfig({
modules: [
function (_options, nuxt) {
// @ts-expect-error not valid nuxt option
nuxt.options.__installed_layer = true
},
],
})
8 changes: 8 additions & 0 deletions test/fixtures/basic/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ export default defineNuxtConfig({
},
},
modules: [
function (_options, nuxt) {
nuxt.hook('modules:done', () => {
// @ts-expect-error not valid nuxt option
if (!nuxt.options.__installed_layer) {
throw new Error('layer in layers/ directory was not auto-registered')
}
})
},
'~/modules/subpath',
'./modules/test',
'~/modules/example',
Expand Down

0 comments on commit 06be4cc

Please sign in to comment.