0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -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; fs: typeof fsMod;
}; };
class UnsupportedFileTypeError extends Error {}
export async function createContentTypesGenerator({ export async function createContentTypesGenerator({
contentConfigObserver, contentConfigObserver,
fs, fs,
@ -109,9 +107,7 @@ export async function createContentTypesGenerator({
return { typesGenerated: true }; return { typesGenerated: true };
} }
async function handleEvent( async function handleEvent(event: ContentEvent): Promise<{ shouldGenerateTypes: boolean }> {
event: ContentEvent
): Promise<{ shouldGenerateTypes: boolean; error?: Error }> {
if (event.name === 'addDir' || event.name === 'unlinkDir') { if (event.name === 'addDir' || event.name === 'unlinkDir') {
const collection = normalizePath( const collection = normalizePath(
path.relative(fileURLToPath(contentPaths.contentDir), fileURLToPath(event.entry)) path.relative(fileURLToPath(contentPaths.contentDir), fileURLToPath(event.entry))
@ -147,21 +143,6 @@ export async function createContentTypesGenerator({
await reloadContentConfigObserver({ fs, settings, viteServer }); await reloadContentConfigObserver({ fs, settings, viteServer });
return { shouldGenerateTypes: true }; 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 { entry } = event;
const { contentDir } = contentPaths; const { contentDir } = contentPaths;
@ -319,16 +300,6 @@ export async function createContentTypesGenerator({
} }
events = []; 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(); const observable = contentConfigObserver.get();
if (eventResponses.some((r) => r.shouldGenerateTypes)) { if (eventResponses.some((r) => r.shouldGenerateTypes)) {
await writeContentFiles({ await writeContentFiles({

View file

@ -11,9 +11,7 @@ import type {
AstroSettings, AstroSettings,
ContentEntryType, ContentEntryType,
DataEntryType, DataEntryType,
ImageInputFormat,
} from '../@types/astro.js'; } from '../@types/astro.js';
import { VALID_INPUT_FORMATS } from '../assets/consts.js';
import { AstroError, AstroErrorData } from '../core/errors/index.js'; import { AstroError, AstroErrorData } from '../core/errors/index.js';
import { formatYAMLException, isYAMLException } from '../core/errors/utils.js'; import { formatYAMLException, isYAMLException } from '../core/errors/utils.js';
@ -247,15 +245,11 @@ export function getEntryType(
paths: Pick<ContentPaths, 'config' | 'contentDir'>, paths: Pick<ContentPaths, 'config' | 'contentDir'>,
contentFileExts: string[], contentFileExts: string[],
dataFileExts: string[] dataFileExts: string[]
): 'content' | 'data' | 'config' | 'ignored' | 'unsupported' { ): 'content' | 'data' | 'config' | 'ignored' {
const { ext, base } = path.parse(entryPath); const { ext } = path.parse(entryPath);
const fileUrl = pathToFileURL(entryPath); const fileUrl = pathToFileURL(entryPath);
if ( if (hasUnderscoreBelowContentDirectoryPath(fileUrl, paths.contentDir)) {
hasUnderscoreBelowContentDirectoryPath(fileUrl, paths.contentDir) ||
isOnIgnoreList(base) ||
isImageAsset(ext)
) {
return 'ignored'; return 'ignored';
} else if (contentFileExts.includes(ext)) { } else if (contentFileExts.includes(ext)) {
return 'content'; return 'content';
@ -264,21 +258,10 @@ export function getEntryType(
} else if (fileUrl.href === paths.config.url.href) { } else if (fileUrl.href === paths.config.url.href) {
return 'config'; return 'config';
} else { } 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( export function hasUnderscoreBelowContentDirectoryPath(
fileUrl: URL, fileUrl: URL,
contentDir: ContentPaths['contentDir'] contentDir: ContentPaths['contentDir']

View file

@ -65,22 +65,12 @@ describe('Content Collections - getEntryType', () => {
expect(type).to.equal('config'); expect(type).to.equal('config');
}); });
it('Returns "unsupported" for non-Markdown files', () => { it('Returns "ignored" for non-Markdown files', () => {
const entry = fileURLToPath(new URL('blog/robots.txt', contentPaths.contentDir)); for (const entryPath of ['blog/robots.txt', 'blog/first-post.png', '.DS_Store']) {
const type = getEntryType(entry, contentPaths, contentFileExts, dataFileExts); const entry = fileURLToPath(new URL(entryPath, contentPaths.contentDir));
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); const type = getEntryType(entry, contentPaths, contentFileExts, dataFileExts);
expect(type).to.equal('ignored'); expect(type).to.equal('ignored');
}
}); });
it('Returns "ignored" when using underscore on file name', () => { it('Returns "ignored" when using underscore on file name', () => {
@ -94,12 +84,6 @@ describe('Content Collections - getEntryType', () => {
const type = getEntryType(entry, contentPaths, contentFileExts, dataFileExts); const type = getEntryType(entry, contentPaths, contentFileExts, dataFileExts);
expect(type).to.equal('ignored'); 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');
});
}); });
}); });
}); });