From 345ba8b7261b710d7a112edebc459ac1b9a8e5df Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Mon, 5 Aug 2024 10:15:00 +0100 Subject: [PATCH 1/3] fix: skip glob files in content dir --- packages/astro/src/content/loaders/glob.ts | 39 +++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/packages/astro/src/content/loaders/glob.ts b/packages/astro/src/content/loaders/glob.ts index e0ce403209d6..c4e778493682 100644 --- a/packages/astro/src/content/loaders/glob.ts +++ b/packages/astro/src/content/loaders/glob.ts @@ -1,7 +1,7 @@ import { promises as fs } from 'node:fs'; import { fileURLToPath, pathToFileURL } from 'node:url'; import fastGlob from 'fast-glob'; -import { green } from 'kleur/colors'; +import { bold, green } from 'kleur/colors'; import micromatch from 'micromatch'; import pLimit from 'p-limit'; import type { ContentEntryRenderFuction, ContentEntryType } from '../../@types/astro.js'; @@ -190,14 +190,51 @@ export function glob(globOptions: GlobOptions): Loader { } const limit = pLimit(10); + const skippedFiles: Array = []; + + const contentDir = new URL('content/', settings.config.srcDir); + + function isInContentDir(file: string) { + const fileUrl = new URL(file, baseDir); + return fileUrl.href.startsWith(contentDir.href); + } + + function isConfigFile(file: string) { + const configFiles = ['config.js', 'config.ts', 'config.mjs']; + const fileUrl = new URL(file, baseDir); + return configFiles.some( + (configFile) => new URL(configFile, contentDir).href === fileUrl.href + ); + } + await Promise.all( files.map((entry) => limit(async () => { + if (isConfigFile(entry)) { + return; + } + if (isInContentDir(entry)) { + skippedFiles.push(entry); + return; + } const entryType = configForFile(entry); await syncData(entry, baseDir, entryType); }) ) ); + + const skipCount = skippedFiles.length; + + if (skipCount > 0) { + logger.warn(`The glob() loader cannot be used for files in ${bold('src/content')}.`); + if (skipCount > 10) { + logger.warn(`Skipped ${green(skippedFiles.length)} files.`); + } else { + logger.warn(`Skipped:`); + skippedFiles.forEach((file) => logger.warn(`• ${green(file)}`)); + } + } + // Remove entries that were not found this time untouchedEntries.forEach((id) => store.delete(id)); From 431efff1b8c6f9ed1f6a0b8d8035bfd58f0ed624 Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Mon, 5 Aug 2024 10:45:19 +0100 Subject: [PATCH 2/3] Changes from review --- packages/astro/src/content/loaders/glob.ts | 31 +++++++++++----------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/packages/astro/src/content/loaders/glob.ts b/packages/astro/src/content/loaders/glob.ts index c4e778493682..363857ea04a1 100644 --- a/packages/astro/src/content/loaders/glob.ts +++ b/packages/astro/src/content/loaders/glob.ts @@ -199,28 +199,29 @@ export function glob(globOptions: GlobOptions): Loader { return fileUrl.href.startsWith(contentDir.href); } + const configFiles = new Set( + ['config.js', 'config.ts', 'config.mjs'].map((file) => new URL(file, contentDir).href) + ); + function isConfigFile(file: string) { - const configFiles = ['config.js', 'config.ts', 'config.mjs']; const fileUrl = new URL(file, baseDir); - return configFiles.some( - (configFile) => new URL(configFile, contentDir).href === fileUrl.href - ); + return configFiles.has(fileUrl.href); } await Promise.all( - files.map((entry) => - limit(async () => { - if (isConfigFile(entry)) { - return; - } - if (isInContentDir(entry)) { - skippedFiles.push(entry); - return; - } + files.map((entry) => { + if (isConfigFile(entry)) { + return; + } + if (isInContentDir(entry)) { + skippedFiles.push(entry); + return; + } + return limit(async () => { const entryType = configForFile(entry); await syncData(entry, baseDir, entryType); - }) - ) + }); + }) ); const skipCount = skippedFiles.length; From 483ffdd48a4b1aef6a7b5c0dfd52cfc735d48eb1 Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Mon, 5 Aug 2024 10:48:34 +0100 Subject: [PATCH 3/3] Log pattern --- packages/astro/src/content/loaders/glob.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/astro/src/content/loaders/glob.ts b/packages/astro/src/content/loaders/glob.ts index 363857ea04a1..cf3f9971877b 100644 --- a/packages/astro/src/content/loaders/glob.ts +++ b/packages/astro/src/content/loaders/glob.ts @@ -229,9 +229,9 @@ export function glob(globOptions: GlobOptions): Loader { if (skipCount > 0) { logger.warn(`The glob() loader cannot be used for files in ${bold('src/content')}.`); if (skipCount > 10) { - logger.warn(`Skipped ${green(skippedFiles.length)} files.`); + logger.warn(`Skipped ${green(skippedFiles.length)} files that matched ${green(globOptions.pattern)}.`); } else { - logger.warn(`Skipped:`); + logger.warn(`Skipped the following files that matched ${green(globOptions.pattern)}:`); skippedFiles.forEach((file) => logger.warn(`• ${green(file)}`)); } }