Skip to content

Commit 2579910

Browse files
authored
fix(module): invalid rootDir of layer sources (#3308)
1 parent a01c764 commit 2579910

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

src/module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,9 @@ async function processCollectionItems(nuxt: Nuxt, collections: ResolvedCollectio
272272

273273
for await (const source of collection.source) {
274274
if (source.prepare) {
275-
await source.prepare({ rootDir: nuxt.options.rootDir })
275+
// @ts-expect-error - `__rootDir` is a private property to store the layer's cwd
276+
const rootDir = collection.__rootDir || nuxt.options.rootDir
277+
await source.prepare({ rootDir })
276278
}
277279

278280
const { fixed } = parseSourceBase(source)

src/utils/config.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const defaultConfig: NuxtContentConfig = {
2020

2121
export const defineContentConfig = createDefineConfig<NuxtContentConfig>()
2222

23-
export async function loadContentConfig(nuxt: Nuxt, options: { defaultFallback?: boolean } = {}) {
23+
export async function loadContentConfig(nuxt: Nuxt) {
2424
const loader: typeof watchConfig = nuxt.options.dev
2525
? opts => watchConfig({
2626
...opts,
@@ -37,7 +37,7 @@ export async function loadContentConfig(nuxt: Nuxt, options: { defaultFallback?:
3737
const layers = [...nuxt.options._layers].reverse()
3838
const contentConfigs = await Promise.all(
3939
layers.map(
40-
layer => loader<NuxtContentConfig>({ name: 'content', cwd: layer.config.rootDir, defaultConfig: options.defaultFallback ? defaultConfig : undefined }),
40+
layer => loader<NuxtContentConfig>({ name: 'content', cwd: layer.config.rootDir, defaultConfig: { collections: {} } }),
4141
),
4242
)
4343

@@ -48,9 +48,21 @@ export async function loadContentConfig(nuxt: Nuxt, options: { defaultFallback?:
4848
nuxt.hook('close', () => Promise.all(contentConfigs.map(c => c.unwatch())).then(() => {}))
4949
}
5050

51-
const collectionsConfig = contentConfigs.reduce((acc, curr) => ({ ...acc, ...curr.config?.collections }), {} as Record<string, DefinedCollection>)
52-
const hasNoCollections = Object.keys(collectionsConfig || {}).length === 0
51+
const collectionsConfig = contentConfigs.reduce((acc, curr) => {
52+
const layerCollections = curr.config?.collections || {}
53+
const cwd = curr.cwd!
54+
55+
Object.entries(layerCollections).forEach(([name, collection]) => {
56+
// @ts-expect-error - `__rootDir` is a private property to store the layer's cwd
57+
collection.__rootDir = cwd
58+
59+
acc[name] = collection
60+
})
61+
62+
return acc
63+
}, {} as Record<string, DefinedCollection>)
5364

65+
const hasNoCollections = Object.keys(collectionsConfig || {}).length === 0
5466
if (hasNoCollections) {
5567
logger.warn('No content configuration found, falling back to default collection. In order to have full control over your collections, create the config file in project root. See: https://content.nuxt.com/docs/getting-started/installation')
5668
}

test/basic.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@ describe('basic', async () => {
2525
rootDir: fileURLToPath(new URL('./fixtures/basic', import.meta.url)),
2626
dev: true,
2727
})
28-
console.log('setup')
2928

3029
describe('`content.config.ts`', async () => {
3130
test('Default collection is defined', async () => {
3231
const rootDir = fileURLToPath(new URL('./fixtures/basic', import.meta.url))
33-
const config = await loadContentConfig({ options: { _layers: [{ config: { rootDir } }] } } as Nuxt, { defaultFallback: true })
32+
const config = await loadContentConfig({ options: { _layers: [{ config: { rootDir } }] } } as Nuxt)
3433

3534
// Pages collection + info collection
3635
expect(config.collections.length).toBe(2)

test/empty.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('empty', async () => {
3535
})
3636
test('Default collection is defined', async () => {
3737
const rootDir = fileURLToPath(new URL('./fixtures/empty', import.meta.url))
38-
const config = await loadContentConfig({ options: { _layers: [{ config: { rootDir } }] } } as Nuxt, { defaultFallback: true })
38+
const config = await loadContentConfig({ options: { _layers: [{ config: { rootDir } }] } } as Nuxt)
3939

4040
// Pages collection + info collection
4141
expect(config.collections.length).toBe(2)

0 commit comments

Comments
 (0)