sepal 0.1.0-alpha.1 → 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/cli.js +41 -5
- package/package.json +3 -20
- package/src/fetch.js +117 -39
- package/src/index.js +11 -4
- package/src/shims.js +88 -10
- package/types/fetch.d.ts +69 -38
- package/types/shims.d.ts +12 -0
- package/types/vite/sveltekit.d.ts +16 -0
- package/types/vite/vite.d.ts +6 -30
- package/vite/sveltekit.js +61 -8
- 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/types/fetch.d.ts
CHANGED
|
@@ -1,56 +1,87 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @param {string | { url
|
|
2
|
+
* @param {string | Request | { url?: string, href?: string }} input
|
|
3
3
|
* @param {{ method?: string, headers?: unknown, body?: string | ArrayBuffer | ArrayBufferView | null }} [init]
|
|
4
|
-
* @returns {Promise<
|
|
4
|
+
* @returns {Promise<Response>}
|
|
5
5
|
*/
|
|
6
|
-
export function fetch(input: string | {
|
|
7
|
-
url
|
|
8
|
-
|
|
9
|
-
href: string;
|
|
6
|
+
export function fetch(input: string | Request | {
|
|
7
|
+
url?: string;
|
|
8
|
+
href?: string;
|
|
10
9
|
}, init?: {
|
|
11
10
|
method?: string;
|
|
12
11
|
headers?: unknown;
|
|
13
12
|
body?: string | ArrayBuffer | ArrayBufferView | null;
|
|
14
|
-
}): Promise<
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
13
|
+
}): Promise<Response>;
|
|
14
|
+
/** Headers: names are case-insensitive and kept in lower case, as in the Fetch API. */
|
|
15
|
+
export class Headers {
|
|
16
|
+
/** @param {unknown} [init] a plain object, a list of pairs, or another `Headers` */
|
|
17
|
+
constructor(init?: unknown);
|
|
18
|
+
/** @param {string} name @param {string} value */
|
|
19
|
+
append(name: string, value: string): void;
|
|
20
|
+
/** @param {string} name @param {string} value */
|
|
21
|
+
set(name: string, value: string): void;
|
|
22
|
+
/** @param {string} name */
|
|
23
|
+
get(name: string): string | null;
|
|
24
|
+
/** @param {string} name */
|
|
25
|
+
has(name: string): boolean;
|
|
26
|
+
/** @param {string} name */
|
|
27
|
+
delete(name: string): void;
|
|
28
|
+
/** @param {(value: string, name: string, headers: Headers) => void} callback */
|
|
29
|
+
forEach(callback: (value: string, name: string, headers: Headers) => void): void;
|
|
30
|
+
entries(): MapIterator<[string, string]>;
|
|
31
|
+
keys(): MapIterator<string>;
|
|
32
|
+
values(): MapIterator<string>;
|
|
33
|
+
[Symbol.iterator](): MapIterator<[string, string]>;
|
|
34
|
+
#private;
|
|
35
|
+
}
|
|
36
|
+
/** A request to make: what `fetch` takes, as a string, a URL or one of these. */
|
|
37
|
+
export class Request {
|
|
38
|
+
/**
|
|
39
|
+
* @param {string | Request | { url?: string, href?: string }} input
|
|
40
|
+
* @param {{ method?: string, headers?: unknown, body?: string | ArrayBuffer | ArrayBufferView | null }} [init]
|
|
41
|
+
*/
|
|
42
|
+
constructor(input: string | Request | {
|
|
43
|
+
url?: string;
|
|
44
|
+
href?: string;
|
|
45
|
+
}, init?: {
|
|
46
|
+
method?: string;
|
|
47
|
+
headers?: unknown;
|
|
48
|
+
body?: string | ArrayBuffer | ArrayBufferView | null;
|
|
49
|
+
});
|
|
50
|
+
/** @type {string} */
|
|
51
|
+
url: string;
|
|
52
|
+
/** @type {string} */
|
|
53
|
+
method: string;
|
|
54
|
+
/** @type {Headers} */
|
|
55
|
+
headers: Headers;
|
|
56
|
+
/** @type {string | ArrayBuffer | ArrayBufferView | null} */
|
|
57
|
+
body: string | ArrayBuffer | ArrayBufferView | null;
|
|
58
|
+
}
|
|
59
|
+
/** A response with its body in hand: what `new Response(body, init)` makes. `fetch` answers with a subclass. */
|
|
60
|
+
export class Response {
|
|
61
|
+
/**
|
|
62
|
+
* @param {string | ArrayBuffer | ArrayBufferView | null} [body]
|
|
63
|
+
* @param {{ status?: number, statusText?: string, headers?: unknown }} [init]
|
|
64
|
+
*/
|
|
65
|
+
constructor(body?: string | ArrayBuffer | ArrayBufferView | null, init?: {
|
|
66
|
+
status?: number;
|
|
67
|
+
statusText?: string;
|
|
68
|
+
headers?: unknown;
|
|
24
69
|
});
|
|
25
70
|
status: number;
|
|
26
71
|
statusText: string;
|
|
27
72
|
ok: boolean;
|
|
73
|
+
headers: Headers;
|
|
28
74
|
url: string;
|
|
29
75
|
redirected: boolean;
|
|
30
|
-
headers: ResponseHeaders;
|
|
31
76
|
bodyUsed: boolean;
|
|
32
|
-
/**
|
|
33
|
-
|
|
34
|
-
/** @
|
|
35
|
-
|
|
77
|
+
/** @type {string | Uint8Array | null} */
|
|
78
|
+
held: string | Uint8Array | null;
|
|
79
|
+
/** @returns {string | Uint8Array} */
|
|
80
|
+
take(): string | Uint8Array;
|
|
81
|
+
/** @returns {Promise<string>} */
|
|
36
82
|
text(): Promise<string>;
|
|
37
83
|
json(): Promise<any>;
|
|
38
|
-
|
|
84
|
+
/** @returns {Promise<Uint8Array>} */
|
|
85
|
+
bytes(): Promise<Uint8Array>;
|
|
39
86
|
arrayBuffer(): Promise<ArrayBuffer>;
|
|
40
87
|
}
|
|
41
|
-
/** Response headers: names are case-insensitive, as in the Fetch API. */
|
|
42
|
-
declare class ResponseHeaders {
|
|
43
|
-
/** @param {[string, string][]} pairs */
|
|
44
|
-
constructor(pairs: [string, string][]);
|
|
45
|
-
/** @type {Map<string, string>} */
|
|
46
|
-
values: Map<string, string>;
|
|
47
|
-
/** @param {string} name */
|
|
48
|
-
get(name: string): string | null;
|
|
49
|
-
/** @param {string} name */
|
|
50
|
-
has(name: string): boolean;
|
|
51
|
-
/** @param {(value: string, name: string) => void} callback */
|
|
52
|
-
forEach(callback: (value: string, name: string) => void): void;
|
|
53
|
-
entries(): MapIterator<[string, string]>;
|
|
54
|
-
[Symbol.iterator](): MapIterator<[string, string]>;
|
|
55
|
-
}
|
|
56
|
-
export {};
|
package/types/shims.d.ts
CHANGED
|
@@ -20,10 +20,22 @@ export function run_timers(): boolean;
|
|
|
20
20
|
* @returns {number} the earliest deadline on the host clock, or -1 if no timer is pending
|
|
21
21
|
*/
|
|
22
22
|
export function next_timer(): number;
|
|
23
|
+
/**
|
|
24
|
+
* Run one callback of this frame, and say whether there was one. As with
|
|
25
|
+
* timers, the host calls this until the answer is no, and runs microtasks
|
|
26
|
+
* after every call. Every callback of a frame gets the same `time`; one
|
|
27
|
+
* queued during the frame runs in the next.
|
|
28
|
+
*
|
|
29
|
+
* @param {number} time the frame's timestamp, on the host clock
|
|
30
|
+
* @returns {boolean}
|
|
31
|
+
*/
|
|
32
|
+
export function run_frame(time: number): boolean;
|
|
23
33
|
export function rejection_count(): number;
|
|
34
|
+
export function frame_pending(): boolean;
|
|
24
35
|
export type Timer = {
|
|
25
36
|
fn: Function;
|
|
26
37
|
args: any[];
|
|
27
38
|
at: number;
|
|
28
39
|
every: number;
|
|
40
|
+
level: number;
|
|
29
41
|
};
|
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The files under `src` that SvelteKit would run on a server, which an app
|
|
3
|
+
* does not have: they would build, and then never run. Relative to `src`.
|
|
4
|
+
*
|
|
5
|
+
* ponytail: comes out when the backend thread lands; that is where they will run.
|
|
6
|
+
* @param {string} src
|
|
7
|
+
*/
|
|
8
|
+
export function server_files(src: string): string[];
|
|
1
9
|
/**
|
|
2
10
|
* Turn what SvelteKit built (an HTML page that imports one module) into what
|
|
3
11
|
* the host loads (a script, a stylesheet, and the other files as they are).
|
|
@@ -5,6 +13,14 @@
|
|
|
5
13
|
* @param {string} to
|
|
6
14
|
*/
|
|
7
15
|
export function convert(from: string, to: string): void;
|
|
16
|
+
/**
|
|
17
|
+
* A stylesheet moved from `dir` (relative to the build, `/`-separated) to
|
|
18
|
+
* app.css at its root. Its relative `url()`s, such as a font next to it in
|
|
19
|
+
* `_app/immutable/assets`, have to say where they now are from there.
|
|
20
|
+
* @param {string} css
|
|
21
|
+
* @param {string} dir
|
|
22
|
+
*/
|
|
23
|
+
export function rebase_css(css: string, dir: string): string;
|
|
8
24
|
/**
|
|
9
25
|
* @typedef {import('./vite.js').Config & {
|
|
10
26
|
* kit?: import('@sveltejs/kit/vite').Config,
|
package/types/vite/vite.d.ts
CHANGED
|
@@ -22,19 +22,6 @@
|
|
|
22
22
|
* window?: { title?: string, width?: number, height?: number }
|
|
23
23
|
* }} AppManifest
|
|
24
24
|
*/
|
|
25
|
-
/**
|
|
26
|
-
* Routing. Leave it out for the default: the built-in router when the
|
|
27
|
-
* project has a `src/routes` directory, no router when it does not.
|
|
28
|
-
*
|
|
29
|
-
* - `false` turns routing off even if `src/routes` exists.
|
|
30
|
-
* - `dir` is where the route files are (default `src/routes`).
|
|
31
|
-
* - `entry` is a router of your own: a module that becomes the app's entry.
|
|
32
|
-
* It mounts the app itself, and can import the generated route table
|
|
33
|
-
* (`routes`, `fallback`) from `virtual:sepal/routes`. The built-in
|
|
34
|
-
* `$app/navigation` and `$app/state` are not set up for it.
|
|
35
|
-
*
|
|
36
|
-
* @typedef {false | { dir?: string, entry?: string }} RouterOptions
|
|
37
|
-
*/
|
|
38
25
|
/**
|
|
39
26
|
* What the app may do outside its window. Everything is off unless it is
|
|
40
27
|
* listed here; a call that is not allowed rejects with a message naming the
|
|
@@ -69,10 +56,12 @@
|
|
|
69
56
|
* (`window`, `document`, element methods; see `sepal/dom`)
|
|
70
57
|
* before anything else, so those libraries find what they reach for.
|
|
71
58
|
*
|
|
59
|
+
* `entry` is the module the app starts from (default `src/main.js` or
|
|
60
|
+
* `src/main.ts`). It mounts the app; a router of your own goes there.
|
|
61
|
+
*
|
|
72
62
|
* @typedef {{
|
|
73
63
|
* app?: AppManifest,
|
|
74
64
|
* permissions?: Permissions,
|
|
75
|
-
* router?: RouterOptions,
|
|
76
65
|
* entry?: string,
|
|
77
66
|
* dom?: boolean
|
|
78
67
|
* }} Config
|
|
@@ -124,21 +113,6 @@ export type AppManifest = {
|
|
|
124
113
|
height?: number;
|
|
125
114
|
};
|
|
126
115
|
};
|
|
127
|
-
/**
|
|
128
|
-
* Routing. Leave it out for the default: the built-in router when the
|
|
129
|
-
* project has a `src/routes` directory, no router when it does not.
|
|
130
|
-
*
|
|
131
|
-
* - `false` turns routing off even if `src/routes` exists.
|
|
132
|
-
* - `dir` is where the route files are (default `src/routes`).
|
|
133
|
-
* - `entry` is a router of your own: a module that becomes the app's entry.
|
|
134
|
-
* It mounts the app itself, and can import the generated route table
|
|
135
|
-
* (`routes`, `fallback`) from `virtual:sepal/routes`. The built-in
|
|
136
|
-
* `$app/navigation` and `$app/state` are not set up for it.
|
|
137
|
-
*/
|
|
138
|
-
export type RouterOptions = false | {
|
|
139
|
-
dir?: string;
|
|
140
|
-
entry?: string;
|
|
141
|
-
};
|
|
142
116
|
/**
|
|
143
117
|
* What the app may do outside its window. Everything is off unless it is
|
|
144
118
|
* listed here; a call that is not allowed rejects with a message naming the
|
|
@@ -171,11 +145,13 @@ export type Permissions = {
|
|
|
171
145
|
* It builds the app as a browser bundle and loads a small DOM
|
|
172
146
|
* (`window`, `document`, element methods; see `sepal/dom`)
|
|
173
147
|
* before anything else, so those libraries find what they reach for.
|
|
148
|
+
*
|
|
149
|
+
* `entry` is the module the app starts from (default `src/main.js` or
|
|
150
|
+
* `src/main.ts`). It mounts the app; a router of your own goes there.
|
|
174
151
|
*/
|
|
175
152
|
export type Config = {
|
|
176
153
|
app?: AppManifest;
|
|
177
154
|
permissions?: Permissions;
|
|
178
|
-
router?: RouterOptions;
|
|
179
155
|
entry?: string;
|
|
180
156
|
dom?: boolean;
|
|
181
157
|
};
|
package/vite/sveltekit.js
CHANGED
|
@@ -18,8 +18,8 @@
|
|
|
18
18
|
//
|
|
19
19
|
// Runs in Node.
|
|
20
20
|
|
|
21
|
-
import { cpSync, existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from 'node:fs';
|
|
22
|
-
import { dirname, join, relative, resolve } from 'node:path';
|
|
21
|
+
import { cpSync, existsSync, mkdirSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs';
|
|
22
|
+
import { dirname, join, posix, relative, resolve } from 'node:path';
|
|
23
23
|
import { dev_window, load_config, manifest } from './app.js';
|
|
24
24
|
import { element_bindings } from './bindings.js';
|
|
25
25
|
import { head } from './head.js';
|
|
@@ -59,6 +59,18 @@ function files_in(directory) {
|
|
|
59
59
|
.map((entry) => relative(directory, join(entry.parentPath, entry.name)).replaceAll('\\', '/'));
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
+
/**
|
|
63
|
+
* The files under `src` that SvelteKit would run on a server, which an app
|
|
64
|
+
* does not have: they would build, and then never run. Relative to `src`.
|
|
65
|
+
*
|
|
66
|
+
* ponytail: comes out when the backend thread lands; that is where they will run.
|
|
67
|
+
* @param {string} src
|
|
68
|
+
*/
|
|
69
|
+
export function server_files(src) {
|
|
70
|
+
if (!existsSync(src)) return [];
|
|
71
|
+
return files_in(src).filter((file) => /(^|\/)(\+(page|layout)\.server\.|\+server\.|hooks\.server\.)|\.remote\.[^/]+$/.test(file));
|
|
72
|
+
}
|
|
73
|
+
|
|
62
74
|
/**
|
|
63
75
|
* Turn what SvelteKit built (an HTML page that imports one module) into what
|
|
64
76
|
* the host loads (a script, a stylesheet, and the other files as they are).
|
|
@@ -98,19 +110,28 @@ export function convert(from, to) {
|
|
|
98
110
|
writeFileSync(
|
|
99
111
|
join(to, 'app.js'),
|
|
100
112
|
`(() => {
|
|
113
|
+
// The bundle was a module, and module code is strict. Run as a sloppy script,
|
|
114
|
+
// the minified Tracker broke on V8 (a renderer method missing on Insights).
|
|
115
|
+
'use strict';
|
|
101
116
|
const __module_url = ${JSON.stringify(`http://app.localhost/${bundle}`)};
|
|
102
117
|
${code}
|
|
103
118
|
globalThis.${payload}
|
|
104
119
|
// Where SvelteKit puts the app: the element its start script sat in.
|
|
105
|
-
const
|
|
106
|
-
document.body.appendChild(
|
|
107
|
-
${start}(
|
|
120
|
+
const __sepal_target = document.createElement('div');
|
|
121
|
+
document.body.appendChild(__sepal_target);
|
|
122
|
+
${start}(__sepal_target);
|
|
108
123
|
})();
|
|
109
124
|
`
|
|
110
125
|
);
|
|
111
126
|
|
|
112
127
|
const styles = [...html.matchAll(/<link href="\/([^"]+)" rel="stylesheet">/g)].map((link) => link[1]).filter((file) => files.includes(file));
|
|
113
|
-
|
|
128
|
+
const css = styles.map((file) => rebase_css(readFileSync(join(from, file), 'utf8'), posix.dirname(file)));
|
|
129
|
+
// SvelteKit's route announcer is a 1px `overflow: hidden` box at the top
|
|
130
|
+
// left. Blitz hit-tests what overflows such a box, so after the first
|
|
131
|
+
// navigation its invisible text took clicks meant for what is drawn there.
|
|
132
|
+
// ponytail: until Blitz's hit testing clips to `overflow: hidden`; then this goes.
|
|
133
|
+
css.push('#svelte-announcer { pointer-events: none }');
|
|
134
|
+
writeFileSync(join(to, 'app.css'), css.join('\n'));
|
|
114
135
|
|
|
115
136
|
// Everything else is for the app to load by its path: static files, and
|
|
116
137
|
// what SvelteKit put in `_app/immutable/assets`.
|
|
@@ -121,6 +142,20 @@ ${start}(element);
|
|
|
121
142
|
}
|
|
122
143
|
}
|
|
123
144
|
|
|
145
|
+
/**
|
|
146
|
+
* A stylesheet moved from `dir` (relative to the build, `/`-separated) to
|
|
147
|
+
* app.css at its root. Its relative `url()`s, such as a font next to it in
|
|
148
|
+
* `_app/immutable/assets`, have to say where they now are from there.
|
|
149
|
+
* @param {string} css
|
|
150
|
+
* @param {string} dir
|
|
151
|
+
*/
|
|
152
|
+
export function rebase_css(css, dir) {
|
|
153
|
+
return css.replace(
|
|
154
|
+
/url\(\s*(['"]?)(?![a-z][a-z0-9+.-]*:|\/|#)([^'")]+)\1\s*\)/gi,
|
|
155
|
+
(_, quote, path) => `url(${quote}${posix.join(dir, path)}${quote})`
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
|
|
124
159
|
/**
|
|
125
160
|
* @typedef {import('./vite.js').Config & {
|
|
126
161
|
* kit?: import('@sveltejs/kit/vite').Config,
|
|
@@ -145,6 +180,8 @@ export async function sepalKit(inline = {}) {
|
|
|
145
180
|
let root = process.cwd();
|
|
146
181
|
let watching = false;
|
|
147
182
|
const show = dev_window(() => ({ root, out_dir: resolve(root, out), name: options.app?.name }));
|
|
183
|
+
// Deprecated in SvelteKit 3's types, but still the only way to move `static/`:
|
|
184
|
+
// the top-level `assets` is `paths.assets`, a URL.
|
|
148
185
|
const static_dir = () => resolve(root, kit.files?.assets ?? 'static');
|
|
149
186
|
|
|
150
187
|
/** @type {import('@sveltejs/kit').Adapter} */
|
|
@@ -153,8 +190,9 @@ export async function sepalKit(inline = {}) {
|
|
|
153
190
|
async adapt(builder) {
|
|
154
191
|
const staging = builder.getBuildDirectory('sepal');
|
|
155
192
|
const to = resolve(root, out);
|
|
156
|
-
builder.rimraf
|
|
157
|
-
|
|
193
|
+
// `builder.rimraf` is deprecated in SvelteKit 3.
|
|
194
|
+
rmSync(staging, { force: true, recursive: true });
|
|
195
|
+
rmSync(to, { force: true, recursive: true });
|
|
158
196
|
builder.writeClient(staging);
|
|
159
197
|
// The page SvelteKit serves for a route it did not prerender: nothing
|
|
160
198
|
// in it but the script that starts the app.
|
|
@@ -188,6 +226,17 @@ export async function sepalKit(inline = {}) {
|
|
|
188
226
|
root = config.root;
|
|
189
227
|
watching = config.build.watch != null;
|
|
190
228
|
},
|
|
229
|
+
buildStart() {
|
|
230
|
+
// On every build, so that a file added in watch mode is caught too.
|
|
231
|
+
const src = resolve(root, kit.files?.src ?? 'src');
|
|
232
|
+
const found = server_files(src);
|
|
233
|
+
if (found.length > 0) {
|
|
234
|
+
const names = found.map((file) => posix.join(relative(root, src).replaceAll('\\', '/'), file)).join(', ');
|
|
235
|
+
this.error(
|
|
236
|
+
`sepal: ${names} ${found.length === 1 ? 'is' : 'are'} server code. It would run in the app's backend thread, which Sepal does not have yet, so it would never run. Use +page.ts/+layout.ts (\`load\` runs in the app) until then.`
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
},
|
|
191
240
|
transform(code, id) {
|
|
192
241
|
const edit = patch.find(({ file }) => id.endsWith(file));
|
|
193
242
|
if (edit === undefined) return;
|
|
@@ -207,6 +256,10 @@ export async function sepalKit(inline = {}) {
|
|
|
207
256
|
output: { ...kit.output, bundleStrategy: 'single' },
|
|
208
257
|
// Nothing is rendered ahead of time. There is no server to send it.
|
|
209
258
|
prerender: { ...kit.prerender, entries: kit.prerender?.entries ?? [] },
|
|
259
|
+
// SvelteKit 3 asks the server for a new version every hour by default:
|
|
260
|
+
// a wake-up on a timer, and a request nothing answers. An app updates
|
|
261
|
+
// through `sepal/updater` instead.
|
|
262
|
+
version: { ...kit.version, pollInterval: kit.version?.pollInterval ?? 0 },
|
|
210
263
|
// Ours first: they have to see `bind:` on elements and `<svelte:head>`
|
|
211
264
|
// before Svelte refuses them.
|
|
212
265
|
preprocess: [element_bindings(), head(), ...[kit.preprocess ?? []].flat()],
|
package/vite/vite.js
CHANGED
|
@@ -6,27 +6,20 @@
|
|
|
6
6
|
// root, so that `vite.config.ts` stays a list of plugins. Options passed to
|
|
7
7
|
// `sepal()` directly override the file, key by key.
|
|
8
8
|
//
|
|
9
|
-
//
|
|
10
|
-
//
|
|
11
|
-
//
|
|
12
|
-
// swaps in a router of your own, or turns routing off.
|
|
9
|
+
// The entry is `src/main.js` or `src/main.ts`, or the `entry` option. An app
|
|
10
|
+
// with pages in `src/routes` is a SvelteKit app: it uses `sepalKit()` from
|
|
11
|
+
// `sepal/sveltekit` instead of this preset.
|
|
13
12
|
//
|
|
14
13
|
// In watch mode (`vite build --watch`) with a host binary available, it also
|
|
15
14
|
// opens the app and reloads it in place after every rebuild.
|
|
16
15
|
|
|
17
16
|
import { existsSync, readFileSync } from 'node:fs';
|
|
18
17
|
import { join, resolve } from 'node:path';
|
|
19
|
-
import { fileURLToPath } from 'node:url';
|
|
20
18
|
import { svelte } from '@sveltejs/vite-plugin-svelte';
|
|
21
19
|
import { normalizePath } from 'vite';
|
|
22
20
|
import { dev_window, load_config, manifest } from './app.js';
|
|
23
21
|
import { element_bindings } from './bindings.js';
|
|
24
22
|
import { head } from './head.js';
|
|
25
|
-
import { generate_routes } from './routes.js';
|
|
26
|
-
|
|
27
|
-
const ROUTES_ID = 'virtual:sepal/routes';
|
|
28
|
-
/** @param {string} file a file in this package's `src` */
|
|
29
|
-
const own = (file) => fileURLToPath(new URL(file, import.meta.url));
|
|
30
23
|
|
|
31
24
|
/**
|
|
32
25
|
* What the app tells the host about itself. Written to `dist/app.json`.
|
|
@@ -53,20 +46,6 @@ const own = (file) => fileURLToPath(new URL(file, import.meta.url));
|
|
|
53
46
|
* }} AppManifest
|
|
54
47
|
*/
|
|
55
48
|
|
|
56
|
-
/**
|
|
57
|
-
* Routing. Leave it out for the default: the built-in router when the
|
|
58
|
-
* project has a `src/routes` directory, no router when it does not.
|
|
59
|
-
*
|
|
60
|
-
* - `false` turns routing off even if `src/routes` exists.
|
|
61
|
-
* - `dir` is where the route files are (default `src/routes`).
|
|
62
|
-
* - `entry` is a router of your own: a module that becomes the app's entry.
|
|
63
|
-
* It mounts the app itself, and can import the generated route table
|
|
64
|
-
* (`routes`, `fallback`) from `virtual:sepal/routes`. The built-in
|
|
65
|
-
* `$app/navigation` and `$app/state` are not set up for it.
|
|
66
|
-
*
|
|
67
|
-
* @typedef {false | { dir?: string, entry?: string }} RouterOptions
|
|
68
|
-
*/
|
|
69
|
-
|
|
70
49
|
/**
|
|
71
50
|
* What the app may do outside its window. Everything is off unless it is
|
|
72
51
|
* listed here; a call that is not allowed rejects with a message naming the
|
|
@@ -102,10 +81,12 @@ const own = (file) => fileURLToPath(new URL(file, import.meta.url));
|
|
|
102
81
|
* (`window`, `document`, element methods; see `sepal/dom`)
|
|
103
82
|
* before anything else, so those libraries find what they reach for.
|
|
104
83
|
*
|
|
84
|
+
* `entry` is the module the app starts from (default `src/main.js` or
|
|
85
|
+
* `src/main.ts`). It mounts the app; a router of your own goes there.
|
|
86
|
+
*
|
|
105
87
|
* @typedef {{
|
|
106
88
|
* app?: AppManifest,
|
|
107
89
|
* permissions?: Permissions,
|
|
108
|
-
* router?: RouterOptions,
|
|
109
90
|
* entry?: string,
|
|
110
91
|
* dom?: boolean
|
|
111
92
|
* }} Config
|
|
@@ -135,7 +116,6 @@ export function sepal(inline = {}) {
|
|
|
135
116
|
let config_path = null;
|
|
136
117
|
/** @type {import('vite').ConfigEnv} */
|
|
137
118
|
let config_env = { mode: 'production', command: 'build' };
|
|
138
|
-
let routes_dir = '';
|
|
139
119
|
/** The entry module, absolute and without extension, when `dom` is on. */
|
|
140
120
|
let dom_entry = '';
|
|
141
121
|
const user = inline.svelte ?? {};
|
|
@@ -172,17 +152,14 @@ export function sepal(inline = {}) {
|
|
|
172
152
|
config_path = file.path;
|
|
173
153
|
options = { ...file.config, ...inline };
|
|
174
154
|
|
|
175
|
-
const
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
? own('./router-entry.js')
|
|
184
|
-
: // No extension: Vite resolves `src/main.js` or `src/main.ts`, whichever exists.
|
|
185
|
-
(options.entry ?? 'src/main');
|
|
155
|
+
const has_main = ['js', 'ts'].some((ext) => existsSync(join(project, `src/main.${ext}`)));
|
|
156
|
+
if (options.entry === undefined && !has_main && existsSync(join(project, 'src/routes'))) {
|
|
157
|
+
throw new Error(
|
|
158
|
+
"sepal: this app has pages in src/routes and no src/main. Pages are SvelteKit's: in vite.config.ts, use `sepalKit()` from 'sepal/sveltekit' in place of `sepal()`, and add a src/routes/+layout.ts with `export const ssr = false;`."
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
// No extension: Vite resolves `src/main.js` or `src/main.ts`, whichever exists.
|
|
162
|
+
const entry = options.entry ?? 'src/main';
|
|
186
163
|
|
|
187
164
|
// Vite's module ids use `/` on Windows as well.
|
|
188
165
|
if (options.dom) dom_entry = normalizePath(resolve(project, entry)).replace(/\.[cm]?[jt]s$/, '');
|
|
@@ -197,14 +174,8 @@ export function sepal(inline = {}) {
|
|
|
197
174
|
// `dom`, `browser` as well: libraries check it at build time to
|
|
198
175
|
// decide whether there is a DOM to work with.
|
|
199
176
|
conditions: ['svelte-custom-renderer', ...(options.dom ? ['browser'] : []), 'svelte', 'module', 'import', 'default'],
|
|
200
|
-
// The
|
|
201
|
-
|
|
202
|
-
alias: {
|
|
203
|
-
$lib: join(project, 'src/lib'),
|
|
204
|
-
...(built_in_router
|
|
205
|
-
? { '$app/navigation': own('./navigation.js'), '$app/state': own('./state.js') }
|
|
206
|
-
: {})
|
|
207
|
-
}
|
|
177
|
+
// The name SvelteKit 2 projects use for `src/lib`.
|
|
178
|
+
alias: { $lib: join(project, 'src/lib') }
|
|
208
179
|
},
|
|
209
180
|
build: {
|
|
210
181
|
target: 'es2022',
|
|
@@ -215,13 +186,13 @@ export function sepal(inline = {}) {
|
|
|
215
186
|
formats: ['iife'],
|
|
216
187
|
fileName: () => 'app.js',
|
|
217
188
|
cssFileName: 'app'
|
|
218
|
-
}
|
|
189
|
+
},
|
|
190
|
+
// The sources are modules, and module code is strict; the IIFE
|
|
191
|
+
// keeps it so. Rolldown's default only keeps directives it finds.
|
|
192
|
+
rolldownOptions: { output: { strict: true } }
|
|
219
193
|
}
|
|
220
194
|
};
|
|
221
195
|
},
|
|
222
|
-
resolveId(id) {
|
|
223
|
-
if (id === ROUTES_ID) return '\0' + ROUTES_ID;
|
|
224
|
-
},
|
|
225
196
|
transform(code, id) {
|
|
226
197
|
// The DOM has to exist before any library module runs, so it is the
|
|
227
198
|
// entry module's first import.
|
|
@@ -229,16 +200,6 @@ export function sepal(inline = {}) {
|
|
|
229
200
|
return { code: `import 'sepal/dom';\n${code}`, map: null };
|
|
230
201
|
}
|
|
231
202
|
},
|
|
232
|
-
load(id) {
|
|
233
|
-
if (id !== '\0' + ROUTES_ID) return;
|
|
234
|
-
// A custom router may not use route files at all.
|
|
235
|
-
if (!existsSync(routes_dir)) return 'export const routes = [];\nexport const fallback = { layouts: [], error: null };\n';
|
|
236
|
-
const { code, files } = generate_routes(routes_dir);
|
|
237
|
-
// Rebuild when a route file is added, removed or changed.
|
|
238
|
-
this.addWatchFile(routes_dir);
|
|
239
|
-
for (const file of files) this.addWatchFile(file);
|
|
240
|
-
return code;
|
|
241
|
-
},
|
|
242
203
|
configResolved(config) {
|
|
243
204
|
root = config.root;
|
|
244
205
|
static_dir = config.publicDir;
|
|
@@ -248,11 +209,11 @@ export function sepal(inline = {}) {
|
|
|
248
209
|
async buildStart() {
|
|
249
210
|
// In watch mode, pick up edits to the config file. What it says about
|
|
250
211
|
// the app and its permissions takes effect on the next build; a
|
|
251
|
-
// change to `
|
|
212
|
+
// change to `entry` needs Vite restarted.
|
|
252
213
|
if (config_path === null) return;
|
|
253
214
|
this.addWatchFile(config_path);
|
|
254
215
|
const file = await load_config(root, config_env);
|
|
255
|
-
options = { ...file.config, ...inline,
|
|
216
|
+
options = { ...file.config, ...inline, entry: options.entry };
|
|
256
217
|
},
|
|
257
218
|
generateBundle() {
|
|
258
219
|
let written;
|
package/ambient.d.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
// Module names the Vite preset provides. Add
|
|
2
|
-
// "sepal/ambient" to `types` in tsconfig.json.
|
|
3
|
-
|
|
4
|
-
declare module '$app/navigation' {
|
|
5
|
-
export * from 'sepal/navigation';
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
declare module '$app/state' {
|
|
9
|
-
export * from 'sepal/state';
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
// The route table generated from the routes directory. The built-in router
|
|
13
|
-
// runs on it; a router of your own can import it too.
|
|
14
|
-
declare module 'virtual:sepal/routes' {
|
|
15
|
-
import type { Route, RouteNode } from 'sepal/router';
|
|
16
|
-
import type { Component } from 'svelte';
|
|
17
|
-
|
|
18
|
-
export const routes: Route[];
|
|
19
|
-
export const fallback: { layouts: RouteNode[]; error: Component<any> | null };
|
|
20
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { goto, back, forward } from "./routing.svelte.js";
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/types/vite/router.d.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* For typing `load` functions, and for writing a router of your own against
|
|
3
|
-
* `virtual:sepal/routes`.
|
|
4
|
-
*/
|
|
5
|
-
export type LoadEvent = import("./routing.svelte.js").LoadEvent;
|
|
6
|
-
/**
|
|
7
|
-
* For typing `load` functions, and for writing a router of your own against
|
|
8
|
-
* `virtual:sepal/routes`.
|
|
9
|
-
*/
|
|
10
|
-
export type PageUrl = import("./routing.svelte.js").PageUrl;
|
|
11
|
-
/**
|
|
12
|
-
* For typing `load` functions, and for writing a router of your own against
|
|
13
|
-
* `virtual:sepal/routes`.
|
|
14
|
-
*/
|
|
15
|
-
export type Route = import("./routing.svelte.js").Route;
|
|
16
|
-
/**
|
|
17
|
-
* For typing `load` functions, and for writing a router of your own against
|
|
18
|
-
* `virtual:sepal/routes`.
|
|
19
|
-
*/
|
|
20
|
-
export type RouteNode = import("./routing.svelte.js").RouteNode;
|
|
21
|
-
export { error, redirect, goto, back, forward, page } from "./routing.svelte.js";
|
package/types/vite/routes.d.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @param {string} dir absolute path of `src/routes`
|
|
3
|
-
* @returns {{ routes: ScannedRoute[], fallback: { layouts: NodeFiles[], error: string | null }, files: string[] }}
|
|
4
|
-
*/
|
|
5
|
-
export function scan_routes(dir: string): {
|
|
6
|
-
routes: ScannedRoute[];
|
|
7
|
-
fallback: {
|
|
8
|
-
layouts: NodeFiles[];
|
|
9
|
-
error: string | null;
|
|
10
|
-
};
|
|
11
|
-
files: string[];
|
|
12
|
-
};
|
|
13
|
-
/**
|
|
14
|
-
* @param {string[]} segments
|
|
15
|
-
* @returns {{ pattern: string, names: string[] }} `pattern` is regular expression source
|
|
16
|
-
*/
|
|
17
|
-
export function route_pattern(segments: string[]): {
|
|
18
|
-
pattern: string;
|
|
19
|
-
names: string[];
|
|
20
|
-
};
|
|
21
|
-
/**
|
|
22
|
-
* The source of `virtual:sepal/routes`.
|
|
23
|
-
* @param {string} dir absolute path of `src/routes`
|
|
24
|
-
* @returns {{ code: string, files: string[] }} `files` are what the module was built from
|
|
25
|
-
*/
|
|
26
|
-
export function generate_routes(dir: string): {
|
|
27
|
-
code: string;
|
|
28
|
-
files: string[];
|
|
29
|
-
};
|
|
30
|
-
export type NodeFiles = {
|
|
31
|
-
component: string | null;
|
|
32
|
-
module: string | null;
|
|
33
|
-
};
|
|
34
|
-
export type ScannedRoute = {
|
|
35
|
-
id: string;
|
|
36
|
-
segments: string[];
|
|
37
|
-
layouts: NodeFiles[];
|
|
38
|
-
page: NodeFiles;
|
|
39
|
-
error: string | null;
|
|
40
|
-
error_depth: number;
|
|
41
|
-
};
|