sepal 0.1.0-alpha.0 → 0.1.0-alpha.2

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 CHANGED
@@ -1,12 +1,32 @@
1
1
  # sepal
2
2
 
3
+ **Native Svelte apps, drawn by Blitz.**
4
+
3
5
  [Sepal](https://github.com/etherCorps/sepal): Svelte apps in a native window, without
4
6
  a webview and without Node at run time. Svelte components are compiled with
5
7
  Svelte's custom renderer API and drive [Blitz](https://github.com/DioxusLabs/blitz),
6
8
  a Rust HTML/CSS engine; the JavaScript runs in an embedded QuickJS.
7
9
 
8
- This one package is the runtime, the Vite preset and the `sepal` command.
9
- Start a project with the starter rather than by installing it directly:
10
+ An alpha, on macOS (Linux builds and passes its tests headless; its window has
11
+ not been tried yet). Svelte 5 and Tailwind v4 as on the web, pages as files
12
+ (Sepal's router or SvelteKit's), shadcn-svelte components through an opt-in
13
+ DOM layer, desktop APIs behind permissions, macOS `.app` bundles and
14
+ self-update, and no frame loop: an idle app does no work.
15
+
16
+ ![The Tracker example in a Sepal window on macOS](https://raw.githubusercontent.com/etherCorps/sepal/main/docs/tracker.png)
17
+
18
+ The Tracker example with 1,000 issues, on an Apple M4 Pro (medians of three
19
+ runs; [how they were measured](https://github.com/etherCorps/sepal/blob/main/compare/results/compare.md)):
20
+
21
+ | | |
22
+ |---|---|
23
+ | Idle CPU, 1,000 issues | 4 ms of CPU time in 20 s, 0.02% of a core |
24
+ | Idle memory, 1,000 issues | 107 MB RAM, 43 MB GPU memory |
25
+ | Memory after wheel-scrolling the whole list, 1,000 issues | 305 MB RAM, 220 MB GPU memory |
26
+
27
+ This one package is the runtime, the Vite preset and the `sepal` command; the
28
+ prebuilt host comes with it. Start a project with the starter rather than by
29
+ installing it directly:
10
30
 
11
31
  ```sh
12
32
  pnpm create sepal my-app
package/cli.js CHANGED
@@ -3,7 +3,11 @@
3
3
  // Building is Vite's job (`vite build`, and `vite build --watch` for dev).
4
4
 
5
5
  import { spawnSync } from 'node:child_process';
6
- import { find_host, platform } from '@ethercorps/calyx';
6
+ import { readFileSync } from 'node:fs';
7
+ import { basename, join, resolve } from 'node:path';
8
+ import { find_host, platform_package } from '@ethercorps/calyx';
9
+
10
+ const own = JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf8'));
7
11
 
8
12
  const USAGE = `usage:
9
13
  sepal run [dist] open the built app in a window
@@ -23,27 +27,72 @@ function fail(message) {
23
27
  process.exit(1);
24
28
  }
25
29
 
26
- /** Run the host with these arguments and exit with its status. @param {string[]} args @returns {never} */
27
- function host(args) {
28
- const bin = find_host();
29
- if (bin === null) {
30
+ /** This machine's calyx, once its version is known to fit. @returns {string} */
31
+ function calyx() {
32
+ /** @type {ReturnType<typeof find_host>} */
33
+ let found;
34
+ try {
35
+ found = find_host();
36
+ } catch (error) {
37
+ fail(`sepal: ${/** @type {Error} */ (error).message}`);
38
+ }
39
+ // The pins are exact (sepal -> calyx -> the platform package), so this only
40
+ // happens to a node_modules put together by hand or half updated. A
41
+ // calyx from SEPAL_HOST is the user's choice and is not checked.
42
+ if (found.version !== null && found.version !== own.version) {
30
43
  fail(
31
- `sepal: no calyx (host binary) for ${platform()}.\n` +
32
- 'Build one from crates/host and point SEPAL_HOST at it.'
44
+ `sepal: this is sepal ${own.version}, but its calyx binary (${platform_package()}) is ${found.version}.\n` +
45
+ `They have to be the same version. Reinstall sepal@${own.version} (or remove node_modules and install\n` +
46
+ 'again). To run a different calyx on purpose, point SEPAL_HOST at it.'
33
47
  );
34
48
  }
49
+ return found.path;
50
+ }
51
+
52
+ /** Run a binary with these arguments and exit with its status. @param {string} bin @param {string[]} args @returns {never} */
53
+ function start(bin, args) {
35
54
  const { status, error } = spawnSync(bin, args, { stdio: 'inherit' });
36
55
  if (error) fail(`sepal: cannot start ${bin}: ${error.message}`);
37
56
  process.exit(status ?? 1);
38
57
  }
39
58
 
59
+ /** Run the host with these arguments and exit with its status. @param {string[]} args @returns {never} */
60
+ function host(args) {
61
+ start(calyx(), args);
62
+ }
63
+
64
+ /**
65
+ * macOS takes the name in the Dock, the app switcher and Activity Monitor
66
+ * from the bundle a process runs from; a bare calyx is called "calyx". So on
67
+ * macOS an app with a name runs out of a throwaway bundle in
68
+ * node_modules/.sepal, as it does in dev, and still loads from `dir`.
69
+ * @param {string} bin @param {string} dir
70
+ */
71
+ function bundled(bin, dir) {
72
+ if (process.platform !== 'darwin') return bin;
73
+ let name;
74
+ try {
75
+ name = JSON.parse(readFileSync(join(dir, 'app.json'), 'utf8')).name;
76
+ } catch {
77
+ return bin;
78
+ }
79
+ if (!name) return bin;
80
+ const made = spawnSync(bin, ['--bundle', dir, resolve('node_modules/.sepal')], { encoding: 'utf8' });
81
+ if (made.status !== 0) {
82
+ console.warn(`sepal: running unbundled: ${made.stderr.trim()}`);
83
+ return bin;
84
+ }
85
+ const bundle = made.stdout.trim();
86
+ return join(bundle, 'Contents/MacOS', basename(bundle, '.app'));
87
+ }
88
+
40
89
  const [command, ...args] = process.argv.slice(2);
41
90
  // `bundle --sign x` with no directory still means `dist`.
42
91
  const dist = args[0] !== undefined && !args[0].startsWith('--') ? args : ['dist', ...args];
43
92
 
44
93
  switch (command) {
45
94
  case 'run':
46
- host(dist);
95
+ start(bundled(calyx(), dist[0]), dist);
47
96
  case 'bundle':
48
97
  host(['--bundle', ...dist]);
49
98
  case 'signer':
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sepal",
3
- "version": "0.1.0-alpha.0",
3
+ "version": "0.1.0-alpha.2",
4
4
  "description": "Native Svelte apps, drawn by Blitz: a Svelte custom renderer for the Blitz HTML/CSS engine, its Vite preset and the sepal command",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -102,7 +102,7 @@
102
102
  "cli.js"
103
103
  ],
104
104
  "dependencies": {
105
- "@ethercorps/calyx": "0.1.0-alpha.0"
105
+ "@ethercorps/calyx": "0.1.0-alpha.2"
106
106
  },
107
107
  "peerDependencies": {
108
108
  "svelte": "^5.59.0",
package/src/fetch.js CHANGED
@@ -1,57 +1,143 @@
1
- // `fetch`, installed as a global when the engine has none.
1
+ // `fetch`, and the Fetch API classes around it (`Request`, `Headers`,
2
+ // `Response`), installed as globals when the engine has none.
2
3
  //
3
4
  // The host makes the request, and only to URLs the app's `permissions.http`
4
5
  // allows. This is the everyday part of the Fetch API: method, headers, a
5
6
  // string or byte body, and `text()`, `json()`, `arrayBuffer()`, `bytes()` on
6
- // the response. There is no streaming, no `AbortSignal`, and no `Request`,
7
- // `Headers` or `FormData` classes.
7
+ // the response. There is no streaming, no `AbortSignal` and no `FormData`.
8
+ // The classes are there because libraries test for them and build them:
9
+ // SvelteKit's client asks `input instanceof Request` on every fetch.
8
10
 
9
11
  import { host_call } from './host-call.js';
10
12
  import { decode, encode, to_bytes } from './base64.js';
11
13
 
12
- /** Response headers: names are case-insensitive, as in the Fetch API. */
13
- class ResponseHeaders {
14
- /** @param {[string, string][]} pairs */
15
- constructor(pairs) {
16
- /** @type {Map<string, string>} */
17
- this.values = new Map();
18
- for (const [name, value] of pairs) {
19
- const key = name.toLowerCase();
20
- const earlier = this.values.get(key);
21
- this.values.set(key, earlier === undefined ? value : `${earlier}, ${value}`);
22
- }
14
+ /** Headers: names are case-insensitive and kept in lower case, as in the Fetch API. */
15
+ export class Headers {
16
+ /** @type {Map<string, string>} */
17
+ #map = new Map();
18
+
19
+ /** @param {unknown} [init] a plain object, a list of pairs, or another `Headers` */
20
+ constructor(init) {
21
+ for (const [name, value] of header_pairs(init)) this.append(name, value);
22
+ }
23
+ /** @param {string} name @param {string} value */
24
+ append(name, value) {
25
+ const key = name.toLowerCase();
26
+ const earlier = this.#map.get(key);
27
+ this.#map.set(key, earlier === undefined ? String(value) : `${earlier}, ${value}`);
28
+ }
29
+ /** @param {string} name @param {string} value */
30
+ set(name, value) {
31
+ this.#map.set(name.toLowerCase(), String(value));
23
32
  }
24
33
  /** @param {string} name */
25
34
  get(name) {
26
- return this.values.get(name.toLowerCase()) ?? null;
35
+ return this.#map.get(name.toLowerCase()) ?? null;
27
36
  }
28
37
  /** @param {string} name */
29
38
  has(name) {
30
- return this.values.has(name.toLowerCase());
39
+ return this.#map.has(name.toLowerCase());
40
+ }
41
+ /** @param {string} name */
42
+ delete(name) {
43
+ this.#map.delete(name.toLowerCase());
31
44
  }
32
- /** @param {(value: string, name: string) => void} callback */
45
+ /** @param {(value: string, name: string, headers: Headers) => void} callback */
33
46
  forEach(callback) {
34
- this.values.forEach(callback);
47
+ this.#map.forEach((value, name) => callback(value, name, this));
35
48
  }
36
49
  entries() {
37
- return this.values.entries();
50
+ return this.#map.entries();
51
+ }
52
+ keys() {
53
+ return this.#map.keys();
54
+ }
55
+ values() {
56
+ return this.#map.values();
38
57
  }
39
58
  [Symbol.iterator]() {
40
- return this.values.entries();
59
+ return this.#map.entries();
60
+ }
61
+ }
62
+
63
+ const METHODS = ['DELETE', 'GET', 'HEAD', 'OPTIONS', 'POST', 'PUT', 'PATCH'];
64
+
65
+ /** A request to make: what `fetch` takes, as a string, a URL or one of these. */
66
+ export class Request {
67
+ /**
68
+ * @param {string | Request | { url?: string, href?: string }} input
69
+ * @param {{ method?: string, headers?: unknown, body?: string | ArrayBuffer | ArrayBufferView | null }} [init]
70
+ */
71
+ constructor(input, init = {}) {
72
+ /** @type {Request | null} */
73
+ const from = input instanceof Request ? input : null;
74
+ const target = /** @type {any} */ (input);
75
+ /** @type {string} */
76
+ this.url = from ? from.url : typeof input === 'string' ? input : String(target?.url ?? target?.href ?? input);
77
+ /** @type {string} */
78
+ const method = init.method ?? from?.method ?? 'GET';
79
+ // As the Fetch API does: the standard methods in upper case, any other as written.
80
+ /** @type {string} */
81
+ this.method = METHODS.includes(method.toUpperCase()) ? method.toUpperCase() : method;
82
+ /** @type {Headers} */
83
+ this.headers = new Headers(init.headers ?? from?.headers);
84
+ /** @type {string | ArrayBuffer | ArrayBufferView | null} */
85
+ this.body = init.body ?? from?.body ?? null;
41
86
  }
42
87
  }
43
88
 
44
- class FetchResponse {
89
+ /** A response with its body in hand: what `new Response(body, init)` makes. `fetch` answers with a subclass. */
90
+ export class Response {
91
+ /**
92
+ * @param {string | ArrayBuffer | ArrayBufferView | null} [body]
93
+ * @param {{ status?: number, statusText?: string, headers?: unknown }} [init]
94
+ */
95
+ constructor(body = null, init = {}) {
96
+ this.status = init.status ?? 200;
97
+ this.statusText = init.statusText ?? '';
98
+ this.ok = this.status >= 200 && this.status < 300;
99
+ this.headers = new Headers(init.headers);
100
+ this.url = '';
101
+ this.redirected = false;
102
+ this.bodyUsed = false;
103
+ /** @type {string | Uint8Array | null} */
104
+ this.held = body == null || typeof body === 'string' ? body : to_bytes(body);
105
+ }
106
+
107
+ /** @returns {string | Uint8Array} */
108
+ take() {
109
+ if (this.bodyUsed) throw new TypeError('the response body was already read');
110
+ this.bodyUsed = true;
111
+ return this.held ?? '';
112
+ }
113
+
114
+ /** @returns {Promise<string>} */
115
+ async text() {
116
+ const body = this.take();
117
+ if (typeof body !== 'string') throw new TypeError('text() of a byte body is not supported; use bytes()');
118
+ return body;
119
+ }
120
+ async json() {
121
+ return JSON.parse(await this.text());
122
+ }
123
+ /** @returns {Promise<Uint8Array>} */
124
+ async bytes() {
125
+ const body = this.take();
126
+ if (typeof body === 'string') throw new TypeError('bytes() of a string body is not supported; use text()');
127
+ return body;
128
+ }
129
+ async arrayBuffer() {
130
+ return /** @type {ArrayBuffer} */ ((await this.bytes()).buffer);
131
+ }
132
+ }
133
+
134
+ /** What `fetch` answers with: the body stays with the host until it is asked for. */
135
+ class FetchResponse extends Response {
45
136
  /** @param {{ id: number, status: number, status_text: string, url: string, redirected: boolean, headers: [string, string][] }} head */
46
137
  constructor(head) {
47
- this.status = head.status;
48
- this.statusText = head.status_text;
49
- this.ok = head.status >= 200 && head.status < 300;
138
+ super(null, { status: head.status, statusText: head.status_text, headers: head.headers });
50
139
  this.url = head.url;
51
140
  this.redirected = head.redirected;
52
- this.headers = new ResponseHeaders(head.headers);
53
- this.bodyUsed = false;
54
- /** The body stays with the host until it is asked for. */
55
141
  this.body_id = head.id;
56
142
  }
57
143
 
@@ -65,15 +151,9 @@ class FetchResponse {
65
151
  text() {
66
152
  return this.read('text');
67
153
  }
68
- async json() {
69
- return JSON.parse(await this.read('text'));
70
- }
71
154
  async bytes() {
72
155
  return decode(await this.read('base64'));
73
156
  }
74
- async arrayBuffer() {
75
- return (await this.bytes()).buffer;
76
- }
77
157
  }
78
158
 
79
159
  /**
@@ -95,17 +175,15 @@ function header_pairs(headers) {
95
175
  }
96
176
 
97
177
  /**
98
- * @param {string | { url: string } | { href: string }} input
178
+ * @param {string | Request | { url?: string, href?: string }} input
99
179
  * @param {{ method?: string, headers?: unknown, body?: string | ArrayBuffer | ArrayBufferView | null }} [init]
100
- * @returns {Promise<FetchResponse>}
180
+ * @returns {Promise<Response>}
101
181
  */
102
182
  export async function fetch(input, init = {}) {
103
- const target = /** @type {any} */ (input);
104
- const url = typeof input === 'string' ? input : String(target.url ?? target.href ?? input);
183
+ const { url, method, headers, body } = new Request(input, init);
105
184
  /** @type {Record<string, unknown>} */
106
- const request = { url, method: init.method ?? 'GET', headers: header_pairs(init.headers) };
185
+ const request = { url, method, headers: [...headers] };
107
186
 
108
- const body = init.body;
109
187
  if (typeof body === 'string') request.body = body;
110
188
  else if (body instanceof ArrayBuffer || ArrayBuffer.isView(body)) request.body_base64 = encode(to_bytes(body));
111
189
  else if (body != null) throw new TypeError('fetch: the body must be a string, an ArrayBuffer or a typed array');
package/src/index.js CHANGED
@@ -3,7 +3,7 @@ import { run_timers, next_timer, track_rejection, report_rejections, rejection_c
3
3
  import { mount as svelte_mount, unmount } from 'svelte';
4
4
  import renderer, { root, dispatch, take_batch, stats, keep_alive, host_hooks } from './renderer.js';
5
5
  import { settle_call } from './host-call.js';
6
- import { fetch } from './fetch.js';
6
+ import { fetch, Headers, Request, Response } from './fetch.js';
7
7
 
8
8
  /**
9
9
  * Mount a component into the window.
@@ -22,8 +22,11 @@ export function mount(component, options = {}) {
22
22
 
23
23
  export { unmount, renderer, root };
24
24
 
25
- // The engine has no `fetch`. This one asks the host, which checks the app's permissions.
26
- if (typeof (/** @type {any} */ (globalThis).fetch) !== 'function') /** @type {any} */ (globalThis).fetch = fetch;
25
+ // The engine has no `fetch`. This one asks the host, which checks the app's
26
+ // permissions. The classes around it come along: libraries test for them.
27
+ for (const [name, value] of Object.entries({ fetch, Headers, Request, Response })) {
28
+ if (typeof (/** @type {any} */ (globalThis)[name]) !== 'function') /** @type {any} */ (globalThis)[name] = value;
29
+ }
27
30
 
28
31
  // The host's entry points into JS. It calls these and nothing else.
29
32
  /** @type {any} */ (globalThis).__sb = {
package/types/fetch.d.ts CHANGED
@@ -1,56 +1,87 @@
1
1
  /**
2
- * @param {string | { url: string } | { href: string }} input
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<FetchResponse>}
4
+ * @returns {Promise<Response>}
5
5
  */
6
- export function fetch(input: string | {
7
- url: string;
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<FetchResponse>;
15
- declare class FetchResponse {
16
- /** @param {{ id: number, status: number, status_text: string, url: string, redirected: boolean, headers: [string, string][] }} head */
17
- constructor(head: {
18
- id: number;
19
- status: number;
20
- status_text: string;
21
- url: string;
22
- redirected: boolean;
23
- headers: [string, string][];
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
- /** The body stays with the host until it is asked for. */
33
- body_id: number;
34
- /** @param {'text' | 'base64'} format @returns {Promise<string>} */
35
- read(format: "text" | "base64"): Promise<string>;
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
- bytes(): Promise<Uint8Array<ArrayBuffer>>;
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 {};
@@ -5,6 +5,14 @@
5
5
  * @param {string} to
6
6
  */
7
7
  export function convert(from: string, to: string): void;
8
+ /**
9
+ * A stylesheet moved from `dir` (relative to the build, `/`-separated) to
10
+ * app.css at its root. Its relative `url()`s, such as a font next to it in
11
+ * `_app/immutable/assets`, have to say where they now are from there.
12
+ * @param {string} css
13
+ * @param {string} dir
14
+ */
15
+ export function rebase_css(css: string, dir: string): string;
8
16
  /**
9
17
  * @typedef {import('./vite.js').Config & {
10
18
  * kit?: import('@sveltejs/kit/vite').Config,
package/vite/app.js CHANGED
@@ -86,9 +86,11 @@ export function dev_window(project) {
86
86
  return;
87
87
  }
88
88
 
89
- let bin = find_host();
90
- if (bin === null) {
91
- console.warn('sepal: no calyx (host binary) found, so the app will not open. Set SEPAL_HOST.');
89
+ let bin;
90
+ try {
91
+ bin = find_host().path;
92
+ } catch (error) {
93
+ console.warn(`sepal: the app will not open: ${/** @type {Error} */ (error).message}`);
92
94
  gave_up = true;
93
95
  return;
94
96
  }
package/vite/sveltekit.js CHANGED
@@ -19,7 +19,7 @@
19
19
  // Runs in Node.
20
20
 
21
21
  import { cpSync, existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from 'node:fs';
22
- import { dirname, join, relative, resolve } from 'node:path';
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';
@@ -110,7 +110,7 @@ ${start}(element);
110
110
  );
111
111
 
112
112
  const styles = [...html.matchAll(/<link href="\/([^"]+)" rel="stylesheet">/g)].map((link) => link[1]).filter((file) => files.includes(file));
113
- writeFileSync(join(to, 'app.css'), styles.map((file) => readFileSync(join(from, file), 'utf8')).join('\n'));
113
+ writeFileSync(join(to, 'app.css'), styles.map((file) => rebase_css(readFileSync(join(from, file), 'utf8'), posix.dirname(file))).join('\n'));
114
114
 
115
115
  // Everything else is for the app to load by its path: static files, and
116
116
  // what SvelteKit put in `_app/immutable/assets`.
@@ -121,6 +121,20 @@ ${start}(element);
121
121
  }
122
122
  }
123
123
 
124
+ /**
125
+ * A stylesheet moved from `dir` (relative to the build, `/`-separated) to
126
+ * app.css at its root. Its relative `url()`s, such as a font next to it in
127
+ * `_app/immutable/assets`, have to say where they now are from there.
128
+ * @param {string} css
129
+ * @param {string} dir
130
+ */
131
+ export function rebase_css(css, dir) {
132
+ return css.replace(
133
+ /url\(\s*(['"]?)(?![a-z][a-z0-9+.-]*:|\/|#)([^'")]+)\1\s*\)/gi,
134
+ (_, quote, path) => `url(${quote}${posix.join(dir, path)}${quote})`
135
+ );
136
+ }
137
+
124
138
  /**
125
139
  * @typedef {import('./vite.js').Config & {
126
140
  * kit?: import('@sveltejs/kit/vite').Config,