sepal 0.1.0-alpha.2 → 0.2.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -6
- package/package.json +3 -20
- package/src/index.js +5 -1
- package/src/shims.js +88 -10
- package/types/shims.d.ts +12 -0
- package/types/vite/sveltekit.d.ts +8 -0
- package/types/vite/vite.d.ts +6 -30
- package/vite/sveltekit.js +46 -7
- package/vite/vite.js +22 -61
- package/ambient.d.ts +0 -20
- package/types/vite/navigation.d.ts +0 -1
- package/types/vite/router-entry.d.ts +0 -1
- package/types/vite/router.d.ts +0 -21
- package/types/vite/routes.d.ts +0 -41
- package/types/vite/routing.svelte.d.ts +0 -131
- package/types/vite/state.d.ts +0 -1
- package/vite/Router.svelte +0 -32
- package/vite/RouterError.svelte +0 -11
- package/vite/navigation.js +0 -3
- package/vite/router-entry.js +0 -12
- package/vite/router.js +0 -17
- package/vite/routes.js +0 -219
- package/vite/routing.svelte.js +0 -378
- package/vite/state.js +0 -2
- package/vite/virtual.d.ts +0 -9
package/vite/routing.svelte.js
DELETED
|
@@ -1,378 +0,0 @@
|
|
|
1
|
-
// File-based routing in the style of SvelteKit, held entirely in memory.
|
|
2
|
-
//
|
|
3
|
-
// There is no address bar, `location` or History API in the host, so the
|
|
4
|
-
// "URL" is a string this module owns, with its own back/forward stack. The
|
|
5
|
-
// route table comes from the Vite preset, which reads `src/routes`.
|
|
6
|
-
|
|
7
|
-
import renderer, { root } from 'sepal/renderer';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* A `+layout` or `+page`: its component and the `load` from the `.js`/`.ts`
|
|
11
|
-
* file next to it. A layout may have only a `load`.
|
|
12
|
-
* @typedef {{ component: import('svelte').Component<any> | null, load?: (event: LoadEvent) => any }} RouteNode
|
|
13
|
-
*/
|
|
14
|
-
/**
|
|
15
|
-
* @typedef {{
|
|
16
|
-
* id: string,
|
|
17
|
-
* pattern: RegExp,
|
|
18
|
-
* names: string[],
|
|
19
|
-
* layouts: RouteNode[],
|
|
20
|
-
* page: RouteNode,
|
|
21
|
-
* error: import('svelte').Component<any> | null,
|
|
22
|
-
* error_depth: number
|
|
23
|
-
* }} Route
|
|
24
|
-
* `error` is the nearest `+error.svelte`; `error_depth` is how many of the
|
|
25
|
-
* route's layouts stay on screen around it (those down to its own directory).
|
|
26
|
-
*/
|
|
27
|
-
/**
|
|
28
|
-
* What a `load` function receives.
|
|
29
|
-
* @typedef {{
|
|
30
|
-
* params: Record<string, string>,
|
|
31
|
-
* url: PageUrl,
|
|
32
|
-
* route: { id: string | null },
|
|
33
|
-
* parent: () => Promise<Record<string, any>>
|
|
34
|
-
* }} LoadEvent
|
|
35
|
-
*/
|
|
36
|
-
/**
|
|
37
|
-
* The current location. A plain object with the commonly used parts of a
|
|
38
|
-
* `URL`; the engine has no `URL` class.
|
|
39
|
-
* @typedef {{
|
|
40
|
-
* href: string,
|
|
41
|
-
* pathname: string,
|
|
42
|
-
* search: string,
|
|
43
|
-
* hash: string,
|
|
44
|
-
* searchParams: { get(name: string): string | null, has(name: string): boolean }
|
|
45
|
-
* }} PageUrl
|
|
46
|
-
*/
|
|
47
|
-
/**
|
|
48
|
-
* What is on screen: the layouts from the outside in, then the page (or an
|
|
49
|
-
* error component in its place). `data[i]` is what layout `i` receives.
|
|
50
|
-
* @typedef {{
|
|
51
|
-
* layouts: RouteNode[],
|
|
52
|
-
* data: Record<string, any>[],
|
|
53
|
-
* page: import('svelte').Component<any>,
|
|
54
|
-
* page_data: Record<string, any>
|
|
55
|
-
* }} View
|
|
56
|
-
*/
|
|
57
|
-
|
|
58
|
-
class HttpError {
|
|
59
|
-
/** @param {number} status @param {string} message */
|
|
60
|
-
constructor(status, message) {
|
|
61
|
-
this.status = status;
|
|
62
|
-
this.message = message;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
class Redirect {
|
|
67
|
-
/** @param {number} status @param {string} location */
|
|
68
|
-
constructor(status, location) {
|
|
69
|
-
this.status = status;
|
|
70
|
-
this.location = location;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Stop a `load` and show the nearest `+error.svelte` with this status.
|
|
76
|
-
* @param {number} status
|
|
77
|
-
* @param {string} [message]
|
|
78
|
-
* @returns {never}
|
|
79
|
-
*/
|
|
80
|
-
export function error(status, message = 'Error') {
|
|
81
|
-
throw new HttpError(status, message);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Stop a `load` and go somewhere else instead.
|
|
86
|
-
* @param {number} status kept for parity with SvelteKit; it has no effect here
|
|
87
|
-
* @param {string} location
|
|
88
|
-
* @returns {never}
|
|
89
|
-
*/
|
|
90
|
-
export function redirect(status, location) {
|
|
91
|
-
throw new Redirect(status, location);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/** @param {string} href an absolute path, optionally with `?search` and `#hash` @returns {PageUrl} */
|
|
95
|
-
function parse(href) {
|
|
96
|
-
const hash_at = href.indexOf('#');
|
|
97
|
-
const hash = hash_at === -1 ? '' : href.slice(hash_at);
|
|
98
|
-
const rest = hash_at === -1 ? href : href.slice(0, hash_at);
|
|
99
|
-
const search_at = rest.indexOf('?');
|
|
100
|
-
const search = search_at === -1 ? '' : rest.slice(search_at);
|
|
101
|
-
let pathname = search_at === -1 ? rest : rest.slice(0, search_at);
|
|
102
|
-
// `/about/` and `/about` are the same page.
|
|
103
|
-
if (pathname.length > 1 && pathname.endsWith('/')) pathname = pathname.slice(0, -1);
|
|
104
|
-
|
|
105
|
-
/** @type {Map<string, string>} */
|
|
106
|
-
const query = new Map();
|
|
107
|
-
for (const pair of search.slice(1).split('&')) {
|
|
108
|
-
if (pair === '') continue;
|
|
109
|
-
const [name, value = ''] = pair.split('=');
|
|
110
|
-
const decode = (/** @type {string} */ text) => decodeURIComponent(text.replace(/\+/g, ' '));
|
|
111
|
-
if (!query.has(decode(name))) query.set(decode(name), decode(value));
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
return {
|
|
115
|
-
href: pathname + search + hash,
|
|
116
|
-
pathname,
|
|
117
|
-
search,
|
|
118
|
-
hash,
|
|
119
|
-
searchParams: { get: (name) => query.get(name) ?? null, has: (name) => query.has(name) }
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Turn an `href` into an absolute path, the way a browser would against the
|
|
125
|
-
* current location.
|
|
126
|
-
* @param {string} href
|
|
127
|
-
*/
|
|
128
|
-
function resolve(href) {
|
|
129
|
-
if (href.startsWith('/')) return href;
|
|
130
|
-
const here = page.url.pathname;
|
|
131
|
-
if (href.startsWith('?') || href.startsWith('#')) return here + href;
|
|
132
|
-
|
|
133
|
-
const segments = here.split('/').slice(1, -1);
|
|
134
|
-
const tail_at = href.search(/[?#]/);
|
|
135
|
-
const tail = tail_at === -1 ? '' : href.slice(tail_at);
|
|
136
|
-
for (const segment of (tail_at === -1 ? href : href.slice(0, tail_at)).split('/')) {
|
|
137
|
-
if (segment === '..') segments.pop();
|
|
138
|
-
else if (segment !== '.' && segment !== '') segments.push(segment);
|
|
139
|
-
}
|
|
140
|
-
return '/' + segments.join('/') + tail;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* The page that is showing. Reactive; the same shape as `page` in SvelteKit's
|
|
145
|
-
* `$app/state`, except that `url` is a plain object.
|
|
146
|
-
* @type {{
|
|
147
|
-
* url: PageUrl,
|
|
148
|
-
* params: Record<string, string>,
|
|
149
|
-
* route: { id: string | null },
|
|
150
|
-
* data: Record<string, any>,
|
|
151
|
-
* status: number,
|
|
152
|
-
* error: { message: string } | null
|
|
153
|
-
* }}
|
|
154
|
-
*/
|
|
155
|
-
export const page = $state({
|
|
156
|
-
url: parse('/'),
|
|
157
|
-
params: {},
|
|
158
|
-
route: { id: null },
|
|
159
|
-
data: {},
|
|
160
|
-
status: 200,
|
|
161
|
-
error: null
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
/** @type {View | null} */
|
|
165
|
-
let view = $state.raw(null);
|
|
166
|
-
|
|
167
|
-
/** Read by `Router.svelte`. */
|
|
168
|
-
export const router = {
|
|
169
|
-
get view() {
|
|
170
|
-
return view;
|
|
171
|
-
}
|
|
172
|
-
};
|
|
173
|
-
|
|
174
|
-
/** @type {Route[]} */
|
|
175
|
-
let routes = [];
|
|
176
|
-
/** Used when nothing matches: the root layouts around the root `+error.svelte`. */
|
|
177
|
-
let fallback = /** @type {{ layouts: RouteNode[], error: import('svelte').Component<any> | null }} */ ({
|
|
178
|
-
layouts: [],
|
|
179
|
-
error: null
|
|
180
|
-
});
|
|
181
|
-
/** @type {import('svelte').Component<any>} */
|
|
182
|
-
let default_error;
|
|
183
|
-
|
|
184
|
-
/** Visited locations. `position` is where we are in it. @type {string[]} */
|
|
185
|
-
const stack = [];
|
|
186
|
-
let position = -1;
|
|
187
|
-
/** Bumped by every navigation, so one that was overtaken can tell and stop. */
|
|
188
|
-
let latest = 0;
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* Run the loads for a set of nodes, outermost first. Each node's data is its
|
|
192
|
-
* ancestors' data plus its own.
|
|
193
|
-
*
|
|
194
|
-
* Returns the data per node, or the index of the node whose load threw.
|
|
195
|
-
* A redirect is rethrown.
|
|
196
|
-
*
|
|
197
|
-
* ponytail: every load runs on every navigation. SvelteKit reruns only the
|
|
198
|
-
* ones whose inputs changed; track `params`/`url` reads if loads get slow.
|
|
199
|
-
*
|
|
200
|
-
* @param {RouteNode[]} nodes
|
|
201
|
-
* @param {Omit<LoadEvent, 'parent'>} event
|
|
202
|
-
*/
|
|
203
|
-
async function run_loads(nodes, event) {
|
|
204
|
-
/** @type {Record<string, any>[]} */
|
|
205
|
-
const data = [];
|
|
206
|
-
let merged = {};
|
|
207
|
-
for (let index = 0; index < nodes.length; index++) {
|
|
208
|
-
const above = merged;
|
|
209
|
-
try {
|
|
210
|
-
const loaded = await nodes[index].load?.({ ...event, parent: async () => above });
|
|
211
|
-
merged = { ...above, ...(loaded ?? {}) };
|
|
212
|
-
} catch (thrown) {
|
|
213
|
-
if (thrown instanceof Redirect) throw thrown;
|
|
214
|
-
return { data, failed: index, thrown };
|
|
215
|
-
}
|
|
216
|
-
data.push(merged);
|
|
217
|
-
}
|
|
218
|
-
return { data, failed: -1, thrown: null };
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
/**
|
|
222
|
-
* Work out what to show for a location. Does not touch any state.
|
|
223
|
-
* @param {PageUrl} url
|
|
224
|
-
*/
|
|
225
|
-
async function resolve_view(url) {
|
|
226
|
-
/** @type {Route | undefined} */
|
|
227
|
-
let route;
|
|
228
|
-
/** @type {Record<string, string>} */
|
|
229
|
-
const params = {};
|
|
230
|
-
for (const candidate of routes) {
|
|
231
|
-
const match = candidate.pattern.exec(url.pathname);
|
|
232
|
-
if (match === null) continue;
|
|
233
|
-
route = candidate;
|
|
234
|
-
candidate.names.forEach((name, index) => (params[name] = decodeURIComponent(match[index + 1] ?? '')));
|
|
235
|
-
break;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
const event = { params, url, route: { id: route?.id ?? null } };
|
|
239
|
-
const nodes = route ? [...route.layouts, route.page] : fallback.layouts;
|
|
240
|
-
const { data, failed, thrown } = await run_loads(nodes, event);
|
|
241
|
-
|
|
242
|
-
if (route && failed === -1) {
|
|
243
|
-
return {
|
|
244
|
-
params,
|
|
245
|
-
route_id: route.id,
|
|
246
|
-
status: 200,
|
|
247
|
-
error: null,
|
|
248
|
-
view: {
|
|
249
|
-
layouts: route.layouts,
|
|
250
|
-
data: data.slice(0, -1),
|
|
251
|
-
page: /** @type {import('svelte').Component<any>} */ (route.page.component),
|
|
252
|
-
page_data: data[data.length - 1]
|
|
253
|
-
}
|
|
254
|
-
};
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
// Not found, or a load threw. Keep the layouts that loaded, down to the
|
|
258
|
-
// error page's own directory; the error page takes the place of the rest.
|
|
259
|
-
//
|
|
260
|
-
// ponytail: when the layout next to the +error.svelte is the one that
|
|
261
|
-
// failed, SvelteKit moves up to the next +error.svelte. This keeps the
|
|
262
|
-
// nearest one and renders it one layout further out.
|
|
263
|
-
const kept = Math.min(
|
|
264
|
-
failed === -1 ? nodes.length : failed,
|
|
265
|
-
route ? route.error_depth : fallback.layouts.length
|
|
266
|
-
);
|
|
267
|
-
const reason = /** @type {any} */ (thrown);
|
|
268
|
-
return {
|
|
269
|
-
params,
|
|
270
|
-
route_id: route?.id ?? null,
|
|
271
|
-
status: route ? (reason instanceof HttpError ? reason.status : 500) : 404,
|
|
272
|
-
error: { message: route ? String(reason?.message ?? reason) : `Not found: ${url.pathname}` },
|
|
273
|
-
view: {
|
|
274
|
-
layouts: nodes.slice(0, kept),
|
|
275
|
-
data: data.slice(0, kept),
|
|
276
|
-
page: (route ? route.error : fallback.error) ?? default_error,
|
|
277
|
-
page_data: data[kept - 1] ?? {}
|
|
278
|
-
}
|
|
279
|
-
};
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
/**
|
|
283
|
-
* Go to a location and, once its loads have finished, record it with `commit`.
|
|
284
|
-
* @param {string} href absolute
|
|
285
|
-
* @param {(href: string) => void} commit
|
|
286
|
-
*/
|
|
287
|
-
async function navigate(href, commit) {
|
|
288
|
-
const mine = ++latest;
|
|
289
|
-
let url = parse(href);
|
|
290
|
-
let resolved;
|
|
291
|
-
for (let hops = 0; ; hops++) {
|
|
292
|
-
try {
|
|
293
|
-
resolved = await resolve_view(url);
|
|
294
|
-
break;
|
|
295
|
-
} catch (thrown) {
|
|
296
|
-
if (!(thrown instanceof Redirect) || hops === 10) throw thrown;
|
|
297
|
-
url = parse(resolve(thrown.location));
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
// A newer navigation started while this one was loading: it wins.
|
|
301
|
-
if (mine !== latest) return;
|
|
302
|
-
|
|
303
|
-
commit(url.href);
|
|
304
|
-
view = resolved.view;
|
|
305
|
-
page.url = url;
|
|
306
|
-
page.params = resolved.params;
|
|
307
|
-
page.route = { id: resolved.route_id };
|
|
308
|
-
page.data = resolved.view.page_data;
|
|
309
|
-
page.status = resolved.status;
|
|
310
|
-
page.error = resolved.error;
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
/**
|
|
314
|
-
* Navigate to another page, like SvelteKit's `goto`.
|
|
315
|
-
* @param {string} href a path; relative ones resolve against the current page
|
|
316
|
-
* @param {{ replaceState?: boolean }} [options] `replaceState` replaces the current history entry instead of adding one
|
|
317
|
-
* @returns {Promise<void>} resolves when the new page is showing
|
|
318
|
-
*/
|
|
319
|
-
export function goto(href, { replaceState = false } = {}) {
|
|
320
|
-
return navigate(resolve(href), (final) => {
|
|
321
|
-
if (replaceState && position >= 0) {
|
|
322
|
-
stack[position] = final;
|
|
323
|
-
} else {
|
|
324
|
-
stack.splice(position + 1);
|
|
325
|
-
position = stack.push(final) - 1;
|
|
326
|
-
}
|
|
327
|
-
});
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
/**
|
|
331
|
-
* Go back one page, like `history.back()` in a browser. Does nothing on the first page.
|
|
332
|
-
* @returns {Promise<void>}
|
|
333
|
-
*/
|
|
334
|
-
export function back() {
|
|
335
|
-
if (position <= 0) return Promise.resolve();
|
|
336
|
-
return navigate(stack[position - 1], () => position--);
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
/**
|
|
340
|
-
* Go forward again after `back()`.
|
|
341
|
-
* @returns {Promise<void>}
|
|
342
|
-
*/
|
|
343
|
-
export function forward() {
|
|
344
|
-
if (position >= stack.length - 1) return Promise.resolve();
|
|
345
|
-
return navigate(stack[position + 1], () => position++);
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
/**
|
|
349
|
-
* A click on `<a href="/somewhere">` navigates, as it does in SvelteKit.
|
|
350
|
-
* Links with a scheme (`https:`, `mailto:`) are left alone.
|
|
351
|
-
* @param {any} event
|
|
352
|
-
*/
|
|
353
|
-
function on_click(event) {
|
|
354
|
-
if (event.defaultPrevented || event.button || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) {
|
|
355
|
-
return;
|
|
356
|
-
}
|
|
357
|
-
for (let node = event.target; node; node = node.parentNode) {
|
|
358
|
-
if (node.nodeName !== 'A') continue;
|
|
359
|
-
const href = node.getAttribute('href');
|
|
360
|
-
if (href === null || href.startsWith('#') || href.startsWith('//') || /^[a-z][a-z0-9+.-]*:/i.test(href)) return;
|
|
361
|
-
event.preventDefault();
|
|
362
|
-
goto(href);
|
|
363
|
-
return;
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
/**
|
|
368
|
-
* Called once by the generated entry module.
|
|
369
|
-
* @param {{ routes: Route[], fallback: typeof fallback }} manifest
|
|
370
|
-
* @param {import('svelte').Component<any>} error_component shown when no `+error.svelte` applies
|
|
371
|
-
*/
|
|
372
|
-
export function start(manifest, error_component) {
|
|
373
|
-
routes = manifest.routes;
|
|
374
|
-
fallback = manifest.fallback;
|
|
375
|
-
default_error = error_component;
|
|
376
|
-
renderer.addEventListener(root, 'click', on_click);
|
|
377
|
-
return goto('/');
|
|
378
|
-
}
|
package/vite/state.js
DELETED
package/vite/virtual.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
// Generated by the Vite preset from `src/routes` (see routes.js). This copy
|
|
2
|
-
// is for type-checking this package; apps get the same declaration from ambient.d.ts.
|
|
3
|
-
declare module 'virtual:sepal/routes' {
|
|
4
|
-
import type { Route, RouteNode } from './routing.svelte.js';
|
|
5
|
-
import type { Component } from 'svelte';
|
|
6
|
-
|
|
7
|
-
export const routes: Route[];
|
|
8
|
-
export const fallback: { layouts: RouteNode[]; error: Component<any> | null };
|
|
9
|
-
}
|