/
create.ts
426 lines (372 loc) · 11.6 KB
/
create.ts
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import type {
AstroConfig,
AstroSettings,
InjectedRoute,
ManifestData,
RouteData,
RoutePart,
} from '../../../@types/astro';
import type { LogOptions } from '../../logger/core';
import nodeFs from 'fs';
import { createRequire } from 'module';
import path from 'path';
import slash from 'slash';
import { fileURLToPath } from 'url';
import { isHybridOutput } from '../../../prerender/utils.js';
import { SUPPORTED_MARKDOWN_FILE_EXTENSIONS } from '../../constants.js';
import { warn } from '../../logger/core.js';
import { removeLeadingForwardSlash } from '../../path.js';
import { resolvePages } from '../../util.js';
import { getRouteGenerator } from './generator.js';
const require = createRequire(import.meta.url);
interface Item {
basename: string;
ext: string;
parts: RoutePart[];
file: string;
isDir: boolean;
isIndex: boolean;
isPage: boolean;
routeSuffix: string;
}
function countOccurrences(needle: string, haystack: string) {
let count = 0;
for (let i = 0; i < haystack.length; i += 1) {
if (haystack[i] === needle) count += 1;
}
return count;
}
function getParts(part: string, file: string) {
const result: RoutePart[] = [];
part.split(/\[(.+?\(.+?\)|.+?)\]/).map((str, i) => {
if (!str) return;
const dynamic = i % 2 === 1;
const [, content] = dynamic ? /([^(]+)$/.exec(str) || [null, null] : [null, str];
if (!content || (dynamic && !/^(\.\.\.)?[a-zA-Z0-9_$]+$/.test(content))) {
throw new Error(`Invalid route ${file} — parameter name must match /^[a-zA-Z0-9_$]+$/`);
}
result.push({
content,
dynamic,
spread: dynamic && /^\.{3}.+$/.test(content),
});
});
return result;
}
function getPattern(
segments: RoutePart[][],
base: string,
addTrailingSlash: AstroConfig['trailingSlash']
) {
const pathname = segments
.map((segment) => {
if (segment.length === 1 && segment[0].spread) {
return '(?:\\/(.*?))?';
} else {
return (
'\\/' +
segment
.map((part) => {
if (part.spread) {
return '(.*?)';
} else if (part.dynamic) {
return '([^/]+?)';
} else {
return part.content
.normalize()
.replace(/\?/g, '%3F')
.replace(/#/g, '%23')
.replace(/%5B/g, '[')
.replace(/%5D/g, ']')
.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
})
.join('')
);
}
})
.join('');
const trailing =
addTrailingSlash && segments.length ? getTrailingSlashPattern(addTrailingSlash) : '$';
let initial = '\\/';
if (addTrailingSlash === 'never' && base !== '/') {
initial = '';
}
return new RegExp(`^${pathname || initial}${trailing}`);
}
function getTrailingSlashPattern(addTrailingSlash: AstroConfig['trailingSlash']): string {
if (addTrailingSlash === 'always') {
return '\\/$';
}
if (addTrailingSlash === 'never') {
return '$';
}
return '\\/?$';
}
function isSpread(str: string) {
const spreadPattern = /\[\.{3}/g;
return spreadPattern.test(str);
}
function validateSegment(segment: string, file = '') {
if (!file) file = segment;
if (/\]\[/.test(segment)) {
throw new Error(`Invalid route ${file} \u2014 parameters must be separated`);
}
if (countOccurrences('[', segment) !== countOccurrences(']', segment)) {
throw new Error(`Invalid route ${file} \u2014 brackets are unbalanced`);
}
if (
(/.+\[\.\.\.[^\]]+\]/.test(segment) || /\[\.\.\.[^\]]+\].+/.test(segment)) &&
file.endsWith('.astro')
) {
throw new Error(`Invalid route ${file} \u2014 rest parameter must be a standalone segment`);
}
}
function comparator(a: Item, b: Item) {
if (a.isIndex !== b.isIndex) {
if (a.isIndex) return isSpread(a.file) ? 1 : -1;
return isSpread(b.file) ? -1 : 1;
}
const max = Math.max(a.parts.length, b.parts.length);
for (let i = 0; i < max; i += 1) {
const aSubPart = a.parts[i];
const bSubPart = b.parts[i];
if (!aSubPart) return 1; // b is more specific, so goes first
if (!bSubPart) return -1;
// if spread && index, order later
if (aSubPart.spread && bSubPart.spread) {
return a.isIndex ? 1 : -1;
}
// If one is ...spread order it later
if (aSubPart.spread !== bSubPart.spread) return aSubPart.spread ? 1 : -1;
if (aSubPart.dynamic !== bSubPart.dynamic) {
return aSubPart.dynamic ? 1 : -1;
}
if (!aSubPart.dynamic && aSubPart.content !== bSubPart.content) {
return (
bSubPart.content.length - aSubPart.content.length ||
(aSubPart.content < bSubPart.content ? -1 : 1)
);
}
}
if (a.isPage !== b.isPage) {
return a.isPage ? 1 : -1;
}
// otherwise sort alphabetically
return a.file < b.file ? -1 : 1;
}
function injectedRouteToItem(
{ config, cwd }: { config: AstroConfig; cwd?: string },
{ pattern, entryPoint }: InjectedRoute
): Item {
const resolved = require.resolve(entryPoint, { paths: [cwd || fileURLToPath(config.root)] });
const ext = path.extname(pattern);
const type = resolved.endsWith('.astro') ? 'page' : 'endpoint';
const isPage = type === 'page';
return {
basename: pattern,
ext,
parts: getParts(pattern, resolved),
file: resolved,
isDir: false,
isIndex: true,
isPage,
routeSuffix: pattern.slice(pattern.indexOf('.'), -ext.length),
};
}
export interface CreateRouteManifestParams {
/** Astro Settings object */
settings: AstroSettings;
/** Current working directory */
cwd?: string;
/** fs module, for testing */
fsMod?: typeof nodeFs;
}
/** Create manifest of all static routes */
export function createRouteManifest(
{ settings, cwd, fsMod }: CreateRouteManifestParams,
logging: LogOptions
): ManifestData {
const components: string[] = [];
const routes: RouteData[] = [];
const validPageExtensions: Set<string> = new Set([
'.astro',
...SUPPORTED_MARKDOWN_FILE_EXTENSIONS,
...settings.pageExtensions,
]);
const validEndpointExtensions: Set<string> = new Set(['.js', '.ts']);
const localFs = fsMod ?? nodeFs;
const isPrerenderDefault = isHybridOutput(settings.config);
const foundInvalidFileExtensions: Set<string> = new Set();
function walk(
fs: typeof nodeFs,
dir: string,
parentSegments: RoutePart[][],
parentParams: string[]
) {
let items: Item[] = [];
fs.readdirSync(dir).forEach((basename) => {
const resolved = path.join(dir, basename);
const file = slash(path.relative(cwd || fileURLToPath(settings.config.root), resolved));
const isDir = fs.statSync(resolved).isDirectory();
const ext = path.extname(basename);
const name = ext ? basename.slice(0, -ext.length) : basename;
if (name[0] === '_') {
return;
}
if (basename[0] === '.' && basename !== '.well-known') {
return;
}
// filter out "foo.astro_tmp" files, etc
if (!isDir && !validPageExtensions.has(ext) && !validEndpointExtensions.has(ext)) {
if (!foundInvalidFileExtensions.has(ext)) {
foundInvalidFileExtensions.add(ext);
warn(logging, 'astro', `Invalid file extension for Pages: ${ext}`);
}
return;
}
const segment = isDir ? basename : name;
validateSegment(segment, file);
const parts = getParts(segment, file);
const isIndex = isDir ? false : basename.startsWith('index.');
const routeSuffix = basename.slice(basename.indexOf('.'), -ext.length);
const isPage = validPageExtensions.has(ext);
items.push({
basename,
ext,
parts,
file: file.replace(/\\/g, '/'),
isDir,
isIndex,
isPage,
routeSuffix,
});
});
items = items.sort(comparator);
items.forEach((item) => {
const segments = parentSegments.slice();
if (item.isIndex) {
if (item.routeSuffix) {
if (segments.length > 0) {
const lastSegment = segments[segments.length - 1].slice();
const lastPart = lastSegment[lastSegment.length - 1];
if (lastPart.dynamic) {
lastSegment.push({
dynamic: false,
spread: false,
content: item.routeSuffix,
});
} else {
lastSegment[lastSegment.length - 1] = {
dynamic: false,
spread: false,
content: `${lastPart.content}${item.routeSuffix}`,
};
}
segments[segments.length - 1] = lastSegment;
} else {
segments.push(item.parts);
}
}
} else {
segments.push(item.parts);
}
const params = parentParams.slice();
params.push(...item.parts.filter((p) => p.dynamic).map((p) => p.content));
if (item.isDir) {
walk(fsMod ?? fs, path.join(dir, item.basename), segments, params);
} else {
components.push(item.file);
const component = item.file;
const trailingSlash = item.isPage ? settings.config.trailingSlash : 'never';
const pattern = getPattern(segments, settings.config.base, trailingSlash);
const generate = getRouteGenerator(segments, trailingSlash);
const pathname = segments.every((segment) => segment.length === 1 && !segment[0].dynamic)
? `/${segments.map((segment) => segment[0].content).join('/')}`
: null;
const route = `/${segments
.map(([{ dynamic, content }]) => (dynamic ? `[${content}]` : content))
.join('/')}`.toLowerCase();
routes.push({
route,
type: item.isPage ? 'page' : 'endpoint',
pattern,
segments,
params,
component,
generate,
pathname: pathname || undefined,
prerender: isPrerenderDefault,
});
}
});
}
const { config } = settings;
const pages = resolvePages(config);
if (localFs.existsSync(pages)) {
walk(localFs, fileURLToPath(pages), [], []);
} else if (settings.injectedRoutes.length === 0) {
const pagesDirRootRelative = pages.href.slice(settings.config.root.href.length);
warn(logging, 'astro', `Missing pages directory: ${pagesDirRootRelative}`);
}
settings.injectedRoutes
?.sort((a, b) =>
// sort injected routes in the same way as user-defined routes
comparator(injectedRouteToItem({ config, cwd }, a), injectedRouteToItem({ config, cwd }, b))
)
.reverse() // prepend to the routes array from lowest to highest priority
.forEach(({ pattern: name, entryPoint }) => {
let resolved: string;
try {
resolved = require.resolve(entryPoint, { paths: [cwd || fileURLToPath(config.root)] });
} catch (e) {
resolved = fileURLToPath(new URL(entryPoint, config.root));
}
const component = slash(path.relative(cwd || fileURLToPath(config.root), resolved));
const segments = removeLeadingForwardSlash(name)
.split(path.posix.sep)
.filter(Boolean)
.map((s: string) => {
validateSegment(s);
return getParts(s, component);
});
const type = resolved.endsWith('.astro') ? 'page' : 'endpoint';
const isPage = type === 'page';
const trailingSlash = isPage ? config.trailingSlash : 'never';
const pattern = getPattern(segments, settings.config.base, trailingSlash);
const generate = getRouteGenerator(segments, trailingSlash);
const pathname = segments.every((segment) => segment.length === 1 && !segment[0].dynamic)
? `/${segments.map((segment) => segment[0].content).join('/')}`
: null;
const params = segments
.flat()
.filter((p) => p.dynamic)
.map((p) => p.content);
const route = `/${segments
.map(([{ dynamic, content }]) => (dynamic ? `[${content}]` : content))
.join('/')}`.toLowerCase();
const collision = routes.find(({ route: r }) => r === route);
if (collision) {
throw new Error(
`An integration attempted to inject a route that is already used in your project: "${route}" at "${component}". \nThis route collides with: "${collision.component}".`
);
}
// the routes array was already sorted by priority,
// pushing to the front of the list ensure that injected routes
// are given priority over all user-provided routes
routes.unshift({
type,
route,
pattern,
segments,
params,
component,
generate,
pathname: pathname || void 0,
prerender: isPrerenderDefault,
});
});
return {
routes,
};
}