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 1 commit
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
39 changes: 38 additions & 1 deletion 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,51 @@ 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);
}

function isConfigFile(file: string) {
const configFiles = ['config.js', 'config.ts', 'config.mjs'];
const fileUrl = new URL(file, baseDir);
return configFiles.some(
(configFile) => new URL(configFile, contentDir).href === fileUrl.href
ascorbic marked this conversation as resolved.
Show resolved Hide resolved
);
}

await Promise.all(
files.map((entry) =>
limit(async () => {
if (isConfigFile(entry)) {
return;
}
if (isInContentDir(entry)) {
skippedFiles.push(entry);
return;
}
ascorbic marked this conversation as resolved.
Show resolved Hide resolved
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.`);
} else {
logger.warn(`Skipped:`);
skippedFiles.forEach((file) => logger.warn(`• ${green(file)}`));
}
}

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

Expand Down
Loading