Skip to content

Commit

Permalink
[Content collections] Remove "unsupported file type" warning (#9671)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people authored Jan 17, 2024
1 parent a5f1682 commit 8521ff7
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 73 deletions.
16 changes: 16 additions & 0 deletions .changeset/itchy-clouds-invite.md
Original file line number Diff line number Diff line change
@@ -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.
31 changes: 1 addition & 30 deletions packages/astro/src/content/types-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ type CreateContentGeneratorParams = {
fs: typeof fsMod;
};

class UnsupportedFileTypeError extends Error {}

export async function createContentTypesGenerator({
contentConfigObserver,
fs,
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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({
Expand Down
25 changes: 4 additions & 21 deletions packages/astro/src/content/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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';
Expand All @@ -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']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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');
});
});
});
});

0 comments on commit 8521ff7

Please sign in to comment.