This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 434
/
create_manifest_data.ts
330 lines (273 loc) · 8.49 KB
/
create_manifest_data.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
import * as fs from 'fs';
import * as path from 'path';
import { Page, PageComponent, ServerRoute, ManifestData } from '../interfaces';
import { posixify, reserved_words } from '../utils';
export default function create_manifest_data(cwd: string, extensions = '.svelte .html'): ManifestData {
const component_extensions = extensions.split(' ');
// TODO remove in a future version
if (!fs.existsSync(cwd)) {
throw new Error('As of Sapper 0.21, the routes/ directory should become src/routes/');
}
function find_layout(file_name: string, component_name: string, dir = '') {
const ext = component_extensions.find((ext) => fs.existsSync(path.join(cwd, dir, `${file_name}${ext}`)));
const file = posixify(path.join(dir, `${file_name}${ext}`));
return ext
? {
name: component_name,
file
}
: null;
}
const components: PageComponent[] = [];
const pages: Page[] = [];
const server_routes: ServerRoute[] = [];
const default_layout: PageComponent = {
default: true,
type: 'layout',
name: '_default_layout',
file: null
};
const default_error: PageComponent = {
default: true,
type: 'error',
name: '_default_error',
file: null
};
function walk(
dir: string,
parent_segments: Part[][],
parent_params: string[],
stack: Array<{
component: PageComponent;
params: string[];
}>
) {
const items = fs.readdirSync(dir)
.map(basename => {
const resolved = path.join(dir, basename);
const file = path.relative(cwd, resolved);
const is_dir = fs.statSync(resolved).isDirectory();
const file_ext = path.extname(basename);
if (basename[0] === '_') return null;
if (basename[0] === '.' && basename !== '.well-known') return null;
if (!is_dir && !/^\.[a-z]+$/i.test(file_ext)) return null; // filter out tmp files etc
const component_extension = component_extensions.find((ext) => basename.endsWith(ext));
const ext = component_extension || file_ext;
const is_page = component_extension != null;
const segment = is_dir ? basename : basename.slice(0, -ext.length);
if (/\]\[/.test(segment)) {
throw new Error(`Invalid route ${file} — parameters must be separated`);
}
const parts = get_parts(segment);
const is_index = is_dir ? false : basename.startsWith('index.');
const route_suffix = basename.slice(basename.indexOf('.'), -ext.length);
parts.forEach(part => {
if (part.qualifier && /[()?:]/.test(part.qualifier.slice(1, -1))) {
throw new Error(`Invalid route ${file} — cannot use (, ), ? or : in route qualifiers`);
}
});
return {
basename,
ext,
parts,
file: posixify(file),
is_dir,
is_index,
is_page,
route_suffix
};
})
.filter(Boolean)
.sort(comparator);
items.forEach(item => {
const segments = parent_segments.slice();
if (item.is_index) {
if (item.route_suffix) {
if (segments.length > 0) {
const last_segment = segments[segments.length - 1].slice();
const last_part = last_segment[last_segment.length - 1];
if (last_part.dynamic) {
last_segment.push({ dynamic: false, content: item.route_suffix });
} else {
last_segment[last_segment.length - 1] = {
dynamic: false,
content: `${last_part.content}${item.route_suffix}`
};
}
segments[segments.length - 1] = last_segment;
} else {
segments.push(item.parts);
}
}
} else {
segments.push(item.parts);
}
const params = parent_params.slice();
params.push(...item.parts.filter(p => p.dynamic).map(p => p.content));
if (item.is_dir) {
const component = find_layout('_layout', `${get_slug(item.file)}__layout`, item.file);
if (component) components.push(component);
walk(
path.join(dir, item.basename),
segments,
params,
component
? stack.concat({ component, params })
: stack.concat(null)
);
} else if (item.is_page) {
const component = {
name: get_slug(item.file),
file: item.file
};
components.push(component);
const parts = (item.is_index && stack[stack.length - 1] === null)
? stack.slice(0, -1).concat({ component, params })
: stack.concat({ component, params });
pages.push({
pattern: get_pattern(segments, true),
parts
});
} else {
server_routes.push({
name: `route_${get_slug(item.file)}`,
pattern: get_pattern(segments, !item.route_suffix),
file: item.file,
params
});
}
});
}
const root = find_layout('_layout', 'main') || default_layout;
const error = find_layout('_error', 'error') || default_error;
walk(cwd, [], [], []);
// check for clashes
const seen_pages: Map<string, Page> = new Map();
pages.forEach(page => {
const pattern = page.pattern.toString();
if (seen_pages.has(pattern)) {
const file = page.parts.pop().component.file;
const other_page = seen_pages.get(pattern);
const other_file = other_page.parts.pop().component.file;
throw new Error(`The ${other_file} and ${file} pages clash`);
}
seen_pages.set(pattern, page);
});
const seen_routes: Map<string, ServerRoute> = new Map();
server_routes.forEach(route => {
const pattern = route.pattern.toString();
if (seen_routes.has(pattern)) {
const other_route = seen_routes.get(pattern);
throw new Error(`The ${other_route.file} and ${route.file} routes clash`);
}
seen_routes.set(pattern, route);
});
return {
root,
error,
components,
pages,
server_routes
};
}
type Part = {
content: string;
dynamic: boolean;
qualifier?: string;
spread?: boolean;
};
function is_spread(path: string) {
const spread_pattern = /\[\.{3}/g;
return spread_pattern.test(path);
}
function comparator(
a: { basename: string; parts: Part[]; file: string; is_index: boolean; },
b: { basename: string; parts: Part[]; file: string; is_index: boolean; }
) {
if (a.is_index !== b.is_index) {
if (a.is_index) return is_spread(a.file) ? 1 : -1;
return is_spread(b.file) ? -1 : 1;
}
const max = Math.max(a.parts.length, b.parts.length);
for (let i = 0; i < max; i += 1) {
const a_sub_part = a.parts[i];
const b_sub_part = b.parts[i];
if (!a_sub_part) return 1; // b is more specific, so goes first
if (!b_sub_part) return -1;
// if spread && index, order later
if (a_sub_part.spread && b_sub_part.spread) {
return a.is_index ? 1 : -1;
}
// If one is ...spread order it later
if (a_sub_part.spread !== b_sub_part.spread) return a_sub_part.spread ? 1 : -1;
if (a_sub_part.dynamic !== b_sub_part.dynamic) {
return a_sub_part.dynamic ? 1 : -1;
}
if (!a_sub_part.dynamic && a_sub_part.content !== b_sub_part.content) {
return (
(b_sub_part.content.length - a_sub_part.content.length) ||
(a_sub_part.content < b_sub_part.content ? -1 : 1)
);
}
// If both parts dynamic, check for regexp patterns
if (a_sub_part.dynamic && b_sub_part.dynamic) {
const regexp_pattern = /\((.*?)\)/;
const a_match = regexp_pattern.exec(a_sub_part.content);
const b_match = regexp_pattern.exec(b_sub_part.content);
if (!a_match && b_match) {
return 1; // No regexp, so less specific than b
}
if (!b_match && a_match) {
return -1;
}
if (a_match && b_match && a_match[1] !== b_match[1]) {
return b_match[1].length - a_match[1].length;
}
}
}
}
function get_parts(part: string): Part[] {
return part.split(/\[(.+?\(.+?\)|.+?)\]/)
.map((str, i) => {
if (!str) return null;
const dynamic = i % 2 === 1;
const [, content, qualifier] = dynamic
? /([^(]+)(\(.+\))?$/.exec(str)
: [null, str, null];
return {
content,
dynamic,
spread: /^\.{3}.+$/.test(content),
qualifier
};
})
.filter(Boolean);
}
function get_slug(file: string) {
let name = file
.replace(/[\\/]index/, '')
.replace(/[/\\]/g, '_')
.replace(/\.\w+$/, '')
.replace(/\[([^(]+)(?:\([^(]+\))?\]/, '$$$1')
.replace(/[^a-zA-Z0-9_$]/g, c => {
return c === '.' ? '_' : `$${c.charCodeAt(0)}`;
});
if (reserved_words.has(name)) name += '_';
return name;
}
function get_pattern(segments: Part[][], add_trailing_slash: boolean) {
const path = segments.map(segment => {
return segment.map(part => {
return part.dynamic
? part.qualifier || (part.spread ? '(.+)' : '([^/]+?)')
: encodeURI(part.content.normalize())
.replace(/\?/g, '%3F')
.replace(/#/g, '%23')
.replace(/%5B/g, '[')
.replace(/%5D/g, ']')
.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}).join('');
}).join('\\/');
const trailing = add_trailing_slash && segments.length ? '\\/?$' : '$';
return new RegExp(`^\\/${path}${trailing}`);
}