diff --git a/.changeset/itchy-clouds-invite.md b/.changeset/itchy-clouds-invite.md new file mode 100644 index 0000000000..67dc73f086 --- /dev/null +++ b/.changeset/itchy-clouds-invite.md @@ -0,0 +1,16 @@ +--- +"astro": minor +--- + +Removes the requirement for non-content files and assets inside content collections to be prefixed with an underscore. For files with extensions like `.astro` or `.css`, you can now remove underscores without seeing a warning in the terminal. + +```diff +src/content/blog/ +post.mdx +- _styles.css +- _Component.astro ++ styles.css ++ Component.astro +``` + +Continue to use underscores in your content collections to exclude individual content files, such as drafts, from the build output. diff --git a/packages/astro/src/content/types-generator.ts b/packages/astro/src/content/types-generator.ts index 82300f9c77..4d466f5942 100644 --- a/packages/astro/src/content/types-generator.ts +++ b/packages/astro/src/content/types-generator.ts @@ -56,8 +56,6 @@ type CreateContentGeneratorParams = { fs: typeof fsMod; }; -class UnsupportedFileTypeError extends Error {} - export async function createContentTypesGenerator({ contentConfigObserver, fs, @@ -109,9 +107,7 @@ export async function createContentTypesGenerator({ return { typesGenerated: true }; } - async function handleEvent( - event: ContentEvent - ): Promise<{ shouldGenerateTypes: boolean; error?: Error }> { + async function handleEvent(event: ContentEvent): Promise<{ shouldGenerateTypes: boolean }> { if (event.name === 'addDir' || event.name === 'unlinkDir') { const collection = normalizePath( path.relative(fileURLToPath(contentPaths.contentDir), fileURLToPath(event.entry)) @@ -147,21 +143,6 @@ export async function createContentTypesGenerator({ await reloadContentConfigObserver({ fs, settings, viteServer }); return { shouldGenerateTypes: true }; } - if (fileType === 'unsupported') { - // Avoid warning if file was deleted. - if (event.name === 'unlink') { - return { shouldGenerateTypes: false }; - } - const { id } = getContentEntryIdAndSlug({ - entry: event.entry, - contentDir: contentPaths.contentDir, - collection: '', - }); - return { - shouldGenerateTypes: false, - error: new UnsupportedFileTypeError(id), - }; - } const { entry } = event; const { contentDir } = contentPaths; @@ -319,16 +300,6 @@ export async function createContentTypesGenerator({ } events = []; - for (const response of eventResponses) { - if (response.error instanceof UnsupportedFileTypeError) { - logger.warn( - 'content', - `Unsupported file type ${bold( - response.error.message - )} found. Prefix filename with an underscore (\`_\`) to ignore.` - ); - } - } const observable = contentConfigObserver.get(); if (eventResponses.some((r) => r.shouldGenerateTypes)) { await writeContentFiles({ diff --git a/packages/astro/src/content/utils.ts b/packages/astro/src/content/utils.ts index f6f3dae1ca..3515a43c72 100644 --- a/packages/astro/src/content/utils.ts +++ b/packages/astro/src/content/utils.ts @@ -11,9 +11,7 @@ import type { AstroSettings, ContentEntryType, DataEntryType, - ImageInputFormat, } from '../@types/astro.js'; -import { VALID_INPUT_FORMATS } from '../assets/consts.js'; import { AstroError, AstroErrorData } from '../core/errors/index.js'; import { formatYAMLException, isYAMLException } from '../core/errors/utils.js'; @@ -247,15 +245,11 @@ export function getEntryType( paths: Pick, contentFileExts: string[], dataFileExts: string[] -): 'content' | 'data' | 'config' | 'ignored' | 'unsupported' { - const { ext, base } = path.parse(entryPath); +): 'content' | 'data' | 'config' | 'ignored' { + const { ext } = path.parse(entryPath); const fileUrl = pathToFileURL(entryPath); - if ( - hasUnderscoreBelowContentDirectoryPath(fileUrl, paths.contentDir) || - isOnIgnoreList(base) || - isImageAsset(ext) - ) { + if (hasUnderscoreBelowContentDirectoryPath(fileUrl, paths.contentDir)) { return 'ignored'; } else if (contentFileExts.includes(ext)) { return 'content'; @@ -264,21 +258,10 @@ export function getEntryType( } else if (fileUrl.href === paths.config.url.href) { return 'config'; } else { - return 'unsupported'; + return 'ignored'; } } -function isOnIgnoreList(fileName: string) { - return ['.DS_Store'].includes(fileName); -} - -/** - * Return if a file extension is a valid image asset, so we can avoid outputting a warning for them. - */ -function isImageAsset(fileExt: string) { - return VALID_INPUT_FORMATS.includes(fileExt.slice(1) as ImageInputFormat); -} - export function hasUnderscoreBelowContentDirectoryPath( fileUrl: URL, contentDir: ContentPaths['contentDir'] diff --git a/packages/astro/test/units/content-collections/get-entry-type.test.js b/packages/astro/test/units/content-collections/get-entry-type.test.js index a953d0c8fe..a9c4e3c39e 100644 --- a/packages/astro/test/units/content-collections/get-entry-type.test.js +++ b/packages/astro/test/units/content-collections/get-entry-type.test.js @@ -65,22 +65,12 @@ describe('Content Collections - getEntryType', () => { expect(type).to.equal('config'); }); - it('Returns "unsupported" for non-Markdown files', () => { - const entry = fileURLToPath(new URL('blog/robots.txt', contentPaths.contentDir)); - const type = getEntryType(entry, contentPaths, contentFileExts, dataFileExts); - expect(type).to.equal('unsupported'); - }); - - it('Returns "ignored" for .DS_Store', () => { - const entry = fileURLToPath(new URL('blog/.DS_Store', contentPaths.contentDir)); - const type = getEntryType(entry, contentPaths, contentFileExts, dataFileExts); - expect(type).to.equal('ignored'); - }); - - it('Returns "ignored" for unsupported files using an underscore', () => { - const entry = fileURLToPath(new URL('blog/_draft-robots.txt', contentPaths.contentDir)); - const type = getEntryType(entry, contentPaths, contentFileExts, dataFileExts); - expect(type).to.equal('ignored'); + it('Returns "ignored" for non-Markdown files', () => { + for (const entryPath of ['blog/robots.txt', 'blog/first-post.png', '.DS_Store']) { + const entry = fileURLToPath(new URL(entryPath, contentPaths.contentDir)); + const type = getEntryType(entry, contentPaths, contentFileExts, dataFileExts); + expect(type).to.equal('ignored'); + } }); it('Returns "ignored" when using underscore on file name', () => { @@ -94,12 +84,6 @@ describe('Content Collections - getEntryType', () => { const type = getEntryType(entry, contentPaths, contentFileExts, dataFileExts); expect(type).to.equal('ignored'); }); - - it('Returns "ignored" for images', () => { - const entry = fileURLToPath(new URL('blog/first-post.png', contentPaths.contentDir)); - const type = getEntryType(entry, contentPaths, contentFileExts, dataFileExts); - expect(type).to.equal('ignored'); - }); }); }); });