Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: skip glob files in content dir #11622

Merged
merged 3 commits into from
Aug 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 43 additions & 5 deletions packages/astro/src/content/loaders/glob.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { promises as fs } from 'node:fs';
import { fileURLToPath, pathToFileURL } from 'node:url';
import fastGlob from 'fast-glob';
import { green } from 'kleur/colors';
import { bold, green } from 'kleur/colors';
import micromatch from 'micromatch';
import pLimit from 'p-limit';
import type { ContentEntryRenderFuction, ContentEntryType } from '../../@types/astro.js';
Expand Down Expand Up @@ -190,14 +190,52 @@ export function glob(globOptions: GlobOptions): Loader {
}

const limit = pLimit(10);
const skippedFiles: Array<string> = [];

const contentDir = new URL('content/', settings.config.srcDir);

function isInContentDir(file: string) {
const fileUrl = new URL(file, baseDir);
return fileUrl.href.startsWith(contentDir.href);
}

const configFiles = new Set(
['config.js', 'config.ts', 'config.mjs'].map((file) => new URL(file, contentDir).href)
);

function isConfigFile(file: string) {
const fileUrl = new URL(file, baseDir);
return configFiles.has(fileUrl.href);
}

await Promise.all(
files.map((entry) =>
limit(async () => {
files.map((entry) => {
if (isConfigFile(entry)) {
return;
}
if (isInContentDir(entry)) {
skippedFiles.push(entry);
return;
}
return limit(async () => {
const entryType = configForFile(entry);
await syncData(entry, baseDir, entryType);
})
)
});
})
);

const skipCount = skippedFiles.length;

if (skipCount > 0) {
logger.warn(`The glob() loader cannot be used for files in ${bold('src/content')}.`);
ascorbic marked this conversation as resolved.
Show resolved Hide resolved
if (skipCount > 10) {
logger.warn(`Skipped ${green(skippedFiles.length)} files that matched ${green(globOptions.pattern)}.`);
} else {
logger.warn(`Skipped the following files that matched ${green(globOptions.pattern)}:`);
skippedFiles.forEach((file) => logger.warn(`• ${green(file)}`));
}
}

// Remove entries that were not found this time
untouchedEntries.forEach((id) => store.delete(id));

Expand Down
Loading