0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

[Content collections] Remove "unsupported file type" warning (#9671)

* feat: remove "unsupported file type" warning for CC

* chore(test): remove unsupported file type unit

* chore: remove unused imports

* chore: changeset

* chore: changeset edits

* edit: add note on underscores to exclude content

Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>

* edit: front-load "removes the requirement"

Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>

---------

Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
This commit is contained in:
Ben Holmes 2024-01-17 08:12:37 -05:00 committed by GitHub
parent a5f1682347
commit 8521ff77fb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 27 additions and 73 deletions

View file

@ -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.

View file

@ -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({

View file

@ -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<ContentPaths, 'config' | 'contentDir'>,
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']

View file

@ -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');
});
});
});
});