sepal 0.2.0-alpha.0 → 0.3.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 +14 -5
- package/cli.js +127 -10
- package/package.json +48 -4
- package/src/abort.js +129 -0
- package/src/app.js +13 -0
- package/src/dom.js +173 -60
- package/src/fetch.js +347 -80
- package/src/form-data.js +141 -0
- package/src/host-call.js +57 -0
- package/src/index.js +9 -11
- package/src/lru.js +45 -0
- package/src/native.js +32 -0
- package/src/notifications.js +60 -0
- package/src/os.js +35 -0
- package/src/process.js +164 -0
- package/src/protocol.js +3 -4
- package/src/renderer.js +140 -10
- package/src/secrets.js +42 -0
- package/src/settings.js +52 -0
- package/src/shims.js +30 -8
- package/src/stem.js +125 -0
- package/src/updater.js +3 -2
- package/src/url.js +65 -20
- package/src/web.js +86 -0
- package/src/window.js +6 -2
- package/types/abort.d.ts +46 -0
- package/types/app.d.ts +7 -0
- package/types/fetch.d.ts +118 -28
- package/types/form-data.d.ts +19 -0
- package/types/host-call.d.ts +18 -0
- package/types/index.d.ts +5 -2
- package/types/lru.d.ts +18 -0
- package/types/native.d.ts +22 -0
- package/types/notifications.d.ts +53 -0
- package/types/os.d.ts +53 -0
- package/types/process.d.ts +90 -0
- package/types/protocol.d.ts +0 -2
- package/types/renderer.d.ts +16 -1
- package/types/secrets.d.ts +22 -0
- package/types/settings.d.ts +29 -0
- package/types/shims.d.ts +1 -0
- package/types/stem.d.ts +21 -0
- package/types/updater.d.ts +6 -4
- package/types/url.d.ts +1 -1
- package/types/vite/adapter.d.ts +22 -0
- package/types/vite/add.d.ts +77 -0
- package/types/vite/app.d.ts +52 -1
- package/types/vite/licenses.d.ts +11 -0
- package/types/vite/native.d.ts +135 -0
- package/types/vite/page.d.ts +20 -0
- package/types/vite/permissions.d.ts +56 -0
- package/types/vite/sveltekit.d.ts +5 -42
- package/types/vite/vite.d.ts +86 -29
- package/types/web.d.ts +17 -0
- package/types/window.d.ts +5 -1
- package/vite/adapter.js +118 -0
- package/vite/add.js +254 -0
- package/vite/app.js +195 -27
- package/vite/head.js +2 -2
- package/vite/licenses.js +115 -0
- package/vite/native.js +535 -0
- package/vite/page.js +95 -0
- package/vite/permissions.d.ts +56 -0
- package/vite/sveltekit.js +11 -263
- package/vite/vite.js +378 -109
package/vite/vite.js
CHANGED
|
@@ -1,25 +1,65 @@
|
|
|
1
|
-
// Vite preset: compile Svelte for the Blitz renderer
|
|
2
|
-
// `
|
|
3
|
-
|
|
1
|
+
// Vite preset: compile Svelte for the Blitz renderer, as a web build: the
|
|
2
|
+
// app's `index.html`, the ES module chunks it loads, one stylesheet, the
|
|
3
|
+
// static files, and `app.json` for the host.
|
|
4
4
|
//
|
|
5
5
|
// Settings come from `sepal.config.ts` (or `.js`/`.mjs`) in the project
|
|
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
|
-
// The entry is `
|
|
10
|
-
//
|
|
11
|
-
//
|
|
9
|
+
// The entry is `index.html`, as for any Vite app; its module script
|
|
10
|
+
// (`src/main.ts` in the examples) mounts the app.
|
|
11
|
+
//
|
|
12
|
+
// A SvelteKit app (one with `src/app.html`) uses SvelteKit's own plugin,
|
|
13
|
+
// given its options through `withSepal()` (the custom renderer, no version
|
|
14
|
+
// polling, the rewrites of `bind:` and `<svelte:head>`) with
|
|
15
|
+
// `sepal/adapter`, and this preset after it, which adds what SvelteKit's
|
|
16
|
+
// options cannot: the custom renderer's build conditions, the edits to
|
|
17
|
+
// SvelteKit's client (see `patch`), `sepal.config.ts` and the dev window.
|
|
18
|
+
// See `KIT_SETUP`.
|
|
12
19
|
//
|
|
13
20
|
// In watch mode (`vite build --watch`) with a host binary available, it also
|
|
14
21
|
// opens the app and reloads it in place after every rebuild.
|
|
15
22
|
|
|
16
23
|
import { existsSync, readFileSync } from 'node:fs';
|
|
17
|
-
import { join, resolve } from 'node:path';
|
|
24
|
+
import { join, relative, resolve } from 'node:path';
|
|
18
25
|
import { svelte } from '@sveltejs/vite-plugin-svelte';
|
|
26
|
+
import { preprocess } from 'svelte/compiler';
|
|
19
27
|
import { normalizePath } from 'vite';
|
|
20
|
-
import { dev_window, load_config, manifest } from './app.js';
|
|
28
|
+
import { compile_bytecode, dev_window, kit, load_config, manifest } from './app.js';
|
|
21
29
|
import { element_bindings } from './bindings.js';
|
|
22
30
|
import { head } from './head.js';
|
|
31
|
+
import { check_css, check_page } from './page.js';
|
|
32
|
+
import { fileURLToPath } from 'node:url';
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* formdata-node's own classes, whatever the resolve conditions. With
|
|
36
|
+
* `browser` (the DOM layer, and every SvelteKit app) it resolves to a file
|
|
37
|
+
* that re-exports the browser's `FormData`, `Blob` and `File`, which the
|
|
38
|
+
* engine does not have: web.js installed `undefined` for all three.
|
|
39
|
+
*/
|
|
40
|
+
const FORMDATA = { find: /^formdata-node$/, replacement: fileURLToPath(import.meta.resolve('formdata-node')) };
|
|
41
|
+
|
|
42
|
+
/** A SvelteKit app's vite.config.ts, for the messages that point to it. */
|
|
43
|
+
export const KIT_SETUP = `import { sveltekit } from '@sveltejs/kit/vite';
|
|
44
|
+
import adapter from 'sepal/adapter';
|
|
45
|
+
import { sepal, withSepal } from 'sepal/vite';
|
|
46
|
+
import { defineConfig } from 'vite';
|
|
47
|
+
|
|
48
|
+
export default defineConfig({
|
|
49
|
+
plugins: [sveltekit(withSepal({ adapter: adapter() })), sepal()]
|
|
50
|
+
});`;
|
|
51
|
+
|
|
52
|
+
/** What `index.html` needs, for the message when there is none. */
|
|
53
|
+
const PAGE = `<!doctype html>
|
|
54
|
+
<html lang="en">
|
|
55
|
+
<head>
|
|
56
|
+
<meta charset="utf-8" />
|
|
57
|
+
<title>My app</title>
|
|
58
|
+
</head>
|
|
59
|
+
<body>
|
|
60
|
+
<script type="module" src="/src/main.ts"></script>
|
|
61
|
+
</body>
|
|
62
|
+
</html>`;
|
|
23
63
|
|
|
24
64
|
/**
|
|
25
65
|
* What the app tells the host about itself. Written to `dist/app.json`.
|
|
@@ -28,7 +68,9 @@ import { head } from './head.js';
|
|
|
28
68
|
* (square, 1024×1024 recommended) in the `static/` directory, named relative to
|
|
29
69
|
* it; `static/icon.png` is used when `icon` is left out, and the Sepal icon
|
|
30
70
|
* when there is no `static/icon.png` either. `width` and `height`
|
|
31
|
-
* are the initial window size in logical pixels
|
|
71
|
+
* are the initial window size in logical pixels, and `minWidth` and `minHeight` how
|
|
72
|
+
* small the window can be made: the size below which the app's layout cannot
|
|
73
|
+
* work. The window never opens smaller. `id` (a bundle identifier such as `com.example.counter`) and
|
|
32
74
|
* `version` are used when the app is bundled.
|
|
33
75
|
*
|
|
34
76
|
* `updater` turns on self-update: `url` is where the update feed
|
|
@@ -36,13 +78,18 @@ import { head } from './head.js';
|
|
|
36
78
|
* updates are signed with (`sepal signer generate`). The app then
|
|
37
79
|
* updates itself through `sepal/updater`.
|
|
38
80
|
*
|
|
81
|
+
* `background: true` keeps an app with server code running when its last
|
|
82
|
+
* window closes: the server code goes on, with no window, until the app
|
|
83
|
+
* calls `quit()` from `sepal/app`. Otherwise closing the window quits the app.
|
|
84
|
+
*
|
|
39
85
|
* @typedef {{
|
|
40
86
|
* name?: string,
|
|
41
87
|
* id?: string,
|
|
42
88
|
* version?: string,
|
|
43
89
|
* icon?: string,
|
|
90
|
+
* background?: boolean,
|
|
44
91
|
* updater?: { url: string, pubkey: string },
|
|
45
|
-
* window?: { title?: string, width?: number, height?: number }
|
|
92
|
+
* window?: { title?: string, width?: number, height?: number, minWidth?: number, minHeight?: number }
|
|
46
93
|
* }} AppManifest
|
|
47
94
|
*/
|
|
48
95
|
|
|
@@ -61,16 +108,26 @@ import { head } from './head.js';
|
|
|
61
108
|
* - `dialog`: open, save and message dialogs.
|
|
62
109
|
* - `clipboard`: reading and writing the clipboard.
|
|
63
110
|
* - `open`: opening web and mail links, and allowed files, in their default app.
|
|
64
|
-
* - `window`: changing the window:
|
|
111
|
+
* - `window`: changing the window: minimise, maximise, decorations, close,
|
|
112
|
+
* drag. The title needs no permission.
|
|
113
|
+
* - `os`: the system's details (`sepal/os`'s `info()`). Environment
|
|
114
|
+
* variables only by name: `os: { env: ['EDITOR'] }`, which gives `info()`
|
|
115
|
+
* too. The locale needs none: it is `navigator.language`.
|
|
116
|
+
* - `settings`: the app's settings (`sepal/settings`), a store the page,
|
|
117
|
+
* the server code and the app's Rust share.
|
|
118
|
+
* - `process`: which programs `sepal/process` may run: names
|
|
119
|
+
* (`'git'`) and absolute paths. It limits which programs start, not what
|
|
120
|
+
* they do: `git` runs hooks and aliases, a shell runs anything. `true`
|
|
121
|
+
* allows any program.
|
|
122
|
+
* - `secrets`: the OS's keychain (`sepal/secrets`), for the app's own
|
|
123
|
+
* tokens and passwords.
|
|
124
|
+
* - `notifications`: the system's notifications (`sepal/notifications`).
|
|
65
125
|
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
* open?: boolean,
|
|
72
|
-
* window?: boolean
|
|
73
|
-
* }} Permissions
|
|
126
|
+
* An interface (vite/permissions.d.ts, `sepal/permissions`), so that a
|
|
127
|
+
* plugin's package can add its key:
|
|
128
|
+
* `declare module 'sepal/permissions' { interface Permissions { … } }`.
|
|
129
|
+
*
|
|
130
|
+
* @typedef {import('./permissions.js').Permissions} Permissions
|
|
74
131
|
*/
|
|
75
132
|
|
|
76
133
|
/**
|
|
@@ -81,13 +138,9 @@ import { head } from './head.js';
|
|
|
81
138
|
* (`window`, `document`, element methods; see `sepal/dom`)
|
|
82
139
|
* before anything else, so those libraries find what they reach for.
|
|
83
140
|
*
|
|
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
|
-
*
|
|
87
141
|
* @typedef {{
|
|
88
142
|
* app?: AppManifest,
|
|
89
143
|
* permissions?: Permissions,
|
|
90
|
-
* entry?: string,
|
|
91
144
|
* dom?: boolean
|
|
92
145
|
* }} Config
|
|
93
146
|
*/
|
|
@@ -110,14 +163,25 @@ export function defineConfig(config) {
|
|
|
110
163
|
* @returns {import('vite').PluginOption[]}
|
|
111
164
|
*/
|
|
112
165
|
export function sepal(inline = {}) {
|
|
166
|
+
// A SvelteKit app has SvelteKit's page template. SvelteKit's own plugin
|
|
167
|
+
// compiles it then (with the `customRenderer` option); this one adds the
|
|
168
|
+
// rest. `configResolved` checks the guess against the plugins there are.
|
|
169
|
+
const kit_app = existsSync(resolve('src/app.html'));
|
|
113
170
|
/** The config file with `inline` on top. Filled in by the `config` hook. @type {Config} */
|
|
114
171
|
let options = inline;
|
|
115
172
|
/** @type {string | null} */
|
|
116
173
|
let config_path = null;
|
|
117
174
|
/** @type {import('vite').ConfigEnv} */
|
|
118
175
|
let config_env = { mode: 'production', command: 'build' };
|
|
119
|
-
/**
|
|
120
|
-
|
|
176
|
+
/**
|
|
177
|
+
* The modules `index.html` loads. Sepal's runtime goes first in each (the
|
|
178
|
+
* DOM layer, with `dom`, which loads it): its engine fixes have to be in
|
|
179
|
+
* place before Svelte's runtime runs, which keeps its own copy of
|
|
180
|
+
* `Object.getOwnPropertyDescriptor`, whatever order the app imports in.
|
|
181
|
+
*/
|
|
182
|
+
const page_entries = new Set();
|
|
183
|
+
/** @type {import('vite').Logger | null} */
|
|
184
|
+
let logger = null;
|
|
121
185
|
const user = inline.svelte ?? {};
|
|
122
186
|
let root = '';
|
|
123
187
|
/** The directory copied into the build as it is (`static/`). Empty when turned off. */
|
|
@@ -126,6 +190,152 @@ export function sepal(inline = {}) {
|
|
|
126
190
|
let watching = false;
|
|
127
191
|
const show = dev_window(() => ({ root, out_dir, name: options.app?.name }));
|
|
128
192
|
|
|
193
|
+
/** @type {import('vite').Plugin} */
|
|
194
|
+
const config_plugin = {
|
|
195
|
+
name: 'sepal:config',
|
|
196
|
+
async config(user, env) {
|
|
197
|
+
const project = resolve(user.root ?? '.');
|
|
198
|
+
config_env = env;
|
|
199
|
+
const file = await load_config(project, env);
|
|
200
|
+
config_path = file.path;
|
|
201
|
+
options = { ...file.config, ...inline };
|
|
202
|
+
|
|
203
|
+
if (kit_app) {
|
|
204
|
+
// The adapter writes the app, and says when it has.
|
|
205
|
+
kit.session = {
|
|
206
|
+
root: project,
|
|
207
|
+
options,
|
|
208
|
+
written: (written_to) => {
|
|
209
|
+
out_dir = written_to;
|
|
210
|
+
if (watching) show();
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
return {
|
|
214
|
+
resolve: {
|
|
215
|
+
// Svelte's client build with custom renderer support, and
|
|
216
|
+
// `browser`: SvelteKit's apps always have the DOM layer.
|
|
217
|
+
conditions: ['svelte-custom-renderer', 'browser', 'svelte', 'module', 'import', 'default'],
|
|
218
|
+
alias: [FORMDATA]
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const has_page = existsSync(join(project, 'index.html'));
|
|
224
|
+
if (!has_page && existsSync(join(project, 'src/routes'))) {
|
|
225
|
+
throw new Error(`sepal: this app has pages in src/routes. Pages are SvelteKit's: add SvelteKit's src/app.html, a src/routes/+layout.ts with \`export const ssr = false;\`, and in vite.config.ts:\n\n${KIT_SETUP}`);
|
|
226
|
+
}
|
|
227
|
+
if (!has_page) {
|
|
228
|
+
throw new Error(`sepal: this app has no index.html. It is the page the app starts from, as in any Vite app, next to vite.config.ts:\n\n${PAGE}`);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return {
|
|
232
|
+
// Files in `static/` are copied to the build as they are, so the app
|
|
233
|
+
// can refer to them by name: `<img src="logo.svg">`. Vite calls this
|
|
234
|
+
// directory `public`; a `publicDir` in the user's config still wins.
|
|
235
|
+
...(user.publicDir === undefined ? { publicDir: 'static' } : {}),
|
|
236
|
+
resolve: {
|
|
237
|
+
// Selects Svelte's client build with custom renderer support. With
|
|
238
|
+
// `dom`, `browser` as well: libraries check it at build time to
|
|
239
|
+
// decide whether there is a DOM to work with.
|
|
240
|
+
conditions: ['svelte-custom-renderer', ...(options.dom ? ['browser'] : []), 'svelte', 'module', 'import', 'default'],
|
|
241
|
+
// The name SvelteKit 2 projects use for `src/lib`.
|
|
242
|
+
alias: [{ find: '$lib', replacement: join(project, 'src/lib') }, FORMDATA]
|
|
243
|
+
},
|
|
244
|
+
build: {
|
|
245
|
+
target: 'es2022',
|
|
246
|
+
// One stylesheet, linked from index.html. A chunk's own
|
|
247
|
+
// stylesheet would be added with `document`, which an app
|
|
248
|
+
// without the DOM layer does not have.
|
|
249
|
+
cssCodeSplit: false,
|
|
250
|
+
// So would preloading: Vite's modulepreload polyfill and the
|
|
251
|
+
// <link>s it writes ahead of a dynamic import. The host loads
|
|
252
|
+
// a module from the app folder when it is imported.
|
|
253
|
+
modulePreload: false
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
},
|
|
257
|
+
resolveId: {
|
|
258
|
+
// Before Vite's own resolver, which would answer first.
|
|
259
|
+
order: 'pre',
|
|
260
|
+
async handler(source, importer) {
|
|
261
|
+
// The modules index.html loads, for `page_entries`. SvelteKit's
|
|
262
|
+
// entry gets the same from `patch`.
|
|
263
|
+
if (kit_app || !importer?.endsWith('.html')) return;
|
|
264
|
+
const resolved = await this.resolve(source, importer, { skipSelf: true });
|
|
265
|
+
if (resolved) page_entries.add(resolved.id);
|
|
266
|
+
return resolved;
|
|
267
|
+
}
|
|
268
|
+
},
|
|
269
|
+
transform(code, id) {
|
|
270
|
+
if (page_entries.has(id)) return { code: `import '${options.dom ? 'sepal/dom' : 'sepal'}';\n${code}`, map: null };
|
|
271
|
+
// Stylesheets, component styles included: nothing in them may load from the network.
|
|
272
|
+
if (/\.css($|\?)/.test(id)) {
|
|
273
|
+
const errors = check_css(code, normalizePath(relative(root, id.split('?')[0])));
|
|
274
|
+
if (errors.length > 0) this.error(`sepal: ${errors.join('\nsepal: ')}`);
|
|
275
|
+
}
|
|
276
|
+
},
|
|
277
|
+
transformIndexHtml: {
|
|
278
|
+
order: 'pre',
|
|
279
|
+
handler(html) {
|
|
280
|
+
const { errors, warnings } = check_page(html);
|
|
281
|
+
if (errors.length > 0) throw new Error(`sepal: ${errors.join('\nsepal: ')}`);
|
|
282
|
+
for (const warning of warnings) logger?.warn(`sepal: ${warning}`);
|
|
283
|
+
}
|
|
284
|
+
},
|
|
285
|
+
configResolved(config) {
|
|
286
|
+
logger = config.logger;
|
|
287
|
+
root = config.root;
|
|
288
|
+
static_dir = config.publicDir;
|
|
289
|
+
out_dir = resolve(root, config.build.outDir);
|
|
290
|
+
watching = config.build.watch != null;
|
|
291
|
+
const kit_plugin = config.plugins.find((plugin) => plugin.name === 'vite-plugin-sveltekit-setup');
|
|
292
|
+
const has_kit = kit_plugin !== undefined;
|
|
293
|
+
// SvelteKit's options, as validated. The adapter checks them too, but
|
|
294
|
+
// only once everything is compiled, and a component with `bind:` on an
|
|
295
|
+
// element would stop the build first, with Svelte's message.
|
|
296
|
+
const kit_options = kit_plugin?.api?.options;
|
|
297
|
+
if (kit_app && kit_options) {
|
|
298
|
+
const missing = kit_setup_problems(kit_options);
|
|
299
|
+
if (missing.length > 0) throw new Error(`sepal: ${missing.join('\nsepal: ')}`);
|
|
300
|
+
}
|
|
301
|
+
if (kit_app && !has_kit) {
|
|
302
|
+
throw new Error(`sepal: this project has src/app.html, so it is a SvelteKit app, but vite.config.ts has no \`sveltekit()\`. Set it up as:\n\n${KIT_SETUP}`);
|
|
303
|
+
}
|
|
304
|
+
if (!kit_app && has_kit) {
|
|
305
|
+
throw new Error(`sepal: vite.config.ts has \`sveltekit()\`, but there is no src/app.html where sepal() looks, in ${process.cwd()}. Run Vite from the project's directory.`);
|
|
306
|
+
}
|
|
307
|
+
},
|
|
308
|
+
async buildStart() {
|
|
309
|
+
// In watch mode, pick up edits to the config file. What it says about
|
|
310
|
+
// the app and its permissions takes effect on the next build; a
|
|
311
|
+
// change to `dom` needs Vite restarted.
|
|
312
|
+
if (config_path === null) return;
|
|
313
|
+
this.addWatchFile(config_path);
|
|
314
|
+
const file = await load_config(root, config_env);
|
|
315
|
+
options = { ...file.config, ...inline };
|
|
316
|
+
if (kit.session !== null) kit.session.options = options;
|
|
317
|
+
},
|
|
318
|
+
generateBundle() {
|
|
319
|
+
// A SvelteKit app's manifest is the adapter's to write.
|
|
320
|
+
if (kit_app) return;
|
|
321
|
+
let written;
|
|
322
|
+
try {
|
|
323
|
+
written = manifest(options, root, static_dir);
|
|
324
|
+
} catch (error) {
|
|
325
|
+
this.error(/** @type {Error} */ (error).message);
|
|
326
|
+
}
|
|
327
|
+
if (!written) return;
|
|
328
|
+
for (const warning of written.warnings) this.warn(`sepal: ${warning}`);
|
|
329
|
+
this.emitFile({ type: 'asset', fileName: 'app.json', source: written.json });
|
|
330
|
+
if (written.icon) this.emitFile({ type: 'asset', fileName: 'icon.png', source: readFileSync(written.icon) });
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
if (kit_app) {
|
|
335
|
+
// SvelteKit writes its own page, and warns about plugins that ask for it.
|
|
336
|
+
delete config_plugin.transformIndexHtml;
|
|
337
|
+
return [config_plugin, sveltekit_edits()];
|
|
338
|
+
}
|
|
129
339
|
return [
|
|
130
340
|
svelte({
|
|
131
341
|
...user,
|
|
@@ -143,97 +353,156 @@ export function sepal(inline = {}) {
|
|
|
143
353
|
}
|
|
144
354
|
}
|
|
145
355
|
}),
|
|
146
|
-
|
|
147
|
-
name: 'sepal:config',
|
|
148
|
-
async config(user, env) {
|
|
149
|
-
const project = resolve(user.root ?? '.');
|
|
150
|
-
config_env = env;
|
|
151
|
-
const file = await load_config(project, env);
|
|
152
|
-
config_path = file.path;
|
|
153
|
-
options = { ...file.config, ...inline };
|
|
154
|
-
|
|
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';
|
|
163
|
-
|
|
164
|
-
// Vite's module ids use `/` on Windows as well.
|
|
165
|
-
if (options.dom) dom_entry = normalizePath(resolve(project, entry)).replace(/\.[cm]?[jt]s$/, '');
|
|
166
|
-
|
|
167
|
-
return {
|
|
168
|
-
// Files in `static/` are copied to the build as they are, so the app
|
|
169
|
-
// can refer to them by name: `<img src="logo.svg">`. Vite calls this
|
|
170
|
-
// directory `public`; a `publicDir` in the user's config still wins.
|
|
171
|
-
...(user.publicDir === undefined ? { publicDir: 'static' } : {}),
|
|
172
|
-
resolve: {
|
|
173
|
-
// Selects Svelte's client build with custom renderer support. With
|
|
174
|
-
// `dom`, `browser` as well: libraries check it at build time to
|
|
175
|
-
// decide whether there is a DOM to work with.
|
|
176
|
-
conditions: ['svelte-custom-renderer', ...(options.dom ? ['browser'] : []), 'svelte', 'module', 'import', 'default'],
|
|
177
|
-
// The name SvelteKit 2 projects use for `src/lib`.
|
|
178
|
-
alias: { $lib: join(project, 'src/lib') }
|
|
179
|
-
},
|
|
180
|
-
build: {
|
|
181
|
-
target: 'es2022',
|
|
182
|
-
cssCodeSplit: false,
|
|
183
|
-
lib: {
|
|
184
|
-
entry,
|
|
185
|
-
name: 'app',
|
|
186
|
-
formats: ['iife'],
|
|
187
|
-
fileName: () => 'app.js',
|
|
188
|
-
cssFileName: 'app'
|
|
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 } }
|
|
193
|
-
}
|
|
194
|
-
};
|
|
195
|
-
},
|
|
196
|
-
transform(code, id) {
|
|
197
|
-
// The DOM has to exist before any library module runs, so it is the
|
|
198
|
-
// entry module's first import.
|
|
199
|
-
if (dom_entry !== '' && id.replace(/\.[cm]?[jt]s$/, '') === dom_entry) {
|
|
200
|
-
return { code: `import 'sepal/dom';\n${code}`, map: null };
|
|
201
|
-
}
|
|
202
|
-
},
|
|
203
|
-
configResolved(config) {
|
|
204
|
-
root = config.root;
|
|
205
|
-
static_dir = config.publicDir;
|
|
206
|
-
out_dir = resolve(root, config.build.outDir);
|
|
207
|
-
watching = config.build.watch != null;
|
|
208
|
-
},
|
|
209
|
-
async buildStart() {
|
|
210
|
-
// In watch mode, pick up edits to the config file. What it says about
|
|
211
|
-
// the app and its permissions takes effect on the next build; a
|
|
212
|
-
// change to `entry` needs Vite restarted.
|
|
213
|
-
if (config_path === null) return;
|
|
214
|
-
this.addWatchFile(config_path);
|
|
215
|
-
const file = await load_config(root, config_env);
|
|
216
|
-
options = { ...file.config, ...inline, entry: options.entry };
|
|
217
|
-
},
|
|
218
|
-
generateBundle() {
|
|
219
|
-
let written;
|
|
220
|
-
try {
|
|
221
|
-
written = manifest(options, root, static_dir);
|
|
222
|
-
} catch (error) {
|
|
223
|
-
this.error(/** @type {Error} */ (error).message);
|
|
224
|
-
}
|
|
225
|
-
if (!written) return;
|
|
226
|
-
this.emitFile({ type: 'asset', fileName: 'app.json', source: written.json });
|
|
227
|
-
if (written.icon) this.emitFile({ type: 'asset', fileName: 'icon.png', source: readFileSync(written.icon) });
|
|
228
|
-
}
|
|
229
|
-
}),
|
|
356
|
+
config_plugin,
|
|
230
357
|
{
|
|
231
358
|
name: 'sepal:dev',
|
|
232
359
|
// `writeBundle`, not `closeBundle`: it only runs when the build
|
|
233
360
|
// succeeded, so a syntax error leaves the last good build on screen.
|
|
234
361
|
writeBundle() {
|
|
362
|
+
const compiled = compile_bytecode(out_dir, (message) => logger?.warn(message));
|
|
363
|
+
if (compiled) logger?.info(compiled);
|
|
235
364
|
if (watching) show();
|
|
236
365
|
}
|
|
237
366
|
}
|
|
238
367
|
];
|
|
239
368
|
}
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* Sepal's rewrites of what Svelte refuses for a custom renderer (`bind:` on
|
|
372
|
+
* elements, `<svelte:head>`), as one preprocessor. `withSepal()` adds it;
|
|
373
|
+
* this is for an app that sets `sveltekit()`'s options by hand, after its
|
|
374
|
+
* own preprocessors: `preprocess: [vitePreprocess(), sepalPreprocess()]`.
|
|
375
|
+
* @returns {import('svelte/compiler').PreprocessorGroup}
|
|
376
|
+
*/
|
|
377
|
+
export function sepalPreprocess() {
|
|
378
|
+
const rewrites = [element_bindings(), head()];
|
|
379
|
+
return {
|
|
380
|
+
name: 'sepal',
|
|
381
|
+
async markup({ content, filename }) {
|
|
382
|
+
if (!content.includes('bind:') && !content.includes('<svelte:head')) return;
|
|
383
|
+
const rewritten = await preprocess(content, rewrites, { filename });
|
|
384
|
+
return { code: rewritten.code, map: /** @type {any} */ (rewritten.map) };
|
|
385
|
+
}
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/** `{}` or `Object.create(null)`: not an array, a function or a class instance. @param {unknown} value @returns {value is Record<string, any>} */
|
|
390
|
+
const plain = (value) => value !== null && typeof value === 'object' && [Object.prototype, null].includes(Object.getPrototypeOf(value));
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* `ours` merged into a copy of `theirs`, level by level where both are plain
|
|
394
|
+
* objects. Anything else is taken as it is; where both have a value, ours.
|
|
395
|
+
* @param {Record<string, any>} theirs @param {Record<string, any>} ours
|
|
396
|
+
* @returns {Record<string, any>}
|
|
397
|
+
*/
|
|
398
|
+
function merge(theirs, ours) {
|
|
399
|
+
const merged = { ...theirs };
|
|
400
|
+
for (const [key, value] of Object.entries(ours)) merged[key] = plain(value) && plain(theirs[key]) ? merge(theirs[key], value) : value;
|
|
401
|
+
return merged;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* SvelteKit's options for a Sepal app: `sveltekit(withSepal({ adapter: adapter() }))`,
|
|
406
|
+
* with `adapter` from `sepal/adapter`. Adds what every Sepal app needs to
|
|
407
|
+
* the app's own options: components compiled for the Sepal renderer, no
|
|
408
|
+
* version polling (apps update through `sepal/updater`), and Sepal's
|
|
409
|
+
* rewrites after the app's own preprocessors (`sepalPreprocess()`), once.
|
|
410
|
+
* Throws on options that cannot work in an app.
|
|
411
|
+
* @template {import('@sveltejs/kit/vite').Config} T
|
|
412
|
+
* @param {T} options
|
|
413
|
+
* @returns {T}
|
|
414
|
+
*/
|
|
415
|
+
export function withSepal(options) {
|
|
416
|
+
const usage = "`sveltekit(withSepal({ adapter: adapter() }))`, with `import adapter from 'sepal/adapter'`";
|
|
417
|
+
const given = /** @type {any} */ (options ?? {});
|
|
418
|
+
if (given.adapter === undefined) throw new Error(`sepal: withSepal() has no adapter. A Sepal app's is sepal/adapter, which writes the app for the host: ${usage}.`);
|
|
419
|
+
if (given.adapter?.name !== 'sepal') throw new Error(`sepal: withSepal() was given the adapter ${given.adapter?.name ?? '(unnamed)'}. A Sepal app's is sepal/adapter, which writes the app for the host: ${usage}.`);
|
|
420
|
+
const renderer = given.compilerOptions?.experimental?.customRenderer;
|
|
421
|
+
if (renderer !== undefined && renderer !== 'sepal/renderer') {
|
|
422
|
+
throw new Error(`sepal: compilerOptions.experimental.customRenderer is ${JSON.stringify(renderer)}. A Sepal app's components are compiled for 'sepal/renderer'; leave it out, withSepal() sets it.`);
|
|
423
|
+
}
|
|
424
|
+
const poll = given.version?.pollInterval;
|
|
425
|
+
if (typeof poll === 'number' && poll > 0) {
|
|
426
|
+
throw new Error(`sepal: version.pollInterval is ${poll}. SvelteKit would ask the server for a new version that often, waking an idle app, and an app has no server to answer. Apps update through sepal/updater; leave it out, withSepal() sets it to 0.`);
|
|
427
|
+
}
|
|
428
|
+
const merged = merge(given, { compilerOptions: { experimental: { customRenderer: 'sepal/renderer' } }, version: { pollInterval: 0 } });
|
|
429
|
+
const groups = [given.preprocess ?? []].flat();
|
|
430
|
+
merged.preprocess = groups.some((group) => group?.name === 'sepal') ? groups : [...groups, sepalPreprocess()];
|
|
431
|
+
return /** @type {T} */ (merged);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
* What SvelteKit's options lack for a Sepal app, as messages; empty when
|
|
436
|
+
* nothing does. `sepal()` checks before the build, the adapter after it.
|
|
437
|
+
* @param {any} config SvelteKit's options, as it validated them
|
|
438
|
+
* @returns {string[]}
|
|
439
|
+
*/
|
|
440
|
+
export function kit_setup_problems(config) {
|
|
441
|
+
const found = [];
|
|
442
|
+
const fix = 'Pass SvelteKit\'s options through withSepal() from sepal/vite: `sveltekit(withSepal({ adapter: adapter() }))` in vite.config.ts';
|
|
443
|
+
if (config.compilerOptions?.experimental?.customRenderer !== 'sepal/renderer') {
|
|
444
|
+
found.push(`components are not compiled for the Sepal renderer. ${fix} (by hand: \`compilerOptions: { experimental: { customRenderer: 'sepal/renderer' } }\`).`);
|
|
445
|
+
}
|
|
446
|
+
if (config.version?.pollInterval !== 0) {
|
|
447
|
+
found.push(`SvelteKit would ask for a new version every hour, waking an idle app; apps update through \`sepal/updater\`. ${fix} (by hand: \`version: { pollInterval: 0 }\`).`);
|
|
448
|
+
}
|
|
449
|
+
if (![config.preprocess ?? []].flat().some((group) => group?.name === 'sepal')) {
|
|
450
|
+
found.push(`Sepal's rewrites of \`bind:\` and \`<svelte:head>\` are not among the preprocessors. ${fix} (by hand: \`sepalPreprocess()\` from sepal/vite, last in \`preprocess\`).`);
|
|
451
|
+
}
|
|
452
|
+
return found;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* The edits to SvelteKit's client. Each names the file, what to find in it,
|
|
457
|
+
* and what goes there instead. SvelteKit's peer range in package.json stops
|
|
458
|
+
* at the versions these were checked against.
|
|
459
|
+
* @type {{ file: string, find: string, replace: string, prepend: string, why: string }[]}
|
|
460
|
+
*/
|
|
461
|
+
const patch = [
|
|
462
|
+
{
|
|
463
|
+
// The entry of SvelteKit's client: `entry.js` for its default split
|
|
464
|
+
// build, `bundle.js` for one bundle. The DOM layer and the runtime
|
|
465
|
+
// (`__sb`, the host's way in) have to exist before any of SvelteKit runs.
|
|
466
|
+
file: '/kit/src/runtime/client/entry.js',
|
|
467
|
+
find: 'export function init(payload) {',
|
|
468
|
+
replace: 'export function init(payload) {',
|
|
469
|
+
prepend: "import 'sepal/dom';\nimport 'sepal';\n",
|
|
470
|
+
why: 'how its client starts'
|
|
471
|
+
},
|
|
472
|
+
{
|
|
473
|
+
file: '/kit/src/runtime/client/bundle.js',
|
|
474
|
+
find: 'export function start(element, options) {',
|
|
475
|
+
replace: 'export function start(element, options) {',
|
|
476
|
+
prepend: "import 'sepal/dom';\nimport 'sepal';\n",
|
|
477
|
+
why: 'how its single bundle starts'
|
|
478
|
+
},
|
|
479
|
+
{
|
|
480
|
+
// SvelteKit mounts its root component itself, and does not say with
|
|
481
|
+
// which renderer.
|
|
482
|
+
file: '/kit/src/runtime/client/client.js',
|
|
483
|
+
find: 'render(Root, {',
|
|
484
|
+
replace: 'render(Root, {\n\t\trenderer: __sepal_renderer,',
|
|
485
|
+
prepend: "import __sepal_renderer from 'sepal/renderer';\n",
|
|
486
|
+
why: 'how it mounts its root component'
|
|
487
|
+
}
|
|
488
|
+
];
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* The edits to SvelteKit's client. The rewrites of components are
|
|
492
|
+
* SvelteKit's options' (`withSepal()`), so that svelte-check sees them too.
|
|
493
|
+
* @returns {import('vite').Plugin}
|
|
494
|
+
*/
|
|
495
|
+
function sveltekit_edits() {
|
|
496
|
+
return {
|
|
497
|
+
name: 'sepal:sveltekit',
|
|
498
|
+
enforce: 'pre',
|
|
499
|
+
transform(code, id) {
|
|
500
|
+
const edit = patch.find(({ file }) => id.endsWith(file));
|
|
501
|
+
if (edit === undefined) return;
|
|
502
|
+
if (!code.includes(edit.find)) {
|
|
503
|
+
this.error(`sepal: this version of SvelteKit changed ${edit.why}, which Sepal has to adjust. Use the SvelteKit version Sepal was tested with (see sepal's peer range for @sveltejs/kit), or update Sepal.`);
|
|
504
|
+
}
|
|
505
|
+
return { code: edit.prepend + code.replace(edit.find, edit.replace), map: null };
|
|
506
|
+
}
|
|
507
|
+
};
|
|
508
|
+
}
|