1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
/**
* This file is edited from astro/loaders/glob.js
*/
import { existsSync, promises as fs } from "node:fs";
import { relative } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import pLimit from "p-limit";
import colors from "piccolore";
import picomatch from "picomatch";
import { glob as tinyglobby } from "tinyglobby";
import {
getContentEntryIdAndSlug,
posixRelative,
} from "../../node_modules/astro/dist/content/utils.js";
function generateIdDefault({ entry, base, data }, postprocessSlug, isLegacy) {
if (data.slug) {
return data.slug;
}
const entryURL = new URL(encodeURI(entry), base);
if (isLegacy) {
const { id } = getContentEntryIdAndSlug({
entry: entryURL,
contentDir: base,
collection: "",
});
return id;
}
const { slug } = getContentEntryIdAndSlug({
entry: entryURL,
contentDir: base,
collection: "",
});
if (postprocessSlug) {
return postprocessSlug(slug);
}
return slug;
}
function checkPrefix(pattern, prefix) {
if (Array.isArray(pattern)) {
return pattern.some((p) => p.startsWith(prefix));
}
return pattern.startsWith(prefix);
}
const secretLegacyFlag = /* @__PURE__ */ Symbol("astro.legacy-glob");
function extendedGlob(globOptions) {
if (checkPrefix(globOptions.pattern, "../")) {
throw new Error(
"Glob patterns cannot start with `../`. Set the `base` option to a parent directory instead.",
);
}
if (checkPrefix(globOptions.pattern, "/")) {
throw new Error(
"Glob patterns cannot start with `/`. Set the `base` option to a parent directory or use a relative path instead.",
);
}
const isLegacy = !!globOptions[secretLegacyFlag];
const generateId =
globOptions?.generateId ??
((opts) => generateIdDefault(opts, globOptions?.postprocessSlug, isLegacy));
const fileToIdMap = /* @__PURE__ */ new Map();
const ignore = globOptions.ignore || [];
return {
name: "extended-glob-loader",
load: async ({
config,
collection,
logger,
watcher,
parseData,
store,
generateDigest,
entryTypes,
}) => {
const renderFunctionByContentType = /* @__PURE__ */ new WeakMap();
const untouchedEntries = new Set(store.keys());
async function syncData(entry, base, entryType, oldId) {
if (!entryType) {
logger.warn(`No entry type found for ${entry}`);
return;
}
const fileUrl = new URL(encodeURI(entry), base);
const contents = await fs.readFile(fileUrl, "utf-8").catch((err) => {
logger.error(`Error reading ${entry}: ${err.message}`);
return;
});
if (!contents && contents !== "") {
logger.warn(`No contents found for ${entry}`);
return;
}
const { body, data } = await entryType.getEntryInfo({
contents,
fileUrl,
});
const id = generateId({ entry, base, data });
if (oldId && oldId !== id) {
store.delete(oldId);
}
untouchedEntries.delete(id);
const existingEntry = store.get(id);
const digest = generateDigest(contents);
const filePath2 = fileURLToPath(fileUrl);
if (
existingEntry &&
existingEntry.digest === digest &&
existingEntry.filePath
) {
if (existingEntry.deferredRender) {
store.addModuleImport(existingEntry.filePath);
}
if (existingEntry.assetImports?.length) {
store.addAssetImports(
existingEntry.assetImports,
existingEntry.filePath,
);
}
fileToIdMap.set(filePath2, id);
return;
}
const relativePath2 = posixRelative(
fileURLToPath(config.root),
filePath2,
);
const parsedData = await parseData({
id,
data,
filePath: filePath2,
});
if (
existingEntry &&
existingEntry.filePath &&
existingEntry.filePath !== relativePath2
) {
const oldFilePath = new URL(existingEntry.filePath, config.root);
if (existsSync(oldFilePath)) {
logger.warn(
`Duplicate id "${id}" found in ${filePath2}. Later items with the same id will overwrite earlier ones.`,
);
}
}
if (entryType.getRenderFunction) {
let render = renderFunctionByContentType.get(entryType);
if (!render) {
render = await entryType.getRenderFunction(config);
renderFunctionByContentType.set(entryType, render);
}
let rendered = void 0;
try {
rendered = await render?.({
id,
data,
body,
filePath: filePath2,
digest,
});
} catch (error) {
logger.error(`Error rendering ${entry}: ${error.message}`);
}
store.set({
id,
data: parsedData,
body: globOptions.retainBody === false ? void 0 : body,
filePath: relativePath2,
digest,
rendered,
assetImports: rendered?.metadata?.imagePaths,
});
} else if ("contentModuleTypes" in entryType) {
store.set({
id,
data: parsedData,
body: globOptions.retainBody === false ? void 0 : body,
filePath: relativePath2,
digest,
deferredRender: true,
});
} else {
store.set({
id,
data: parsedData,
body: globOptions.retainBody === false ? void 0 : body,
filePath: relativePath2,
digest,
});
}
fileToIdMap.set(filePath2, id);
}
let baseDir;
if (isLegacy && !globOptions.base) {
baseDir = new URL(`./src/content/${collection}`, config.root);
} else {
baseDir = globOptions.base
? new URL(globOptions.base, config.root)
: config.root;
}
if (!baseDir.pathname.endsWith("/")) {
baseDir.pathname = `${baseDir.pathname}/`;
}
const filePath = fileURLToPath(baseDir);
const relativePath = relative(fileURLToPath(config.root), filePath);
const exists = existsSync(baseDir);
if (!exists) {
logger.warn(
`The base directory "${fileURLToPath(baseDir)}" does not exist.`,
);
}
const files = await tinyglobby(globOptions.pattern, {
cwd: fileURLToPath(baseDir),
expandDirectories: false,
ignore,
});
if (exists && files.length === 0) {
logger.warn(
`No files found matching "${globOptions.pattern}" in directory "${relativePath}"`,
);
return;
}
function configForFile(file) {
const ext = file.split(".").at(-1);
if (!ext) {
logger.warn(`No extension found for ${file}`);
return;
}
return entryTypes.get(`.${ext}`);
}
const limit = pLimit(10);
const skippedFiles = [];
const contentDir = new URL("content/", config.srcDir);
const configFiles = new Set(
["config.js", "config.ts", "config.mjs"].map(
(file) => new URL(file, contentDir).href,
),
);
function isConfigFile(file) {
const fileUrl = new URL(file, baseDir);
return configFiles.has(fileUrl.href);
}
await Promise.all(
files.map((entry) => {
if (isConfigFile(entry)) {
return;
}
return limit(async () => {
const entryType = configForFile(entry);
await syncData(entry, baseDir, entryType);
});
}),
);
const skipCount = skippedFiles.length;
if (skipCount > 0) {
const patternList = Array.isArray(globOptions.pattern)
? globOptions.pattern.join(", ")
: globOptions.pattern;
logger.warn(
`The glob() loader cannot be used for files in ${colors.bold("src/content")} when legacy mode is enabled.`,
);
if (skipCount > 10) {
logger.warn(
`Skipped ${colors.green(skippedFiles.length)} files that matched ${colors.green(patternList)}.`,
);
} else {
logger.warn(
`Skipped the following files that matched ${colors.green(patternList)}:`,
);
skippedFiles.forEach((file) =>
logger.warn(`\u2022 ${colors.green(file)}`),
);
}
}
untouchedEntries.forEach((id) => store.delete(id));
if (!watcher) {
return;
}
watcher.add(filePath);
const matchesGlob = (entry) =>
!entry.startsWith("../") &&
picomatch.isMatch(entry, globOptions.pattern);
const basePath = fileURLToPath(baseDir);
async function onChange(changedPath) {
const entry = posixRelative(basePath, changedPath);
if (!matchesGlob(entry)) {
return;
}
const entryType = configForFile(changedPath);
const baseUrl = pathToFileURL(basePath);
const oldId = fileToIdMap.get(changedPath);
await syncData(entry, baseUrl, entryType, oldId);
logger.info(`Reloaded data from ${colors.green(entry)}`);
}
watcher.on("change", onChange);
watcher.on("add", onChange);
watcher.on("unlink", async (deletedPath) => {
const entry = posixRelative(basePath, deletedPath);
if (!matchesGlob(entry)) {
return;
}
const id = fileToIdMap.get(deletedPath);
if (id) {
store.delete(id);
fileToIdMap.delete(deletedPath);
}
});
},
};
}
export { extendedGlob, secretLegacyFlag };
|