From 9e7366567e2b83d46a46db35e74ad508d1978039 Mon Sep 17 00:00:00 2001 From: Ben Holmes Date: Fri, 2 Jun 2023 07:54:50 -0400 Subject: [PATCH] Content collections: ignored underscore directories in file glob (#7268) * fix: ignored underscore dirs in glob * chore: changeset --- .changeset/rotten-pens-destroy.md | 5 ++++ .../vite-plugin-content-virtual-mod.ts | 24 ++++++++++++++----- 2 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 .changeset/rotten-pens-destroy.md diff --git a/.changeset/rotten-pens-destroy.md b/.changeset/rotten-pens-destroy.md new file mode 100644 index 000000000..c8fb40872 --- /dev/null +++ b/.changeset/rotten-pens-destroy.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix: ignore `.json` files within content collection directories starting with an `_` underscore. diff --git a/packages/astro/src/content/vite-plugin-content-virtual-mod.ts b/packages/astro/src/content/vite-plugin-content-virtual-mod.ts index 838d6b425..bd516b199 100644 --- a/packages/astro/src/content/vite-plugin-content-virtual-mod.ts +++ b/packages/astro/src/content/vite-plugin-content-virtual-mod.ts @@ -44,14 +44,21 @@ export function astroContentVirtualModPlugin({ ) .replace('@@CONTENT_DIR@@', relContentDir) .replace( - '@@CONTENT_ENTRY_GLOB_PATH@@', - // [!_] = ignore files starting with "_" - `${relContentDir}**/[!_]*${getExtGlob(contentEntryExts)}` + "'@@CONTENT_ENTRY_GLOB_PATH@@'", + JSON.stringify(globWithUnderscoresIgnored(relContentDir, contentEntryExts)) ) - .replace('@@DATA_ENTRY_GLOB_PATH@@', `${relContentDir}**/[!_]*${getExtGlob(dataEntryExts)}`) .replace( - '@@RENDER_ENTRY_GLOB_PATH@@', - `${relContentDir}**/*${getExtGlob(/** Note: data collections excluded */ contentEntryExts)}` + "'@@DATA_ENTRY_GLOB_PATH@@'", + JSON.stringify(globWithUnderscoresIgnored(relContentDir, dataEntryExts)) + ) + .replace( + "'@@RENDER_ENTRY_GLOB_PATH@@'", + JSON.stringify( + globWithUnderscoresIgnored( + relContentDir, + /** Note: data collections excluded */ contentEntryExts + ) + ) ); const astroContentVirtualModuleId = '\0' + VIRTUAL_MODULE_ID; @@ -189,3 +196,8 @@ const UnexpectedLookupMapError = new AstroError({ ...AstroErrorData.UnknownContentCollectionError, message: `Unexpected error while parsing content entry IDs and slugs.`, }); + +function globWithUnderscoresIgnored(relContentDir: string, exts: string[]): string[] { + const extGlob = getExtGlob(exts); + return [`${relContentDir}/**/*${extGlob}`, `!**/_*/**${extGlob}`, `!**/_*${extGlob}`]; +}