forked from sveltejs/kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
532 lines (451 loc) · 14.7 KB
/
index.js
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
import fs from 'fs';
import path from 'path';
import mime from 'mime';
import { runtime_directory } from '../../utils.js';
import { posixify } from '../../../utils/filesystem.js';
import { parse_route_id } from '../../../utils/routing.js';
import { sort_routes } from './sort.js';
/**
* Generates the manifest data used for the client-side manifest and types generation.
* @param {{
* config: import('types').ValidatedConfig;
* fallback?: string;
* cwd?: string;
* }} opts
* @returns {import('types').ManifestData}
*/
export default function create_manifest_data({
config,
fallback = `${runtime_directory}/components`,
cwd = process.cwd()
}) {
const assets = create_assets(config);
const matchers = create_matchers(config, cwd);
const { nodes, routes } = create_routes_and_nodes(cwd, config, fallback);
for (const route of routes) {
for (const param of route.params) {
if (param.matcher && !matchers[param.matcher]) {
throw new Error(`No matcher found for parameter '${param.matcher}' in route ${route.id}`);
}
}
}
return {
assets,
matchers,
nodes,
routes
};
}
/**
* @param {import('types').ValidatedConfig} config
*/
export function create_assets(config) {
return list_files(config.kit.files.assets).map((file) => ({
file,
size: fs.statSync(path.resolve(config.kit.files.assets, file)).size,
type: mime.getType(file)
}));
}
/**
* @param {import('types').ValidatedConfig} config
* @param {string} cwd
*/
function create_matchers(config, cwd) {
const params_base = path.relative(cwd, config.kit.files.params);
/** @type {Record<string, string>} */
const matchers = {};
if (fs.existsSync(config.kit.files.params)) {
for (const file of fs.readdirSync(config.kit.files.params)) {
const ext = path.extname(file);
if (!config.kit.moduleExtensions.includes(ext)) continue;
const type = file.slice(0, -ext.length);
if (/^\w+$/.test(type)) {
const matcher_file = path.join(params_base, file);
// Disallow same matcher with different extensions
if (matchers[type]) {
throw new Error(`Duplicate matchers: ${matcher_file} and ${matchers[type]}`);
} else {
matchers[type] = matcher_file;
}
} else {
throw new Error(
`Matcher names can only have underscores and alphanumeric characters — "${file}" is invalid`
);
}
}
}
return matchers;
}
/**
* @param {import('types').ValidatedConfig} config
* @param {string} cwd
* @param {string} fallback
*/
function create_routes_and_nodes(cwd, config, fallback) {
/** @type {import('types').RouteData[]} */
const routes = [];
const routes_base = posixify(path.relative(cwd, config.kit.files.routes));
const valid_extensions = [...config.extensions, ...config.kit.moduleExtensions];
/** @type {import('types').PageNode[]} */
const nodes = [];
if (fs.existsSync(config.kit.files.routes)) {
/**
* @param {number} depth
* @param {string} id
* @param {string} segment
* @param {import('types').RouteData | null} parent
*/
const walk = (depth, id, segment, parent) => {
const unescaped = id.replace(/\[([ux])\+([^\]]+)\]/gi, (match, type, code) => {
if (match !== match.toLowerCase()) {
throw new Error(`Character escape sequence in ${id} must be lowercase`);
}
if (!/[0-9a-f]+/.test(code)) {
throw new Error(`Invalid character escape sequence in ${id}`);
}
if (type === 'x') {
if (code.length !== 2) {
throw new Error(`Hexadecimal escape sequence in ${id} must be two characters`);
}
return String.fromCharCode(parseInt(code, 16));
} else {
if (code.length < 4 || code.length > 6) {
throw new Error(
`Unicode escape sequence in ${id} must be between four and six characters`
);
}
return String.fromCharCode(parseInt(code, 16));
}
});
if (/\]\[/.test(unescaped)) {
throw new Error(`Invalid route ${id} — parameters must be separated`);
}
if (count_occurrences('[', id) !== count_occurrences(']', id)) {
throw new Error(`Invalid route ${id} — brackets are unbalanced`);
}
if (/#/.test(segment)) {
// Vite will barf on files with # in them
throw new Error(`Route ${id} should be renamed to ${id.replace(/#/g, '[x+23]')}`);
}
if (/\[\.\.\.\w+\]\/\[\[/.test(id)) {
throw new Error(
`Invalid route ${id} — an [[optional]] route segment cannot follow a [...rest] route segment`
);
}
if (/\[\[\.\.\./.test(id)) {
throw new Error(
`Invalid route ${id} — a rest route segment is always optional, remove the outer square brackets`
);
}
const { pattern, params } = parse_route_id(id);
/** @type {import('types').RouteData} */
const route = {
id,
parent,
segment,
pattern,
params,
layout: null,
error: null,
leaf: null,
page: null,
endpoint: null
};
// important to do this before walking children, so that child
// routes appear later
routes.push(route);
// if we don't do this, the route map becomes unwieldy to console.log
Object.defineProperty(route, 'parent', { enumerable: false });
const dir = path.join(cwd, routes_base, id);
// We can't use withFileTypes because of a NodeJs bug which returns wrong results
// with isDirectory() in case of symlinks: https://github.com/nodejs/node/issues/30646
const files = fs.readdirSync(dir).map((name) => ({
is_dir: fs.statSync(path.join(dir, name)).isDirectory(),
name
}));
// process files first
for (const file of files) {
if (file.is_dir) continue;
if (!file.name.startsWith('+')) continue;
if (!valid_extensions.find((ext) => file.name.endsWith(ext))) continue;
if (file.name.endsWith('.d.ts')) {
let name = file.name.slice(0, -5);
const ext = valid_extensions.find((ext) => name.endsWith(ext));
if (ext) name = name.slice(0, -ext.length);
const valid =
/^\+(?:(page(?:@(.*))?)|(layout(?:@(.*))?)|(error))$/.test(name) ||
/^\+(?:(server)|(page(?:(@[a-zA-Z0-9_-]*))?(\.server)?)|(layout(?:(@[a-zA-Z0-9_-]*))?(\.server)?))$/.test(
name
);
if (valid) continue;
}
const project_relative = posixify(path.relative(cwd, path.join(dir, file.name)));
const item = analyze(
project_relative,
file.name,
config.extensions,
config.kit.moduleExtensions
);
if (item.kind === 'component') {
if (item.is_error) {
route.error = {
depth,
component: project_relative
};
} else if (item.is_layout) {
if (!route.layout) route.layout = { depth, child_pages: [] };
route.layout.component = project_relative;
if (item.uses_layout !== undefined) route.layout.parent_id = item.uses_layout;
} else {
if (!route.leaf) route.leaf = { depth };
route.leaf.component = project_relative;
if (item.uses_layout !== undefined) route.leaf.parent_id = item.uses_layout;
}
} else if (item.is_layout) {
if (!route.layout) route.layout = { depth, child_pages: [] };
route.layout[item.kind] = project_relative;
} else if (item.is_page) {
if (!route.leaf) route.leaf = { depth };
route.leaf[item.kind] = project_relative;
} else {
route.endpoint = {
file: project_relative
};
}
}
// then handle children
for (const file of files) {
if (file.is_dir) {
walk(depth + 1, path.posix.join(id, file.name), file.name, route);
}
}
};
walk(0, '/', '', null);
if (routes.length === 1) {
const root = routes[0];
if (!root.leaf && !root.error && !root.layout && !root.endpoint) {
throw new Error(
// TODO adjust this error message for 1.0
// 'No routes found. If you are using a custom src/routes directory, make sure it is specified in svelte.config.js'
'The filesystem router API has changed, see https://github.com/sveltejs/kit/discussions/5774 for details'
);
}
}
} else {
// If there's no routes directory, we'll just create a single empty route. This ensures the root layout and
// error components are included in the manifest, which is needed for subsequent build/dev commands to work
routes.push({
id: '/',
segment: '',
pattern: /^$/,
params: [],
parent: null,
layout: null,
error: null,
leaf: null,
page: null,
endpoint: null
});
}
prevent_conflicts(routes);
const root = routes[0];
if (!root.layout?.component) {
if (!root.layout) root.layout = { depth: 0, child_pages: [] };
root.layout.component = posixify(path.relative(cwd, `${fallback}/layout.svelte`));
}
if (!root.error?.component) {
if (!root.error) root.error = { depth: 0 };
root.error.component = posixify(path.relative(cwd, `${fallback}/error.svelte`));
}
// we do layouts/errors first as they are more likely to be reused,
// and smaller indexes take fewer bytes. also, this guarantees that
// the default error/layout are 0/1
for (const route of routes) {
if (route.layout) {
if (!route.layout?.component) {
route.layout.component = posixify(path.relative(cwd, `${fallback}/layout.svelte`));
}
nodes.push(route.layout);
}
if (route.error) nodes.push(route.error);
}
for (const route of routes) {
if (route.leaf) nodes.push(route.leaf);
}
const indexes = new Map(nodes.map((node, i) => [node, i]));
for (const route of routes) {
if (!route.leaf) continue;
route.page = {
layouts: [],
errors: [],
leaf: /** @type {number} */ (indexes.get(route.leaf))
};
/** @type {import('types').RouteData | null} */
let current_route = route;
let current_node = route.leaf;
let parent_id = route.leaf.parent_id;
while (current_route) {
if (parent_id === undefined || current_route.segment === parent_id) {
if (current_route.layout || current_route.error) {
route.page.layouts.unshift(
current_route.layout ? indexes.get(current_route.layout) : undefined
);
route.page.errors.unshift(
current_route.error ? indexes.get(current_route.error) : undefined
);
}
if (current_route.layout) {
/** @type {import('types').PageNode[]} */ (current_route.layout.child_pages).push(
route.leaf
);
current_node.parent = current_node = current_route.layout;
parent_id = current_node.parent_id;
} else {
parent_id = undefined;
}
}
current_route = current_route.parent;
}
if (parent_id !== undefined) {
throw new Error(`${current_node.component} references missing segment "${parent_id}"`);
}
}
return {
nodes,
routes: sort_routes(routes)
};
}
/**
* @param {string} project_relative
* @param {string} file
* @param {string[]} component_extensions
* @param {string[]} module_extensions
* @returns {import('./types').RouteFile}
*/
function analyze(project_relative, file, component_extensions, module_extensions) {
const component_extension = component_extensions.find((ext) => file.endsWith(ext));
if (component_extension) {
const name = file.slice(0, -component_extension.length);
const pattern = /^\+(?:(page(?:@(.*))?)|(layout(?:@(.*))?)|(error))$/;
const match = pattern.exec(name);
if (!match) {
// TODO remove for 1.0
if (/^\+layout-/.test(name)) {
throw new Error(
`${project_relative} should be reimplemented with layout groups: https://kit.svelte.dev/docs/advanced-routing#advanced-layouts`
);
}
throw new Error(`Files prefixed with + are reserved (saw ${project_relative})`);
}
return {
kind: 'component',
is_page: !!match[1],
is_layout: !!match[3],
is_error: !!match[5],
uses_layout: match[2] ?? match[4]
};
}
const module_extension = module_extensions.find((ext) => file.endsWith(ext));
if (module_extension) {
const name = file.slice(0, -module_extension.length);
const pattern =
/^\+(?:(server)|(page(?:(@[a-zA-Z0-9_-]*))?(\.server)?)|(layout(?:(@[a-zA-Z0-9_-]*))?(\.server)?))$/;
const match = pattern.exec(name);
if (!match) {
throw new Error(`Files prefixed with + are reserved (saw ${project_relative})`);
} else if (match[3] || match[6]) {
throw new Error(
// prettier-ignore
`Only Svelte files can reference named layouts. Remove '${match[3] || match[6]}' from ${file} (at ${project_relative})`
);
}
const kind = !!(match[1] || match[4] || match[7]) ? 'server' : 'shared';
return {
kind,
is_page: !!match[2],
is_layout: !!match[5]
};
}
throw new Error(`Files and directories prefixed with + are reserved (saw ${project_relative})`);
}
/** @param {string} dir */
function list_files(dir) {
/** @type {string[]} */
const files = [];
/** @param {string} current */
function walk(current) {
for (const file of fs.readdirSync(path.resolve(dir, current))) {
const child = path.posix.join(current, file);
if (fs.statSync(path.resolve(dir, child)).isDirectory()) {
walk(child);
} else {
files.push(child);
}
}
}
if (fs.existsSync(dir)) walk('');
return files;
}
/**
* @param {string} needle
* @param {string} haystack
*/
function count_occurrences(needle, haystack) {
let count = 0;
for (let i = 0; i < haystack.length; i += 1) {
if (haystack[i] === needle) count += 1;
}
return count;
}
/** @param {import('types').RouteData[]} routes */
function prevent_conflicts(routes) {
/** @type {Map<string, string>} */
const lookup = new Map();
for (const route of routes) {
if (!route.leaf && !route.endpoint) continue;
const normalized = normalize_route_id(route.id);
// find all permutations created by optional parameters
const split = normalized.split(/<\?(.+?)\>/g);
let permutations = [/** @type {string} */ (split[0])];
// turn `x/[[optional]]/y` into `x/y` and `x/[required]/y`
for (let i = 1; i < split.length; i += 2) {
const matcher = split[i];
const next = split[i + 1];
permutations = permutations.reduce((a, b) => {
a.push(b + next);
if (!(matcher === '*' && b.endsWith('//'))) a.push(b + `<${matcher}>${next}`);
return a;
}, /** @type {string[]} */ ([]));
}
for (const permutation of permutations) {
// remove leading/trailing/duplicated slashes caused by prior
// manipulation of optional parameters and (groups)
const key = permutation
.replace(/\/{2,}/, '/')
.replace(/^\//, '')
.replace(/\/$/, '');
if (lookup.has(key)) {
throw new Error(
`The "${lookup.get(key)}" and "${route.id}" routes conflict with each other`
);
}
lookup.set(key, route.id);
}
}
}
/** @param {string} id */
function normalize_route_id(id) {
return (
id
// remove groups
.replace(/(?<=^|\/)\(.+?\)(?=$|\/)/g, '')
.replace(/\[[ux]\+([0-9a-f]+)\]/g, (_, x) =>
String.fromCharCode(parseInt(x, 16)).replace(/\//g, '%2f')
)
// replace `[param]` with `<*>`, `[param=x]` with `<x>`, and `[[param]]` with `<?*>`
.replace(
/\[(?:(\[)|(\.\.\.))?.+?(=.+?)?\]\]?/g,
(_, optional, rest, matcher) => `<${optional ? '?' : ''}${rest ?? ''}${matcher ?? '*'}>`
)
);
}